forked from kudoochui/kudos
-
Notifications
You must be signed in to change notification settings - Fork 0
/
timer.go
89 lines (77 loc) · 1.58 KB
/
timer.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
package connector
import (
"sync"
"time"
"github.com/chslink/kudos/utils/timer"
)
type Timers struct {
dispatcher *timer.Dispatcher
timerDispatcherLen int
chanJob chan *TimeJob
chanStop chan *timer.Timer
closed bool
lock sync.RWMutex
}
func NewTimer() *Timers {
return &Timers{
timerDispatcherLen: 20,
dispatcher: timer.NewDispatcher(20),
chanJob: make(chan *TimeJob, 5),
chanStop: make(chan *timer.Timer, 20),
}
}
type TimeJob struct {
timeout time.Duration
cronExpr *timer.CronExpr
f func()
}
func (t *Timers) RunTimer(closeSig chan bool) {
//t.dispatcher = timer.NewDispatcher(t.timerDispatcherLen)
for {
select {
case <-closeSig:
t.lock.Lock()
t.closed = true
t.lock.Unlock()
return
case tt := <-t.dispatcher.ChanTimer:
tt.Cb()
case job := <-t.chanJob:
t.work(job)
case handler := <-t.chanStop:
handler.Stop()
}
}
}
func (t *Timers) work(job *TimeJob) {
if job.cronExpr != nil {
} else {
}
}
func (t *Timers) AfterFunc(d time.Duration, cb func()) *timer.Timer {
t.lock.RLock()
if t.closed {
t.lock.RUnlock()
return nil
}
t.lock.RUnlock()
return t.dispatcher.AfterFunc(d, cb)
}
func (t *Timers) CronFunc(cronExpr *timer.CronExpr, cb func()) *timer.Cron {
t.lock.RLock()
if t.closed {
t.lock.RUnlock()
return nil
}
t.lock.RUnlock()
return t.dispatcher.CronFunc(cronExpr, cb)
}
func (t *Timers) ClearTimeout(handler *timer.Timer) {
t.lock.RLock()
if t.closed {
t.lock.RUnlock()
return
}
t.lock.RUnlock()
t.chanStop <- handler
}