You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
v2ray-core/common/task/periodic.go

85 lines
1.4 KiB

package task
import (
"sync"
"time"
)
// Periodic is a task that runs periodically.
type Periodic struct {
7 years ago
// Interval of the task being run
Interval time.Duration
7 years ago
// Execute is the task function
Execute func() error
7 years ago
// OnFailure will be called when Execute returns non-nil error
OnError func(error)
access sync.RWMutex
timer *time.Timer
closed bool
}
func (t *Periodic) setClosed(f bool) {
t.access.Lock()
t.closed = f
t.access.Unlock()
}
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
}
if err := t.Execute(); err != nil {
return err
}
t.access.Lock()
defer t.access.Unlock()
if t.closed {
return nil
}
t.timer = time.AfterFunc(t.Interval, func() {
7 years ago
if err := t.checkedExecute(); err != nil && t.OnError != nil {
t.OnError(err)
}
})
return nil
}
7 years ago
// Start implements common.Runnable. Start must not be called multiple times without Close being called.
func (t *Periodic) Start() error {
t.setClosed(false)
if err := t.checkedExecute(); err != nil {
t.setClosed(true)
return err
}
return nil
}
// Close implements common.Closable.
func (t *Periodic) Close() error {
t.access.Lock()
defer t.access.Unlock()
t.closed = true
if t.timer != nil {
t.timer.Stop()
t.timer = nil
}
return nil
}