-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathworker.go
More file actions
166 lines (142 loc) · 2.79 KB
/
Copy pathworker.go
File metadata and controls
166 lines (142 loc) · 2.79 KB
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
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
package checkend
import (
"context"
"fmt"
"sync"
"time"
)
// Worker handles asynchronous sending of notices.
type Worker struct {
config *Configuration
client *Client
queue chan *Notice
done chan struct{}
wg sync.WaitGroup
flushCh chan chan struct{}
running bool
runningMu sync.Mutex
}
// NewWorker creates a new Worker.
func NewWorker(config *Configuration) *Worker {
return &Worker{
config: config,
client: NewClient(config),
queue: make(chan *Notice, config.MaxQueueSize),
done: make(chan struct{}),
flushCh: make(chan chan struct{}),
}
}
// Start starts the worker goroutine.
func (w *Worker) Start() {
w.runningMu.Lock()
defer w.runningMu.Unlock()
if w.running {
return
}
w.running = true
w.wg.Add(1)
go w.run()
}
// Stop stops the worker and waits for pending notices with timeout.
func (w *Worker) Stop() {
w.runningMu.Lock()
if !w.running {
w.runningMu.Unlock()
return
}
w.running = false
w.runningMu.Unlock()
close(w.done)
// Wait with timeout
done := make(chan struct{})
go func() {
w.wg.Wait()
close(done)
}()
select {
case <-done:
// Graceful shutdown completed
case <-time.After(w.config.ShutdownTimeout):
// Timeout reached
if w.config.Debug {
fmt.Println("[Checkend] [warning] Shutdown timeout reached, some notices may not have been sent")
}
}
}
// Push adds a notice to the queue.
func (w *Worker) Push(notice *Notice) bool {
w.runningMu.Lock()
running := w.running
w.runningMu.Unlock()
if !running {
return false
}
select {
case w.queue <- notice:
return true
default:
// Queue full
return false
}
}
// Flush waits for all queued notices to be sent.
func (w *Worker) Flush() {
w.runningMu.Lock()
running := w.running
w.runningMu.Unlock()
if !running || len(w.queue) == 0 {
return
}
done := make(chan struct{})
w.flushCh <- done
<-done
}
func (w *Worker) run() {
defer w.wg.Done()
for {
select {
case <-w.done:
w.drain()
return
case notice := <-w.queue:
w.sendWithRetry(notice, 3)
case done := <-w.flushCh:
// Drain the queue for flush
for len(w.queue) > 0 {
select {
case notice := <-w.queue:
w.sendWithRetry(notice, 3)
default:
break
}
}
close(done)
}
}
}
func (w *Worker) sendWithRetry(notice *Notice, maxRetries int) {
for attempt := 0; attempt < maxRetries; attempt++ {
resp := w.client.Send(notice)
if resp != nil {
return
}
if attempt < maxRetries-1 {
delay := time.Duration(1<<uint(attempt)) * 100 * time.Millisecond
time.Sleep(delay)
}
}
}
func (w *Worker) drain() {
ctx, cancel := context.WithTimeout(context.Background(), w.config.ShutdownTimeout)
defer cancel()
for {
select {
case <-ctx.Done():
return
case notice := <-w.queue:
w.client.Send(notice)
default:
return
}
}
}