-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathticker.go
More file actions
63 lines (58 loc) · 1.26 KB
/
ticker.go
File metadata and controls
63 lines (58 loc) · 1.26 KB
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
package clock
import (
"time"
)
// Ticker represents a time.Ticker.
type Ticker struct {
C <-chan time.Time
Stop func()
// 当 ticker != nil 的时候, Ticker 代表了 real clock
ticker *time.Ticker
*task
}
// NewTicker returns a new Ticker containing a channel that will send the
// current time with a period specified by the duration d.
func (s *Simulator) NewTicker(d time.Duration) *Ticker {
s.Lock()
defer s.Unlock()
if d <= 0 {
panic("non-positive interval for NewTicker")
}
return s.newTicker(d)
}
// Tick is a convenience wrapper for NewTicker providing access to the ticking
// channel only.
func (s *Simulator) Tick(d time.Duration) <-chan time.Time {
s.Lock()
defer s.Unlock()
if d <= 0 {
return nil
}
return s.newTicker(d).C
}
func (s *Simulator) newTicker(d time.Duration) *Ticker {
c := make(chan time.Time, 1)
run := func(t *task) *task {
// time.Tick.C 的发送逻辑是
// 能发送,就发送
// 不能发送,就抛弃
// 所以,c 带有缓存,免得全部都丢弃了
select {
case c <- s.now:
default:
}
t.deadline = t.deadline.Add(d)
return t
}
t := &Ticker{
C: c,
task: newTask(s.now.Add(d), run),
}
t.Stop = func() {
s.Lock()
s.heap.remove(t.task)
s.Unlock()
}
s.accept(t.task)
return t
}