-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathwatchdog.go
97 lines (90 loc) · 1.96 KB
/
watchdog.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
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
package watchdog
import (
"context"
"errors"
"time"
"github.com/google/uuid"
"github.com/redis/go-redis/v9"
)
// Watchdog is a tool to monitor the status of a service.
type Watchdog struct {
RetryMaxWaitDuration time.Duration
RetryIntervalDuration time.Duration
IsUnlock bool // wether to unlock the event after the execution is completed, default is true
}
func Default() *Watchdog {
return &Watchdog{
RetryMaxWaitDuration: 5 * time.Second,
RetryIntervalDuration: 100 * time.Millisecond,
IsUnlock: true,
}
}
func (w *Watchdog) Clone() *Watchdog {
return &Watchdog{
RetryMaxWaitDuration: w.RetryMaxWaitDuration,
RetryIntervalDuration: w.RetryIntervalDuration,
IsUnlock: w.IsUnlock,
}
}
func (w *Watchdog) Watch(client *redis.Client, key string, duration time.Duration, fn func(ctx context.Context) error) error {
val, err := uuid.NewV7()
if err != nil {
return err
}
newCtx, cancel := context.WithCancel(context.Background())
defer cancel()
returyCtx, cancel := context.WithTimeout(newCtx, w.RetryMaxWaitDuration)
defer cancel()
closeChan := make(chan struct{})
defer close(closeChan)
var (
c = &ctx{
client: client,
ctx: newCtx,
key: key,
val: val.String(),
expireDuration: duration,
}
ok bool
)
for {
ok, err = c.Lock()
if err != nil || !ok {
select {
case <-returyCtx.Done():
return errors.New("watchdog: timeout")
case <-time.NewTimer(w.RetryIntervalDuration).C:
continue // lock reentrant
}
}
break
}
if !ok {
return errors.New("watchdog: lock failed")
}
go func() {
ticker := time.NewTicker(duration / 3)
defer func() {
ticker.Stop()
if w.IsUnlock {
_, _ = c.Unlock()
}
}()
for {
select {
case <-ticker.C:
_ = c.Expire()
case <-closeChan:
return
case <-newCtx.Done():
return
}
}
}()
defer func() {
if w.IsUnlock {
_, _ = c.Unlock()
}
}()
return fn(c.ctx)
}