-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathdispatcher.go
More file actions
324 lines (282 loc) · 8.32 KB
/
Copy pathdispatcher.go
File metadata and controls
324 lines (282 loc) · 8.32 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
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
package ripple
import (
"context"
"errors"
"math/rand"
"sync"
"time"
)
const (
maxBackoffDuration = 30 * time.Second
maxJitterMs = 1000
)
// Dispatcher manages event queuing, batching, flushing, and retry logic.
type Dispatcher struct {
config DispatcherConfig
queue *Queue
httpAdapter HTTPAdapter
storageAdapter StorageAdapter
loggerAdapter LoggerAdapter
headers map[string]string
timer *time.Timer
flushMu sync.Mutex
retryCancel context.CancelFunc
disposed bool
mu sync.Mutex
}
// NewDispatcher creates a new Dispatcher instance.
func NewDispatcher(config DispatcherConfig, httpAdapter HTTPAdapter, storageAdapter StorageAdapter, loggerAdapter LoggerAdapter) *Dispatcher {
return &Dispatcher{
config: config,
queue: NewQueue(),
httpAdapter: httpAdapter,
storageAdapter: storageAdapter,
loggerAdapter: loggerAdapter,
headers: map[string]string{
config.APIKeyHeader: config.APIKey,
"Content-Type": "application/json",
},
}
}
// Enqueue adds an event to the queue.
func (d *Dispatcher) Enqueue(event Event) {
d.mu.Lock()
if d.disposed {
d.mu.Unlock()
d.loggerAdapter.Warn("Cannot enqueue event: Dispatcher has been disposed")
return
}
d.mu.Unlock()
d.queue.Enqueue(event)
// Apply buffer limit and persist
eventsToSave := d.applyQueueLimit(d.queue.ToSlice())
if len(eventsToSave) < d.queue.Len() {
d.queue.Clear()
d.queue.LoadFromSlice(eventsToSave)
}
if err := d.storageAdapter.Save(eventsToSave); err != nil {
d.logStorageError("Failed to persist events to storage", err, map[string]any{
"queueSize": d.queue.Len(),
})
}
if d.queue.Len() >= d.config.MaxBatchSize {
d.Flush()
} else {
d.scheduleFlush()
}
}
// Flush immediately flushes all queued events.
func (d *Dispatcher) Flush() {
d.flushMu.Lock()
defer d.flushMu.Unlock()
d.stopTimer()
if d.queue.IsEmpty() {
return
}
ctx, cancel := context.WithCancel(context.Background())
d.mu.Lock()
d.retryCancel = cancel
d.mu.Unlock()
defer cancel()
allEvents := d.queue.ToSlice()
d.queue.Clear()
for i := 0; i < len(allEvents); i += d.config.MaxBatchSize {
end := i + d.config.MaxBatchSize
if end > len(allEvents) {
end = len(allEvents)
}
d.sendWithRetry(ctx, allEvents[i:end], 0)
}
}
// Restore loads persisted events from storage.
func (d *Dispatcher) Restore() {
d.mu.Lock()
d.disposed = false
d.mu.Unlock()
events, err := d.storageAdapter.Load()
if err != nil {
d.loggerAdapter.Error("Failed to restore events from storage", map[string]any{
"error": err.Error(),
})
return
}
limited := d.applyQueueLimit(events)
d.queue.LoadFromSlice(limited)
if d.queue.Len() > 0 {
d.scheduleFlush()
}
}
// Dispose cleans up resources: aborts retries, clears queue, releases mutex.
func (d *Dispatcher) Dispose() {
d.mu.Lock()
d.disposed = true
cancel := d.retryCancel
d.mu.Unlock()
if cancel != nil {
cancel()
}
d.stopTimer()
d.queue.Clear()
if err := d.storageAdapter.Close(); err != nil {
d.loggerAdapter.Error("failed to close storage adapter", map[string]any{
"error": err.Error(),
})
}
}
// applyQueueLimit applies the maxBufferSize limit using FIFO eviction.
func (d *Dispatcher) applyQueueLimit(events []Event) []Event {
if d.config.MaxBufferSize > 0 && len(events) > d.config.MaxBufferSize {
return events[len(events)-d.config.MaxBufferSize:]
}
return events
}
// sendWithRetry sends events with exponential backoff retry logic.
// Note: This method never logs headers to prevent API key exposure.
func (d *Dispatcher) sendWithRetry(ctx context.Context, events []Event, attempt int) {
resp, err := d.httpAdapter.SendWithContext(ctx, d.config.Endpoint, events, d.headers)
if err != nil {
d.handleNetworkError(ctx, err, events, attempt)
} else {
d.handleResponse(ctx, resp, events, attempt)
}
}
func (d *Dispatcher) handleResponse(ctx context.Context, resp *HTTPResponse, events []Event, attempt int) {
if resp.Status >= 200 && resp.Status < 300 {
if err := d.storageAdapter.Clear(); err != nil {
d.loggerAdapter.Error("Failed to clear storage after successful send", map[string]any{
"error": err.Error(),
})
}
} else if resp.Status >= 400 && resp.Status < 500 {
d.loggerAdapter.Warn("4xx client error, dropping events", map[string]any{
"status": resp.Status,
"eventsCount": len(events),
})
if err := d.storageAdapter.Clear(); err != nil {
d.loggerAdapter.Error("Failed to clear storage after 4xx error", map[string]any{
"error": err.Error(),
})
}
} else if resp.Status >= 500 {
d.handleServerError(ctx, resp.Status, events, attempt)
} else {
d.loggerAdapter.Warn("Unexpected status code, dropping events", map[string]any{
"status": resp.Status,
"eventsCount": len(events),
})
if err := d.storageAdapter.Clear(); err != nil {
d.loggerAdapter.Error("Failed to clear storage after unexpected status", map[string]any{
"error": err.Error(),
})
}
}
}
func (d *Dispatcher) handleServerError(ctx context.Context, status int, events []Event, attempt int) {
if attempt < d.config.MaxRetries {
d.loggerAdapter.Warn("5xx server error, retrying", map[string]any{
"status": status,
"attempt": attempt + 1,
"maxRetries": d.config.MaxRetries,
})
if !d.delay(ctx, d.calculateBackoff(attempt)) {
return
}
d.sendWithRetry(ctx, events, attempt+1)
} else {
d.loggerAdapter.Error("5xx server error, max retries reached", map[string]any{
"status": status,
"maxRetries": d.config.MaxRetries,
"eventsCount": len(events),
})
d.requeueEvents(events)
}
}
func (d *Dispatcher) handleNetworkError(ctx context.Context, err error, events []Event, attempt int) {
d.loggerAdapter.Error("Network error occurred", map[string]any{"error": err.Error()})
if attempt < d.config.MaxRetries {
d.loggerAdapter.Warn("Network error, retrying", map[string]any{
"attempt": attempt + 1,
"maxRetries": d.config.MaxRetries,
"error": err.Error(),
})
if !d.delay(ctx, d.calculateBackoff(attempt)) {
return
}
d.sendWithRetry(ctx, events, attempt+1)
} else {
d.loggerAdapter.Error("Network error, max retries reached", map[string]any{
"maxRetries": d.config.MaxRetries,
"eventsCount": len(events),
"error": err.Error(),
})
d.requeueEvents(events)
}
}
func (d *Dispatcher) requeueEvents(events []Event) {
currentQueue := d.queue.ToSlice()
events = append(events, currentQueue...)
limited := d.applyQueueLimit(events)
d.queue.Clear()
d.queue.LoadFromSlice(limited)
if err := d.storageAdapter.Save(limited); err != nil {
d.logStorageError("Failed to persist events after requeue", err, nil)
}
}
// scheduleFlush schedules a one-shot flush after the configured interval.
func (d *Dispatcher) scheduleFlush() {
d.mu.Lock()
defer d.mu.Unlock()
if d.disposed || d.timer != nil {
return
}
d.timer = time.AfterFunc(d.config.FlushInterval, func() {
d.mu.Lock()
d.timer = nil
d.mu.Unlock()
d.Flush()
})
}
func (d *Dispatcher) stopTimer() {
d.mu.Lock()
defer d.mu.Unlock()
if d.timer != nil {
d.timer.Stop()
d.timer = nil
}
}
// logStorageError logs storage errors, using warn level for StorageQuotaExceededError.
func (d *Dispatcher) logStorageError(message string, err error, extra map[string]any) {
args := map[string]any{"error": err.Error()}
for k, v := range extra {
args[k] = v
}
var quotaErr *StorageQuotaExceededError
if errors.As(err, "aErr) {
d.loggerAdapter.Warn(message, args)
} else {
d.loggerAdapter.Error(message, args)
}
}
// calculateBackoff computes exponential backoff with jitter.
// Formula: (2^attempt seconds) + random jitter, capped at 30s.
// Example progression: 1s, 2s, 4s, 8s, 16s, 30s (capped).
func (d *Dispatcher) calculateBackoff(attempt int) time.Duration {
// Exponential backoff: 2^attempt seconds
backoff := time.Duration(1<<attempt) * time.Second
if backoff > maxBackoffDuration {
backoff = maxBackoffDuration
}
// Add random jitter (0-1000ms) to prevent thundering herd
jitter := time.Duration(rand.Intn(maxJitterMs)) * time.Millisecond
return backoff + jitter
}
// delay waits for the given duration or until context is cancelled.
// Returns true if the delay completed, false if cancelled.
func (d *Dispatcher) delay(ctx context.Context, duration time.Duration) bool {
select {
case <-time.After(duration):
return true
case <-ctx.Done():
return false
}
}