Repository navigation
Expand file tree
/
Copy pathtime.go
More file actions
88 lines (76 loc) · 2.04 KB
/
Copy pathtime.go
File metadata and controls
88 lines (76 loc) · 2.04 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
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
package utils
import (
"sync"
"sync/atomic"
"time"
)
var (
timestamp atomic.Uint32
updaterMu sync.Mutex
stopUpdater chan struct{}
updaterDone chan struct{}
)
// newTicker returns a channel and stop func because a fake *time.Ticker panics on Stop.
var (
now = time.Now
newTicker = func(d time.Duration) (<-chan time.Time, func()) {
t := time.NewTicker(d)
return t.C, t.Stop
}
)
// Timestamp returns the Unix time in seconds as a uint32, which wraps on 2106-02-07. While the updater
// runs it is the value it last published, at most about a second old, for the cost of one atomic load;
// otherwise it reads the clock.
func Timestamp() uint32 {
if t := timestamp.Load(); t != 0 {
return t
}
return clockTimestamp()
}
// clockTimestamp is Timestamp's out-of-line slow path.
//
//go:noinline
func clockTimestamp() uint32 {
return uint32(now().Unix())
}
// StartTimeStampUpdater starts a goroutine that refreshes the cached timestamp every second.
// Repeated and concurrent calls are safe.
func StartTimeStampUpdater() {
updaterMu.Lock()
defer updaterMu.Unlock()
if stopUpdater != nil {
return
}
timestamp.Store(uint32(now().Unix()))
stopUpdater = make(chan struct{})
updaterDone = make(chan struct{})
// Created before the goroutine so it exists when this returns.
tick, stopTick := newTicker(time.Second)
go func(stop, done chan struct{}) {
// stopTick before close(done): Stop's wait then implies release.
defer close(done)
defer stopTick()
for {
select {
case <-tick:
timestamp.Store(uint32(now().Unix()))
case <-stop:
return
}
}
}(stopUpdater, updaterDone)
}
// StopTimeStampUpdater stops the updater goroutine; call it on shutdown to avoid a leak.
// Timestamp reads the clock afterwards. Repeated and concurrent calls are safe.
func StopTimeStampUpdater() {
updaterMu.Lock()
defer updaterMu.Unlock()
if stopUpdater == nil {
return
}
close(stopUpdater)
<-updaterDone
stopUpdater = nil
updaterDone = nil
timestamp.Store(0) // nothing refreshes the value any more: read the clock instead
}