-
Notifications
You must be signed in to change notification settings - Fork 1
/
clock.go
241 lines (183 loc) · 4.57 KB
/
clock.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
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
package goclock
import (
"context"
"sync"
"time"
"github.com/benbjohnson/clock"
)
const (
// Day represents full day.
Day = 24 * time.Hour
// Week represents full week.
Week = 7 * Day
)
var (
// def is the package-level clock.Clock instance.
def = newClk(clock.New())
)
func newClk(clk clock.Clock) *localClock {
lClk := &localClock{
clock: clk,
}
return lClk
}
type localClock struct {
// Used to sync overriding clock. Locking is enabled by Default
mutex mutexWrap
clock clock.Clock
}
func (l *localClock) After(d time.Duration) <-chan time.Time {
l.mutex.Lock()
defer l.mutex.Unlock()
return l.clock.After(d)
}
func (l *localClock) AfterFunc(d time.Duration, f func()) *clock.Timer {
l.mutex.Lock()
defer l.mutex.Unlock()
return l.clock.AfterFunc(d, f)
}
func (l *localClock) Now() time.Time {
l.mutex.Lock()
defer l.mutex.Unlock()
return l.clock.Now()
}
func (l *localClock) Since(t time.Time) time.Duration {
l.mutex.Lock()
defer l.mutex.Unlock()
return l.clock.Since(t)
}
func (l *localClock) Sleep(d time.Duration) {
l.mutex.Lock()
defer l.mutex.Unlock()
l.clock.Sleep(d)
}
func (l *localClock) Tick(d time.Duration) <-chan time.Time {
l.mutex.Lock()
defer l.mutex.Unlock()
return l.clock.Tick(d)
}
func (l *localClock) Ticker(d time.Duration) *clock.Ticker {
l.mutex.Lock()
defer l.mutex.Unlock()
return l.clock.Ticker(d)
}
func (l *localClock) Timer(d time.Duration) *clock.Timer {
l.mutex.Lock()
defer l.mutex.Unlock()
return l.clock.Timer(d)
}
func (l *localClock) Until(t time.Time) time.Duration {
l.mutex.Lock()
defer l.mutex.Unlock()
return l.clock.Until(t)
}
func (l *localClock) WithDeadline(parent context.Context, d time.Time) (context.Context, context.CancelFunc) {
l.mutex.Lock()
defer l.mutex.Unlock()
return l.clock.WithDeadline(parent, d)
}
func (l *localClock) WithTimeout(parent context.Context, t time.Duration) (context.Context, context.CancelFunc) {
l.mutex.Lock()
defer l.mutex.Unlock()
return l.clock.WithTimeout(parent, t)
}
func (l *localClock) set(clk clock.Clock) {
l.mutex.Lock()
defer l.mutex.Unlock()
l.clock = clk
}
func (l *localClock) get() clock.Clock {
l.mutex.Lock()
defer l.mutex.Unlock()
return l.clock
}
func (l *localClock) restore() {
l.mutex.Lock()
defer l.mutex.Unlock()
l.clock = clock.New()
}
func (l *localClock) setNoLock() {
l.mutex.Disable()
}
// After waits for the duration to elapse and then sends the current time
func After(d time.Duration) <-chan time.Time {
return def.After(d)
}
// AfterFunc waits for the duration to elapse and then calls f in its own goroutine.
func AfterFunc(d time.Duration, f func()) *clock.Timer {
return def.AfterFunc(d, f)
}
// Now returns the current local time.
func Now() time.Time {
return def.Now()
}
// Since returns the time elapsed since t.
func Since(t time.Time) time.Duration {
return def.Since(t)
}
// Sleep pauses the current goroutine for at least the duration d.
func Sleep(d time.Duration) {
def.Sleep(d)
}
// Tick is a convenience wrapper for NewTicker providing access to the ticking channel only.
func Tick(d time.Duration) <-chan time.Time {
return def.Tick(d)
}
// Ticker returns a new Ticker containing a channel that will send the
// time with a period specified by the duration argument.
func Ticker(d time.Duration) *clock.Ticker {
return def.Ticker(d)
}
// Timer creates a new Timer that will send the current time on its channel after at least duration d.
func Timer(d time.Duration) *clock.Timer {
return def.Timer(d)
}
func Until(t time.Time) time.Duration {
return def.Until(t)
}
func WithDeadline(parent context.Context, d time.Time) (context.Context, context.CancelFunc) {
return def.WithDeadline(parent, d)
}
func WithTimeout(parent context.Context, t time.Duration) (context.Context, context.CancelFunc) {
return def.WithTimeout(parent, t)
}
// Current returns current instance of clock
func Current() clock.Clock {
return def.get()
}
// Mock sets the Clock with a mock frozen at the given time and returns it.
func Mock(now time.Time) *clock.Mock {
mock := clock.NewMock()
mock.Set(now)
def.set(mock)
return mock
}
// Set sets the clock.
func Set(clk clock.Clock) {
def.set(clk)
}
// Restore replaces the Clock with the real clock.
func Restore() {
def.restore()
}
// NoLock removes locking mechanism usage on clock.
func NoLock() {
def.setNoLock()
}
type mutexWrap struct {
lock sync.Mutex
disabled bool
}
func (mw *mutexWrap) Lock() {
if !mw.disabled {
mw.lock.Lock()
}
}
func (mw *mutexWrap) Unlock() {
if !mw.disabled {
mw.lock.Unlock()
}
}
func (mw *mutexWrap) Disable() {
mw.disabled = true
}