-
Notifications
You must be signed in to change notification settings - Fork 110
/
ticker.go
35 lines (31 loc) · 879 Bytes
/
ticker.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
package utils
import (
"context"
"time"
"go.viam.com/rdk/logging"
)
// SlowStartupLogger starts a goroutine that logs every few seconds as long as the context has not timed out or was not cancelled.
func SlowStartupLogger(ctx context.Context, msg, fieldName, fieldVal string, logger logging.Logger) func() {
slowTicker := time.NewTicker(2 * time.Second)
firstTick := true
ctxWithCancel, cancel := context.WithCancel(ctx)
startTime := time.Now()
go func() {
for {
select {
case <-slowTicker.C:
elapsed := time.Since(startTime).Seconds()
logger.CWarnw(ctx, msg, fieldName, fieldVal, "time elapsed", elapsed)
if firstTick {
slowTicker.Reset(3 * time.Second)
firstTick = false
} else {
slowTicker.Reset(5 * time.Second)
}
case <-ctxWithCancel.Done():
return
}
}
}()
return func() { slowTicker.Stop(); cancel() }
}