mirror of
https://github.com/v2fly/v2ray-core.git
synced 2026-06-21 18:29:55 -04:00
update activity timer
This commit is contained in:
@@ -14,16 +14,26 @@ type Periodic struct {
|
||||
// OnFailure will be called when Execute returns non-nil error
|
||||
OnError func(error)
|
||||
|
||||
access sync.Mutex
|
||||
access sync.RWMutex
|
||||
timer *time.Timer
|
||||
closed bool
|
||||
}
|
||||
|
||||
func (t *Periodic) checkedExecute() error {
|
||||
func (t *Periodic) setClosed(f bool) {
|
||||
t.access.Lock()
|
||||
defer t.access.Unlock()
|
||||
t.closed = f
|
||||
t.access.Unlock()
|
||||
}
|
||||
|
||||
if t.closed {
|
||||
func (t *Periodic) hasClosed() bool {
|
||||
t.access.RLock()
|
||||
defer t.access.RUnlock()
|
||||
|
||||
return t.closed
|
||||
}
|
||||
|
||||
func (t *Periodic) checkedExecute() error {
|
||||
if t.hasClosed() {
|
||||
return nil
|
||||
}
|
||||
|
||||
@@ -31,23 +41,23 @@ func (t *Periodic) checkedExecute() error {
|
||||
return err
|
||||
}
|
||||
|
||||
t.access.Lock()
|
||||
t.timer = time.AfterFunc(t.Interval, func() {
|
||||
if err := t.checkedExecute(); err != nil && t.OnError != nil {
|
||||
t.OnError(err)
|
||||
}
|
||||
})
|
||||
t.access.Unlock()
|
||||
|
||||
return nil
|
||||
}
|
||||
|
||||
// Start implements common.Runnable. Start must not be called multiple times without Close being called.
|
||||
func (t *Periodic) Start() error {
|
||||
t.access.Lock()
|
||||
t.closed = false
|
||||
t.access.Unlock()
|
||||
t.setClosed(false)
|
||||
|
||||
if err := t.checkedExecute(); err != nil {
|
||||
t.closed = true
|
||||
t.setClosed(true)
|
||||
return err
|
||||
}
|
||||
|
||||
|
||||
@@ -3,7 +3,7 @@ package task
|
||||
import (
|
||||
"context"
|
||||
|
||||
"v2ray.com/core/common/signal"
|
||||
"v2ray.com/core/common/signal/semaphore"
|
||||
)
|
||||
|
||||
type Task func() error
|
||||
@@ -109,7 +109,7 @@ func execute(tasks ...Task) error {
|
||||
// executeParallel executes a list of tasks asynchronously, returns the first error encountered or nil if all tasks pass.
|
||||
func executeParallel(tasks ...Task) error {
|
||||
n := len(tasks)
|
||||
s := signal.NewSemaphore(n)
|
||||
s := semaphore.New(n)
|
||||
done := make(chan error, 1)
|
||||
|
||||
for _, task := range tasks {
|
||||
|
||||
Reference in New Issue
Block a user