-
Notifications
You must be signed in to change notification settings - Fork 0
/
utils.go
48 lines (39 loc) · 941 Bytes
/
utils.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
package feedfollower
import (
"context"
"log"
"sync"
"time"
)
func RunPeriodically(toRun func(), interval time.Duration, wg *sync.WaitGroup, ctx context.Context) {
/*
A wrapper to run function repeatedly over specified intervals of time.
wg should be provided in order to properfly await function from ouside
ctx enables graceful shutdown in case interrupt event is received
During the normal operation the function runs over and over indefinitely and
wait on wg never returns
*/
defer wg.Done()
ticker := time.NewTicker(interval)
defer ticker.Stop()
toRun()
for {
select {
case <-ctx.Done():
log.Println("Requested shutdown of periodic function, exiting")
return
case <-ticker.C:
// TODO: add timeout for this function
toRun()
}
}
}
func ProcessChannelEventUntilContextDone(processor func(), ctx context.Context) {
for {
select {
case <-ctx.Done():
default:
processor()
}
}
}