-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathqueue.go
More file actions
356 lines (320 loc) · 8.02 KB
/
queue.go
File metadata and controls
356 lines (320 loc) · 8.02 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
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
package microqueue
import (
"context"
"sync"
"sync/atomic"
"time"
)
// Queue is the user-facing interface for a single named queue.
type Queue interface {
// Producer APIs
Publish(msg Message) error
PublishWithDelay(msg Message, delay time.Duration) error
// Consumer APIs
Subscribe(consumerID string, handler func(Message) error) (unsubscribe func(), err error)
ConsumeOne(consumerID string, timeout time.Duration) (Message, error)
// Ack/Nack (only meaningful in AckManual)
Ack(consumerID string, msgID string) error
Nack(consumerID string, msgID string, requeue bool) error
DroppedCount() uint64
// Observability & lifecycle
PendingCount() int
Close() error
}
// QueueOption allows tuning queue behavior.
type QueueOption func(*queue)
// WithBuffer sets the delivery buffer size (ready channel capacity).
func WithBuffer(n int) QueueOption {
return func(q *queue) { q.readyBuf = n }
}
// WithMaxTimers bounds internal timer slice to help GC (soft limit).
func WithMaxTimers(n int) QueueOption {
return func(q *queue) { q.maxTimers = n }
}
// internal structure tracked for in-flight ownership
type inflight struct {
msg Message
consumerID string
delivered time.Time
}
type queue struct {
name string
ackMode AckMode
ctx context.Context
cancel context.CancelFunc
// ready channel serves messages to both Subscribe workers and ConsumeOne pulls
ready chan Message
readyBuf int
// in-flight map tracks messages delivered but not yet acked (manual) or not yet returned (auto-pull).
muIn sync.Mutex
inMap map[string]inflight // key: msg.ID
// delayed message timers (one-shot). We keep handles for shutdown.
muTimers sync.Mutex
timers []*time.Timer
maxTimers int
droppedCount uint64 // count of dropped messages when full
// subscription workers
muSubs sync.Mutex
subs map[string]*subWorker // key: consumerID
closed bool
closeOnce sync.Once
}
// subWorker binds a consumerID to a delivery goroutine.
type subWorker struct {
consumerID string
stop context.CancelFunc
wg sync.WaitGroup
}
// newQueue constructs a queue instance.
func newQueue(parent context.Context, name string, ackMode AckMode, opts ...QueueOption) *queue {
ctx, cancel := context.WithCancel(parent)
q := &queue{
name: name,
ackMode: ackMode,
ctx: ctx,
cancel: cancel,
readyBuf: 1024,
inMap: make(map[string]inflight),
subs: make(map[string]*subWorker),
maxTimers: 1_000_000, // generous default
}
for _, opt := range opts {
opt(q)
}
q.ready = make(chan Message, q.readyBuf)
return q
}
// Publish enqueues message for immediate delivery.
// If the queue is full, the oldest message will be dropped instead of blocking.
func (q *queue) Publish(msg Message) error {
if msg.ID == "" {
return ErrEmptyID
}
msg.Timestamp = time.Now()
if q.isClosed() {
return ErrClosed
}
select {
case q.ready <- msg:
return nil
default:
// Drop oldest
select {
case <-q.ready:
atomic.AddUint64(&q.droppedCount, 1)
default:
}
// Push new
select {
case q.ready <- msg:
return nil
case <-q.ctx.Done():
return ErrClosed
}
}
}
// PublishWithDelay enqueues message to be delivered after delay.
func (q *queue) PublishWithDelay(msg Message, delay time.Duration) error {
if msg.ID == "" {
return ErrEmptyID
}
if q.isClosed() {
return ErrClosed
}
if delay <= 0 {
return q.Publish(msg)
}
msg.Delay = delay
msg.Timestamp = time.Now()
timer := time.AfterFunc(delay, func() {
// try deliver unless closed
select {
case <-q.ctx.Done():
return
default:
}
_ = q.Publish(msg) // Publish already checks closed; ignore error on shutdown.
})
q.trackTimer(timer)
return nil
}
func (q *queue) trackTimer(t *time.Timer) {
q.muTimers.Lock()
defer q.muTimers.Unlock()
q.timers = append(q.timers, t)
// Soft prune to avoid unbounded slice growth
if len(q.timers) > q.maxTimers {
alive := q.timers[:0]
for _, tm := range q.timers {
if tm != nil {
alive = append(alive, tm)
}
}
q.timers = alive
}
}
// Subscribe starts a competing-consumer worker bound to consumerID.
// In AckManual mode, Subscribe records in-flight ownership before invoking handler.
// In AckAuto mode, after handler returns:
// - nil -> done (no requeue)
// - error-> message re-queued with Retry++.
func (q *queue) Subscribe(consumerID string, handler func(Message) error) (func(), error) {
if q.isClosed() {
return func() {}, ErrClosed
}
ctx, stop := context.WithCancel(q.ctx)
w := &subWorker{consumerID: consumerID, stop: stop}
w.wg.Add(1)
go func() {
defer w.wg.Done()
for {
select {
case <-ctx.Done():
return
case msg, ok := <-q.ready:
if !ok {
return
}
// mark in-flight for manual ack
if q.ackMode == AckManual {
q.markInflight(consumerID, msg)
}
err := handler(msg)
// auto-ack behavior
if q.ackMode == AckAuto {
if err != nil {
// requeue on handler error
msg.Retry++
_ = q.Publish(msg)
}
}
// note: in manual mode user must call Ack/Nack explicitly.
}
}
}()
q.muSubs.Lock()
q.subs[consumerID] = w
q.muSubs.Unlock()
unsub := func() {
w.stop()
w.wg.Wait()
q.muSubs.Lock()
delete(q.subs, consumerID)
q.muSubs.Unlock()
}
return unsub, nil
}
// ConsumeOne pulls a single message (competing consumer) with timeout.
// In AckManual mode, the message is marked in-flight and must be Ack'ed or Nack'ed.
// In AckAuto mode, Ack/Nack are no-op for this msg; delivery completes on return.
func (q *queue) ConsumeOne(consumerID string, timeout time.Duration) (Message, error) {
if q.isClosed() {
return Message{}, ErrClosed
}
var timer <-chan time.Time
if timeout > 0 {
t := time.NewTimer(timeout)
defer t.Stop()
timer = t.C
}
select {
case <-q.ctx.Done():
return Message{}, ErrClosed
case <-timer:
return Message{}, ErrTimeout
case msg := <-q.ready:
if q.ackMode == AckManual {
q.markInflight(consumerID, msg)
}
return msg, nil
}
}
func (q *queue) markInflight(consumerID string, msg Message) {
q.muIn.Lock()
q.inMap[msg.ID] = inflight{msg: msg, consumerID: consumerID, delivered: time.Now()}
q.muIn.Unlock()
}
// Ack marks an in-flight message as done (AckManual only).
func (q *queue) Ack(consumerID, msgID string) error {
if q.ackMode == AckAuto {
return ErrAckModeAuto
}
q.muIn.Lock()
defer q.muIn.Unlock()
inf, ok := q.inMap[msgID]
if !ok {
return ErrMsgNotInFlight
}
if inf.consumerID != consumerID {
return ErrWrongConsumer
}
delete(q.inMap, msgID)
return nil
}
// Nack negatively acknowledges an in-flight message (AckManual only).
// If requeue is true, the message is appended back to the ready queue (Retry++).
func (q *queue) Nack(consumerID, msgID string, requeue bool) error {
if q.ackMode == AckAuto {
return ErrAckModeAuto
}
q.muIn.Lock()
inf, ok := q.inMap[msgID]
if !ok {
q.muIn.Unlock()
return ErrMsgNotInFlight
}
if inf.consumerID != consumerID {
q.muIn.Unlock()
return ErrWrongConsumer
}
delete(q.inMap, msgID)
q.muIn.Unlock()
if requeue {
inf.msg.Retry++
return q.Publish(inf.msg)
}
return nil
}
// PendingCount returns approximate count of not-yet-delivered messages.
// It excludes in-flight. Intended for metrics/rough sizing.
func (q *queue) PendingCount() int {
return len(q.ready)
}
// Close gracefully stops subscriptions, cancels timers, and closes delivery channel.
func (q *queue) Close() error {
q.closeOnce.Do(func() {
q.muSubs.Lock()
for _, w := range q.subs {
w.stop()
}
for _, w := range q.subs {
w.wg.Wait()
}
q.subs = map[string]*subWorker{}
q.muSubs.Unlock()
// stop timers
q.muTimers.Lock()
for _, t := range q.timers {
if t != nil {
t.Stop()
}
}
q.timers = nil
q.muTimers.Unlock()
q.cancel()
q.muSubs.Lock()
q.closed = true
q.muSubs.Unlock()
close(q.ready)
})
return nil
}
func (q *queue) isClosed() bool {
q.muSubs.Lock()
defer q.muSubs.Unlock()
return q.closed
}
// DroppedCount returns the number of messages dropped due to a full queue.
func (q *queue) DroppedCount() uint64 {
return atomic.LoadUint64(&q.droppedCount)
}