-
Notifications
You must be signed in to change notification settings - Fork 24
/
manager.go
132 lines (116 loc) · 3.39 KB
/
manager.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
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
/*
Copyright (c) 2016, Percona LLC and/or its affiliates. All rights reserved.
This program is free software: you can redistribute it and/or modify
it under the terms of the GNU Affero General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU Affero General Public License for more details.
You should have received a copy of the GNU Affero General Public License
along with this program. If not, see <http://www.gnu.org/licenses/>
*/
package ticker
import (
"log"
"math"
"sync"
"time"
)
type Manager interface {
Add(c chan time.Time, atInterval uint, sync bool)
Remove(c chan time.Time)
ETA(c chan time.Time) float64
}
type Clock struct {
tickerFactory TickerFactory
syncTicker map[uint]Ticker
syncTickerMux *sync.Mutex
waitTicker map[chan time.Time]Ticker
waitTickerMux *sync.Mutex
nowFunc func() int64
watcher map[chan time.Time]Ticker
watcherMux *sync.Mutex
}
func NewClock(tickerFactory TickerFactory, nowFunc func() int64) *Clock {
r := &Clock{
tickerFactory: tickerFactory,
nowFunc: nowFunc,
syncTicker: make(map[uint]Ticker),
syncTickerMux: new(sync.Mutex),
waitTicker: make(map[chan time.Time]Ticker),
waitTickerMux: new(sync.Mutex),
watcher: make(map[chan time.Time]Ticker),
watcherMux: new(sync.Mutex),
}
return r
}
func (clock *Clock) Add(c chan time.Time, atInterval uint, sync bool) {
var ticker Ticker
var ok bool
if sync {
clock.syncTickerMux.Lock()
defer clock.syncTickerMux.Unlock()
ticker, ok = clock.syncTicker[atInterval]
if !ok {
ticker = clock.tickerFactory.Make(atInterval, sync)
go ticker.Run(clock.nowFunc())
clock.syncTicker[atInterval] = ticker
}
} else {
clock.waitTickerMux.Lock()
defer clock.waitTickerMux.Unlock()
if _, ok := clock.waitTicker[c]; ok {
log.Panic("WaitTicker exists for ", c)
}
ticker = clock.tickerFactory.Make(atInterval, sync)
go ticker.Run(clock.nowFunc())
clock.waitTicker[c] = ticker
}
ticker.Add(c)
clock.watcherMux.Lock()
defer clock.watcherMux.Unlock()
clock.watcher[c] = ticker
}
func (clock *Clock) Remove(c chan time.Time) {
clock.watcherMux.Lock()
defer clock.watcherMux.Unlock()
if ticker, ok := clock.watcher[c]; ok {
ticker.Remove(c)
delete(clock.watcher, c)
}
// todo: stop ticker if it has no watchers
clock.waitTickerMux.Lock()
defer clock.waitTickerMux.Unlock()
if _, ok := clock.waitTicker[c]; ok {
delete(clock.waitTicker, c)
}
}
func (clock *Clock) ETA(c chan time.Time) float64 {
clock.watcherMux.Lock()
defer clock.watcherMux.Unlock()
ticker, ok := clock.watcher[c]
if !ok {
return 0
}
return ticker.ETA(clock.nowFunc())
}
// Return time when interval began for current time.
func Began(interval uint, now uint) time.Time {
i := float64(interval)
t := float64(now)
d := uint(i - math.Mod(t, i))
if d != interval {
/**
* now is not an interval, so it's after the interval's start.
* E.g. if i=60 and now (t)=130, then t falls between intervals:
* 120
* 130 =t
* 180 d=50
* Interval began at 120, so decrease t by 10: i - d.
*/
now = now - (interval - d)
}
return time.Unix(int64(now), 0).UTC()
}