mirror of
https://github.com/shtorm-7/sing-box-extended.git
synced 2026-07-31 19:14:17 +03:00
Add admin panel, manager, node_manager, bandwidth limiter, connection limiter, bonding, failover, vless encryption, mkcp transport
This commit is contained in:
58
transport/v2raykcp/updater.go
Normal file
58
transport/v2raykcp/updater.go
Normal file
@@ -0,0 +1,58 @@
|
||||
package v2raykcp
|
||||
|
||||
import (
|
||||
"sync/atomic"
|
||||
"time"
|
||||
)
|
||||
|
||||
type Updater struct {
|
||||
interval int64
|
||||
shouldContinue func() bool
|
||||
shouldTerminate func() bool
|
||||
updateFunc func()
|
||||
notifier chan struct{}
|
||||
}
|
||||
|
||||
func NewUpdater(interval uint32, shouldContinue func() bool, shouldTerminate func() bool, updateFunc func()) *Updater {
|
||||
u := &Updater{
|
||||
interval: int64(time.Duration(interval) * time.Millisecond),
|
||||
shouldContinue: shouldContinue,
|
||||
shouldTerminate: shouldTerminate,
|
||||
updateFunc: updateFunc,
|
||||
notifier: make(chan struct{}, 1),
|
||||
}
|
||||
return u
|
||||
}
|
||||
|
||||
func (u *Updater) WakeUp() {
|
||||
select {
|
||||
case u.notifier <- struct{}{}:
|
||||
go u.run()
|
||||
default:
|
||||
}
|
||||
}
|
||||
|
||||
func (u *Updater) run() {
|
||||
defer func() {
|
||||
<-u.notifier
|
||||
}()
|
||||
|
||||
if u.shouldTerminate() {
|
||||
return
|
||||
}
|
||||
ticker := time.NewTicker(u.Interval())
|
||||
defer ticker.Stop()
|
||||
|
||||
for u.shouldContinue() {
|
||||
u.updateFunc()
|
||||
<-ticker.C
|
||||
}
|
||||
}
|
||||
|
||||
func (u *Updater) Interval() time.Duration {
|
||||
return time.Duration(atomic.LoadInt64(&u.interval))
|
||||
}
|
||||
|
||||
func (u *Updater) SetInterval(d time.Duration) {
|
||||
atomic.StoreInt64(&u.interval, int64(d))
|
||||
}
|
||||
Reference in New Issue
Block a user