-
Notifications
You must be signed in to change notification settings - Fork 0
/
delay.go
39 lines (35 loc) · 1.11 KB
/
delay.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
package main
import (
"sync/atomic"
"time"
)
// Generally you want to steer clear of sync/atomic, but simple numbers like this where
// you are concurrently storing a usage statistic is one of the few cases where it
// makes sense over a mutex. atomic does not provide read/write ordering guarantees
// which don't matter here as much as general trends do. Be very careful about
// any changes, as there are a lot of hidden gotchas when working with atomic values.
// Read in detail the discussion here:
// https://stackoverflow.com/questions/47445344/is-there-a-difference-in-go-between-a-counter-using-atomic-operations-and-one-us
var maxDelay int64 = 0
func updateMaxDelay(ts []time.Time) {
cur := atomic.LoadInt64(&maxDelay)
var max int64
for _, t := range ts {
// If a timestamp is set
if (t != time.Time{}) {
// how long ago is the log from?
lag := int64(time.Since(t))
if lag > max {
max = lag
}
}
}
if max > cur {
atomic.StoreInt64(&maxDelay, max)
}
}
func logMaxDelay() {
// Reset the value
val := atomic.SwapInt64(&maxDelay, 0)
lg.GaugeFloat("max_log_delay", time.Duration(val).Seconds())
}