2018-05-17 11:21:04 +00:00
|
|
|
package mailserver
|
|
|
|
|
2018-07-04 09:30:57 +00:00
|
|
|
import (
|
|
|
|
"sync"
|
|
|
|
"time"
|
|
|
|
)
|
2018-05-17 11:21:04 +00:00
|
|
|
|
|
|
|
type ticker struct {
|
2018-07-04 09:30:57 +00:00
|
|
|
mu sync.RWMutex
|
2018-05-17 11:21:04 +00:00
|
|
|
timeTicker *time.Ticker
|
|
|
|
}
|
|
|
|
|
|
|
|
func (t *ticker) run(period time.Duration, fn func()) {
|
|
|
|
if t.timeTicker != nil {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2018-07-04 09:30:57 +00:00
|
|
|
tt := time.NewTicker(period)
|
|
|
|
t.mu.Lock()
|
|
|
|
t.timeTicker = tt
|
|
|
|
t.mu.Unlock()
|
2018-05-17 11:21:04 +00:00
|
|
|
go func() {
|
2018-07-04 09:30:57 +00:00
|
|
|
for range tt.C {
|
2018-05-17 11:21:04 +00:00
|
|
|
fn()
|
|
|
|
}
|
|
|
|
}()
|
|
|
|
}
|
|
|
|
|
|
|
|
func (t *ticker) stop() {
|
2018-07-04 09:30:57 +00:00
|
|
|
t.mu.RLock()
|
2018-05-17 11:21:04 +00:00
|
|
|
t.timeTicker.Stop()
|
2018-07-04 09:30:57 +00:00
|
|
|
t.mu.RUnlock()
|
2018-05-17 11:21:04 +00:00
|
|
|
}
|