-
Notifications
You must be signed in to change notification settings - Fork 0
/
bus.go
369 lines (290 loc) · 7.25 KB
/
bus.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
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
357
358
359
360
361
362
363
364
365
366
367
368
369
package bus
import (
"context"
"encoding/json"
"errors"
"fmt"
"io"
"iter"
"strings"
"time"
)
var (
Version = "dev"
GitCommit = ""
)
//
// Event
//
type Event struct {
Id string `json:"id"`
Subject string `json:"subject"`
ResponseSubject string `json:"response_subject"`
Payload json.RawMessage `json:"payload"`
CreatedAt time.Time `json:"created_at"`
Index int64 `json:"index"`
// for internal use
consumerId string
acker Acker
putter Putter
}
func (e *Event) encode(w io.Writer) error {
return json.NewEncoder(w).Encode(e)
}
func (e *Event) decode(r io.Reader) error {
return json.NewDecoder(r).Decode(e)
}
func (e *Event) validate() error {
// subject is required
if e.Subject == "" {
return errors.New("subject is required")
}
// subject must be in a form of "a.b.c"
if strings.Contains(e.Subject, "*") || strings.Contains(e.Subject, ">") {
return errors.New("subject should not have * or >")
}
// simple validation for response subject
if e.ResponseSubject != "" {
if strings.Contains(e.ResponseSubject, "*") || strings.Contains(e.ResponseSubject, ">") {
return errors.New("response subject should not have * or >")
}
}
return nil
}
func (e *Event) Ack(ctx context.Context, opts ...AckOpt) error {
if err := e.acker.Ack(ctx, e.consumerId, e.Id); err != nil {
return fmt.Errorf("failed to ack event: %w", err)
}
if e.ResponseSubject == "" {
return nil
}
var putOpts = []PutOpt{
WithSubject(e.ResponseSubject),
}
for _, opt := range opts {
if o, ok := opt.(PutOpt); ok {
putOpts = append(putOpts, o)
}
}
if err := e.putter.Put(ctx, putOpts...).Error(); err != nil {
return fmt.Errorf("failed to send response: %w", err)
}
return nil
}
const (
AckManual = "manual" // client should ack the event
AckNone = "none" // no need to ack and server push the event to the client as fast as possible
)
const (
StartOldest = "oldest"
StartNewest = "newest"
)
const (
DefaultAck = AckNone
DefaultStart = StartNewest
DefaultRedelivery = 5 * time.Second
)
//
// Putter
//
type Response struct {
err error
Id string
Index int64
CreatedAt time.Time
Payload json.RawMessage
}
func (s *Response) String() string {
var sb strings.Builder
sb.WriteString("id: ")
sb.WriteString(s.Id)
if s.Index != -1 {
sb.WriteString(", index: ")
sb.WriteString(fmt.Sprintf("%d", s.Index))
}
sb.WriteString(", created_at: ")
sb.WriteString(s.CreatedAt.Format(time.RFC3339Nano))
return sb.String()
}
func (r *Response) Error() error {
if r.err != nil {
return r.err
}
if len(r.Payload) == 0 || r.Payload[0] == '{' || r.Payload[0] == '[' {
return nil
}
return fmt.Errorf("%s", r.Payload)
}
type putOpt struct {
event Event
confirmCount int
}
type PutOpt interface {
configurePut(*putOpt) error
}
type PutOptFunc func(*putOpt) error
func (f PutOptFunc) configurePut(p *putOpt) error {
return f(p)
}
type Putter interface {
Put(ctx context.Context, opts ...PutOpt) *Response
}
//
// Getter
//
type getOpt struct {
subject string
ackStrategy string
redelivery time.Duration
start string
metaFn func(map[string]string)
}
// GetOpt is an interface that can be used to configure the Get operation
type GetOpt interface {
configureGet(*getOpt) error
}
type GetOptFunc func(*getOpt) error
func (f GetOptFunc) configureGet(g *getOpt) error {
return f(g)
}
// Getter is an interface that can be used to get events from the bus
type Getter interface {
Get(ctx context.Context, opts ...GetOpt) iter.Seq2[*Event, error]
}
//
// Acker
//
type ackOpt struct {
payload json.RawMessage
}
// AckOpt is an interface that can be used to configure the Ack operation
type AckOpt interface {
configureAck(*ackOpt) error
}
// Acker is an interface that can be used to acknowledge the event
type Acker interface {
Ack(ctx context.Context, consumerId string, eventId string) error
}
//
// Options
// options are utility functions which can be used to configure the Putter and Getter
// Subject
type subjectOpt string
var _ PutOpt = (*subjectOpt)(nil)
var _ GetOpt = (*subjectOpt)(nil)
func (s subjectOpt) configurePut(p *putOpt) error {
if p.event.Subject != "" {
return errors.New("subject already set")
}
p.event.Subject = string(s)
// should not have * or >
if strings.Contains(string(s), "*") || strings.Contains(string(s), ">") {
return errors.New("subject should not have * or >")
}
return nil
}
func (s subjectOpt) configureGet(g *getOpt) error {
if g.subject != "" {
return errors.New("subject already set")
}
// should not starts with * or >
if strings.HasPrefix(string(s), "*") || strings.HasPrefix(string(s), ">") {
return errors.New("subject should not starts with * or >")
}
// should not have anything after >
if strings.Contains(string(s), ">") && !strings.HasSuffix(string(s), ">") {
return errors.New("subject should not have anything after >")
}
g.subject = string(s)
return nil
}
// WithSubject sets the subject of the event and consumer
func WithSubject(subject string) subjectOpt {
return subjectOpt(subject)
}
func WithStartFrom(start string) GetOpt {
return GetOptFunc(func(g *getOpt) error {
if start != StartOldest && start != StartNewest && !strings.HasPrefix(start, "e_") {
return errors.New("invalid start from")
}
g.start = start
return nil
})
}
func WithDelivery(duration time.Duration) GetOpt {
return GetOptFunc(func(g *getOpt) error {
if duration < 0 {
return errors.New("delivery duration should be greater than 0")
}
g.redelivery = duration
return nil
})
}
func WithAckStrategy(strategy string) GetOpt {
return GetOptFunc(func(g *getOpt) error {
if strategy != AckManual && strategy != AckNone {
return errors.New("invalid ack strategy")
}
g.ackStrategy = strategy
return nil
})
}
func WithExtractMeta(fn func(map[string]string)) GetOpt {
return GetOptFunc(func(g *getOpt) error {
if g.metaFn != nil {
return errors.New("meta function already set")
}
g.metaFn = fn
return nil
})
}
func WithConfirm(n int) PutOpt {
return PutOptFunc(func(p *putOpt) error {
if n < 0 {
return errors.New("confirm count should be greater than 0")
}
if p.event.ResponseSubject != "" {
return errors.New("response subject already set")
}
p.confirmCount = n
p.event.ResponseSubject = newInboxSubject()
return nil
})
}
func WithRequestReply() PutOpt {
return PutOptFunc(func(p *putOpt) error {
if p.confirmCount != 0 {
return errors.New("confirm count already set")
}
p.event.ResponseSubject = newInboxSubject()
return nil
})
}
// Payload
type dataOpt struct{ value any }
func (d *dataOpt) configurePut(p *putOpt) error {
if p.event.Payload != nil {
return errors.New("event's payload already set")
}
// how to encode error value ?? {error: "error message"} or simple string?????
data, err := json.Marshal(d.value)
if err != nil {
return err
}
p.event.Payload = json.RawMessage(data)
return nil
}
func (d *dataOpt) configureAck(a *ackOpt) error {
if a.payload != nil {
return errors.New("payload already set")
}
data, err := json.Marshal(d.value)
if err != nil {
return err
}
a.payload = json.RawMessage(data)
return nil
}
func WithData(data any) *dataOpt {
return &dataOpt{data}
}