forked from influxdata/kapacitor
-
Notifications
You must be signed in to change notification settings - Fork 0
/
clock.go
102 lines (84 loc) · 2.01 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
/*
A clock that provides blocking calls that wait until absolute times have occurred. The clock can be controlled programmatically or be based of real time.
*/
package clock
import (
"sync"
"time"
)
// A clock interface to read time and wait until an absolute time arrives.
// Three implementations are available: A 'wall' clock that is based on realtime, a 'fast' clock that is always ahead and a 'set' clock that can be controlled via a setting time explicitly.
type Clock interface {
Setter
// Wait until time t has arrived. If t is in the past it immediately returns.
Until(t time.Time)
}
type Setter interface {
// Returns the start or 'zero' time of the clock
Zero() time.Time
// Set the time on the clock
Set(t time.Time)
}
// realtime implementation of clock
type wallclock struct {
zero time.Time
}
// Get a realtime wall clock.
func Wall() Clock {
return &wallclock{time.Now()}
}
func (w *wallclock) Zero() time.Time {
return w.zero
}
func (w *wallclock) Until(t time.Time) {
time.Sleep(t.Sub(time.Now()))
}
func (w *wallclock) Set(t time.Time) {}
// implementation of clock that is always in the future
type fastclock struct {
zero time.Time
}
// Get a realtime fast clock.
func Fast() Clock {
return &fastclock{time.Now()}
}
func (f *fastclock) Zero() time.Time {
return f.zero
}
func (f *fastclock) Until(t time.Time) {}
func (f *fastclock) Set(t time.Time) {}
// setable implementation of the clock
type setclock struct {
zero time.Time
now time.Time
cond *sync.Cond
}
// Get a clock that can be controlled programmatically.
func New(start time.Time) Clock {
l := &sync.Mutex{}
c := &setclock{
zero: start,
now: start,
cond: sync.NewCond(l),
}
return c
}
func (c *setclock) Zero() time.Time {
return c.zero
}
func (c *setclock) Until(t time.Time) {
c.cond.L.Lock()
for t.After(c.now) {
c.cond.Wait()
}
c.cond.L.Unlock()
}
func (c *setclock) Set(t time.Time) {
if t.Before(c.now) {
panic("cannot set time backwards")
}
c.cond.L.Lock()
c.now = t
c.cond.Broadcast()
c.cond.L.Unlock()
}