/
mticker.go
94 lines (83 loc) · 1.74 KB
/
mticker.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
90
91
92
93
94
package main
import (
"sync"
"time"
)
type mTicker struct {
mux sync.Mutex
subscribers subscribers
ticker *time.Ticker
stopCh chan struct{}
dropped int
}
type subscribers map[*subscriber]interface {
}
type subscriber struct {
tick chan time.Time
}
// creates and starts a new ticker that can
// have subscribed channels to receive ticks
func newMTicker(interval time.Duration) *mTicker {
t := &mTicker{
subscribers: make(subscribers),
stopCh: make(chan struct{}, 1),
ticker: time.NewTicker(interval),
}
go t.tick()
return t
}
func newSubscriber() *subscriber {
return &subscriber{
tick: make(chan time.Time, 1),
}
}
// Subscribe returns a channel to which ticks will be delivered. Ticks that
// can't be delivered to the channel, because it is not ready to receive, are
// discarded.
func (t *mTicker) subscribe() *subscriber {
t.mux.Lock()
defer t.mux.Unlock()
sub := newSubscriber()
t.subscribers[sub] = nil
return sub
}
// closes subscriber channel and
// removes from subscribers map
func (t *mTicker) unsubscribe(subscriber *subscriber) {
t.mux.Lock()
defer t.mux.Unlock()
close(subscriber.tick)
delete(t.subscribers, subscriber)
}
// Stop stops the ticker, and closes
// all subscribed channels
func (t *mTicker) stop() {
if t.stopCh != nil {
t.ticker.Stop()
t.stopCh <- struct{}{}
// close all subscribed time chans
for sub := range t.subscribers {
t.unsubscribe(sub)
}
}
}
// broadcast ticks to all
// subscribed channels
func (t *mTicker) tick() {
for {
select {
case tick := <-t.ticker.C:
t.mux.Lock()
for sub := range t.subscribers {
select {
case sub.tick <- tick:
default:
t.dropped++
}
}
t.mux.Unlock()
case <-t.stopCh:
return
}
}
}