-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathapp.go
More file actions
232 lines (206 loc) · 7.17 KB
/
app.go
File metadata and controls
232 lines (206 loc) · 7.17 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
package babelqueue
import (
"context"
"errors"
"fmt"
"time"
)
// Handler processes one decoded message. Returning an error triggers the retry /
// dead-letter path; returning nil acknowledges the message.
type Handler func(ctx context.Context, env Envelope) error
// App is the optional BabelQueue runtime: it produces and consumes polyglot
// messages over a [Transport]. Routing is by URN; the wire format is the canonical
// envelope (via the same core codec), so it interoperates with the PHP/Laravel,
// Python, ... SDKs. Retry uses the top-level attempts counter; failures past
// MaxAttempts go to a dead-letter queue when enabled.
//
// The core codec ([Make]/[Encode]/[Decode]) has zero dependencies; the App adds
// no dependencies either (broker drivers live in separate modules). An App is safe
// for concurrent Publish calls; run Consume from a single goroutine per queue.
type App struct {
transport Transport
queue string
onUnknownURN string
maxAttempts int
deadLetter bool
deadLetterQueue string
deadLetterSuffix string
pollTimeout time.Duration
handlers map[string]Handler
}
// AppOption customizes NewApp.
type AppOption func(*App)
// WithDefaultQueue sets the queue used by Publish/Consume when none is given
// (default "default").
func WithDefaultQueue(queue string) AppOption { return func(a *App) { a.queue = queue } }
// WithMaxAttempts sets how many delivery attempts a message gets before it is
// dead-lettered or dropped (default 3).
func WithMaxAttempts(n int) AppOption { return func(a *App) { a.maxAttempts = n } }
// WithUnknownURNStrategy sets what happens to a message whose URN has no handler:
// one of [StrategyFail], [StrategyDelete], [StrategyRelease], [StrategyDeadLetter].
func WithUnknownURNStrategy(strategy string) AppOption {
return func(a *App) { a.onUnknownURN = strategy }
}
// WithDeadLetter enables routing exhausted/failed messages to a dead-letter queue.
func WithDeadLetter(enabled bool) AppOption { return func(a *App) { a.deadLetter = enabled } }
// WithDeadLetterQueue overrides the dead-letter queue name (default: the source
// queue plus the dead-letter suffix).
func WithDeadLetterQueue(queue string) AppOption {
return func(a *App) { a.deadLetterQueue = queue }
}
// WithDeadLetterSuffix sets the suffix appended to the source queue to derive the
// dead-letter queue name (default ".dlq").
func WithDeadLetterSuffix(suffix string) AppOption {
return func(a *App) { a.deadLetterSuffix = suffix }
}
// WithPollTimeout sets how long Pop blocks waiting for a message each iteration
// (default 1s). Ignored by transports that do not block.
func WithPollTimeout(d time.Duration) AppOption { return func(a *App) { a.pollTimeout = d } }
// NewApp builds a runtime over the given transport.
func NewApp(transport Transport, opts ...AppOption) *App {
app := &App{
transport: transport,
queue: "default",
onUnknownURN: StrategyFail,
maxAttempts: 3,
deadLetterSuffix: ".dlq",
pollTimeout: time.Second,
handlers: make(map[string]Handler),
}
for _, o := range opts {
o(app)
}
return app
}
// Handle registers handler as the consumer for urn (the last registration wins).
func (a *App) Handle(urn string, handler Handler) {
a.handlers[urn] = handler
}
// Publish builds the canonical envelope for (urn, data) and publishes it. It
// returns the message id (meta.id). Pass envelope options such as [WithQueue] or
// [WithTraceID] to override the target queue or continue a trace.
func (a *App) Publish(ctx context.Context, urn string, data map[string]any, opts ...Option) (string, error) {
env, err := Make(urn, data, append([]Option{WithQueue(a.queue)}, opts...)...)
if err != nil {
return "", err
}
body, err := env.Encode()
if err != nil {
return "", err
}
if err := a.transport.Publish(ctx, env.Meta.Queue, string(body)); err != nil {
return "", err
}
return env.Meta.ID, nil
}
// Consume processes messages from the default queue (or the optional override)
// until ctx is cancelled. It blocks; run it in its own goroutine. A single bad
// message never stops the loop — it is retried or dead-lettered.
func (a *App) Consume(ctx context.Context, queue ...string) error {
target := a.queue
if len(queue) > 0 && queue[0] != "" {
target = queue[0]
}
for {
if err := ctx.Err(); err != nil {
return err
}
msg, err := a.transport.Pop(ctx, target, a.pollTimeout)
if err != nil {
return err
}
if msg == nil {
continue
}
a.dispatch(ctx, msg)
}
}
// Run is an alias for Consume on the default queue.
func (a *App) Run(ctx context.Context) error { return a.Consume(ctx) }
// Drain processes up to max messages from queue and returns the count, stopping
// early when the queue is empty. With max <= 0 it drains until empty. Useful for
// tests and one-shot workers.
func (a *App) Drain(ctx context.Context, queue string, max int) (int, error) {
if queue == "" {
queue = a.queue
}
processed := 0
for max <= 0 || processed < max {
if err := ctx.Err(); err != nil {
return processed, err
}
msg, err := a.transport.Pop(ctx, queue, a.pollTimeout)
if err != nil {
return processed, err
}
if msg == nil {
break
}
a.dispatch(ctx, msg)
processed++
}
return processed, nil
}
func (a *App) dispatch(ctx context.Context, msg *ReceivedMessage) {
env, _ := Decode([]byte(msg.Body))
urn := env.URN()
handler, ok := a.handlers[urn]
if !ok || urn == "" {
a.routeUnknown(ctx, urn, msg, env)
return
}
if err := handler(ctx, env); err != nil {
a.retryOrDeadLetter(ctx, msg, env, err)
return
}
_ = a.transport.Ack(ctx, msg)
}
func (a *App) routeUnknown(ctx context.Context, urn string, msg *ReceivedMessage, env Envelope) {
switch a.onUnknownURN {
case StrategyDelete:
_ = a.transport.Ack(ctx, msg)
case StrategyRelease:
_ = a.transport.Publish(ctx, msg.Queue, msg.Body)
_ = a.transport.Ack(ctx, msg)
case StrategyDeadLetter:
a.deadLetterMessage(ctx, msg, env, "unknown_urn", nil)
default: // StrategyFail — surfaced through the retry/dead-letter path.
a.retryOrDeadLetter(ctx, msg, env, fmt.Errorf("%w: %q", ErrUnknownURN, urn))
}
}
func (a *App) retryOrDeadLetter(ctx context.Context, msg *ReceivedMessage, env Envelope, cause error) {
env.Attempts++
if env.Attempts < a.maxAttempts {
if body, err := env.Encode(); err == nil {
_ = a.transport.Publish(ctx, msg.Queue, string(body))
}
_ = a.transport.Ack(ctx, msg)
return
}
if a.deadLetter {
reason := "failed"
if errors.Is(cause, ErrUnknownURN) {
reason = "unknown_urn"
}
a.deadLetterMessage(ctx, msg, env, reason, cause)
return
}
// Retries exhausted and no dead-letter configured — drop it (ack so it leaves
// the queue).
_ = a.transport.Ack(ctx, msg)
}
func (a *App) deadLetterMessage(ctx context.Context, msg *ReceivedMessage, env Envelope, reason string, cause error) {
originalQueue := msg.Queue
if env.Meta.Queue != "" {
originalQueue = env.Meta.Queue
}
annotated := Annotate(env, reason, originalQueue, env.Attempts, cause)
target := a.deadLetterQueue
if target == "" {
target = msg.Queue + a.deadLetterSuffix
}
if body, err := annotated.Encode(); err == nil {
_ = a.transport.Publish(ctx, target, string(body))
}
_ = a.transport.Ack(ctx, msg)
}