/
periodically.go
64 lines (53 loc) · 1.1 KB
/
periodically.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
/*
© 2022–present Harald Rudell <harald.rudell@gmail.com> (https://haraldrudell.github.io/haraldrudell/)
ISC License
*/
package parl
import (
"context"
"sync"
"time"
)
const (
defaultPeriod = time.Second
)
type Periodically struct {
period time.Duration
fn func(t time.Time)
wg sync.WaitGroup
ctx context.Context
}
func NewPeriodically(fn func(t time.Time), ctx context.Context, period ...time.Duration) (periodically *Periodically) {
p := Periodically{ctx: ctx, fn: fn}
if len(period) > 0 {
p.period = period[0]
}
if p.period < defaultPeriod {
p.period = defaultPeriod
}
p.wg.Add(1)
go p.doThread()
return &p
}
func (p *Periodically) Wait() {
p.wg.Wait()
}
func (p *Periodically) doThread() {
defer p.wg.Done()
defer Recover(func() DA { return A() }, nil, Infallible)
ticker := time.NewTicker(p.period)
defer ticker.Stop()
done := p.ctx.Done()
for {
select {
case <-done:
return // context cancel exit
case t := <-ticker.C:
go p.doFn(t)
}
}
}
func (p *Periodically) doFn(t time.Time) {
defer Recover(func() DA { return A() }, nil, Infallible)
p.fn(t)
}