-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathbus.go
More file actions
434 lines (398 loc) · 11.5 KB
/
Copy pathbus.go
File metadata and controls
434 lines (398 loc) · 11.5 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
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
package events
import (
"context"
"errors"
"fmt"
"sync"
"github.com/goforj/events/eventscore"
)
// Bus is the root event bus implementation.
// @group Bus
type Bus struct {
*busState
ctx context.Context
}
// busState holds the synchronization and resources shared by every context-bound bus handle.
type busState struct {
driver eventscore.Driver
codec Codec
transport eventscore.DriverAPI
mu sync.RWMutex
handlers map[string][]registeredHandler
transportSubs map[string]*transportTopic
}
// transportTopic tracks one transport subscription generation for a topic.
// A generation prevents callbacks from a closing subscription from reaching
// handlers registered against its replacement.
type transportTopic struct {
generation *transportGeneration
setup *transportSetup
subscription eventscore.Subscription
}
// transportGeneration gives each transport subscription a stable identity.
type transportGeneration struct{}
// transportSetup lets concurrent subscribers share one transport handshake.
type transportSetup struct {
done chan struct{}
err error
}
// New constructs a root bus for the requested driver.
// @group Construction
//
// Example: construct a bus from config
//
// bus, _ := events.New(events.Config{Driver: "sync"})
// fmt.Println(bus.Driver())
// // Output: sync
func New(cfg Config, opts ...Option) (*Bus, error) {
options := options{codec: cfg.Codec}
options.apply(opts)
if options.codec != nil && isNilInterface(options.codec) {
return nil, ErrNilCodec
}
if options.codec == nil {
options.codec = jsonCodec{}
}
if cfg.Transport != nil && isNilInterface(cfg.Transport) {
return nil, ErrNilTransport
}
if cfg.Transport != nil {
cfg.Driver = cfg.Transport.Driver()
}
if cfg.Driver == "" {
cfg.Driver = eventscore.DriverSync
}
if cfg.Transport == nil && cfg.Driver != eventscore.DriverSync && cfg.Driver != eventscore.DriverNull {
return nil, fmt.Errorf("%w: %q", ErrUnsupportedDriver, cfg.Driver)
}
return &Bus{
busState: &busState{
driver: cfg.Driver,
codec: options.codec,
transport: cfg.Transport,
handlers: make(map[string][]registeredHandler),
transportSubs: make(map[string]*transportTopic),
},
}, nil
}
// NewSync constructs the root sync bus.
// @group Construction
//
// Example: construct a sync bus
//
// bus, _ := events.NewSync()
// fmt.Println(bus.Driver())
// // Output: sync
func NewSync(opts ...Option) (*Bus, error) {
return New(Config{Driver: eventscore.DriverSync}, opts...)
}
// NewNull constructs the root null bus.
// @group Construction
//
// Example: construct a null bus
//
// bus, _ := events.NewNull()
// fmt.Println(bus.Driver())
// // Output: null
func NewNull(opts ...Option) (*Bus, error) {
return New(Config{Driver: eventscore.DriverNull}, opts...)
}
// WithContext returns a derived bus handle bound to ctx for subsequent operations.
// @group Bus
func (b *Bus) WithContext(ctx context.Context) API {
clone := *b
clone.ctx = ctx
return &clone
}
// Driver reports the active backend.
// @group Bus
//
// Example: inspect the active backend
//
// bus, _ := events.NewSync()
// fmt.Println(bus.Driver())
// // Output: sync
func (b *Bus) Driver() eventscore.Driver {
return b.driver
}
// context returns the handle context while normalizing nil contexts at the API boundary.
func (b *Bus) context() context.Context {
if b.ctx == nil {
return context.Background()
}
return b.ctx
}
// Ready reports whether the bus is ready.
// @group Bus
//
// Example: check readiness
//
// bus, _ := events.NewSync()
// fmt.Println(bus.Ready() == nil)
// // Output: true
func (b *Bus) Ready() error {
return b.ready(b.context())
}
// ready performs the context-aware readiness check used by derived handles.
func (b *Bus) ready(ctx context.Context) error {
if b.transport != nil {
return b.transport.Ready(ctx)
}
return nil
}
// Publish publishes an event using the background context.
// @group Publish
//
// Example: publish a typed event
//
// type UserCreated struct {
// ID string `json:"id"`
// }
//
// bus, _ := events.NewSync()
// _, _ = bus.Subscribe(func(event UserCreated) {
// fmt.Println(event.ID)
// })
// _ = bus.Publish(UserCreated{ID: "123"})
// // Output: 123
func (b *Bus) Publish(event any) error {
return b.publish(b.context(), event)
}
// publish performs the context-aware publish used by derived handles.
func (b *Bus) publish(ctx context.Context, event any) error {
if b.driver == eventscore.DriverNull {
return nil
}
msg, err := b.buildMessage(event)
if err != nil {
return err
}
if ctx == nil {
ctx = context.Background()
}
if b.transport != nil {
return b.transport.PublishContext(ctx, msg)
}
return b.dispatchMessage(ctx, msg)
}
// dispatchMessage delivers a local message to the current topic handlers.
func (b *Bus) dispatchMessage(ctx context.Context, msg eventscore.Message) error {
b.mu.RLock()
handlers := append([]registeredHandler(nil), b.handlers[msg.Topic]...)
b.mu.RUnlock()
return b.dispatchHandlers(ctx, msg, handlers)
}
// dispatchTransportMessage ignores callbacks from starting or retired generations.
func (b *Bus) dispatchTransportMessage(ctx context.Context, msg eventscore.Message, generation *transportGeneration) error {
b.mu.RLock()
topic := b.transportSubs[msg.Topic]
if topic == nil || topic.generation != generation || topic.setup != nil {
b.mu.RUnlock()
return nil
}
handlers := make([]registeredHandler, 0, len(b.handlers[msg.Topic]))
for _, handler := range b.handlers[msg.Topic] {
if handler.generation == generation {
handlers = append(handlers, handler)
}
}
b.mu.RUnlock()
return b.dispatchHandlers(ctx, msg, handlers)
}
// dispatchHandlers decodes separately for each handler so pointer and value signatures stay independent.
func (b *Bus) dispatchHandlers(ctx context.Context, msg eventscore.Message, handlers []registeredHandler) error {
for _, handler := range handlers {
value, err := handler.decodePayload(b.codec, msg.Payload)
if err != nil {
return err
}
if err := handler.invoke(ctx, value); err != nil {
return err
}
}
return nil
}
// Subscribe registers a handler using the background context.
// @group Subscribe
//
// Example: subscribe to a typed event
//
// type UserCreated struct {
// ID string `json:"id"`
// }
//
// bus, _ := events.NewSync()
// sub, _ := bus.Subscribe(func(ctx context.Context, event UserCreated) error {
// fmt.Println(event.ID)
// return nil
// })
// defer sub.Close()
// _ = bus.Publish(UserCreated{ID: "123"})
// // Output: 123
func (b *Bus) Subscribe(handler any) (Subscription, error) {
return b.subscribe(b.context(), handler)
}
// subscribe performs the context-aware registration used by derived handles.
func (b *Bus) subscribe(ctx context.Context, handler any) (Subscription, error) {
registered, err := newRegisteredHandler(handler)
if err != nil {
return nil, err
}
if b.driver == eventscore.DriverNull {
return noopSubscription{}, nil
}
if ctx == nil {
ctx = context.Background()
}
if b.transport == nil {
b.mu.Lock()
b.handlers[registered.topic] = append(b.handlers[registered.topic], registered)
b.mu.Unlock()
return &subscription{bus: b, topic: registered.topic, id: registered.id}, nil
}
b.mu.Lock()
topic := b.transportSubs[registered.topic]
if topic == nil {
topic = &transportTopic{
generation: &transportGeneration{},
setup: &transportSetup{done: make(chan struct{})},
}
b.transportSubs[registered.topic] = topic
}
registered.generation = topic.generation
b.handlers[registered.topic] = append(b.handlers[registered.topic], registered)
setup := topic.setup
if setup == nil {
b.mu.Unlock()
return &subscription{bus: b, topic: registered.topic, id: registered.id, generation: registered.generation}, nil
}
owner := len(b.handlers[registered.topic]) == 1
b.mu.Unlock()
if !owner {
return b.waitForTransportSetup(ctx, registered, setup)
}
transportSub, err := b.transport.SubscribeContext(ctx, registered.topic, func(msgCtx context.Context, msg eventscore.Message) error {
return b.dispatchTransportMessage(msgCtx, msg, registered.generation)
})
if err == nil && isNilInterface(transportSub) {
err = ErrNilSubscription
}
b.mu.Lock()
if err != nil {
b.removeGenerationLocked(registered.topic, registered.generation)
delete(b.transportSubs, registered.topic)
setup.err = err
close(setup.done)
b.mu.Unlock()
return nil, err
}
topic.subscription = transportSub
topic.setup = nil
close(setup.done)
b.mu.Unlock()
return &subscription{bus: b, topic: registered.topic, id: registered.id, generation: registered.generation}, nil
}
// waitForTransportSetup waits for the first subscriber's shared transport handshake.
func (b *Bus) waitForTransportSetup(ctx context.Context, registered registeredHandler, setup *transportSetup) (Subscription, error) {
select {
case <-setup.done:
if setup.err != nil {
return nil, setup.err
}
return &subscription{bus: b, topic: registered.topic, id: registered.id, generation: registered.generation}, nil
case <-ctx.Done():
cleanupErr := b.removeHandler(registered.topic, registered.id, registered.generation)
if cleanupErr == nil {
return nil, ctx.Err()
}
return nil, errors.Join(ctx.Err(), cleanupErr)
}
}
// removeGenerationLocked rolls back every handler that joined a failed setup.
func (b *Bus) removeGenerationLocked(topic string, generation *transportGeneration) {
handlers := b.handlers[topic]
kept := handlers[:0]
for _, handler := range handlers {
if handler.generation != generation {
kept = append(kept, handler)
}
}
if len(kept) == 0 {
delete(b.handlers, topic)
return
}
b.handlers[topic] = kept
}
type noopSubscription struct{}
// Close preserves the subscription lifecycle contract for the drop-only bus.
func (noopSubscription) Close() error { return nil }
type subscription struct {
bus *Bus
topic string
id uint64
generation *transportGeneration
once sync.Once
err error
}
// Close removes the handler and releases the shared transport subscription when it is last.
func (s *subscription) Close() error {
s.once.Do(func() {
s.err = s.bus.removeHandler(s.topic, s.id, s.generation)
})
return s.err
}
// removeHandler detaches state under the bus lock and closes external resources after unlocking.
func (b *Bus) removeHandler(topic string, id uint64, generation *transportGeneration) error {
b.mu.Lock()
handlers := b.handlers[topic]
removed := false
for i := range handlers {
if handlers[i].id == id {
handlers = append(handlers[:i], handlers[i+1:]...)
removed = true
break
}
}
if !removed {
b.mu.Unlock()
return nil
}
if len(handlers) == 0 {
delete(b.handlers, topic)
} else {
b.handlers[topic] = handlers
}
var transportSub eventscore.Subscription
remainingGeneration := false
for _, handler := range handlers {
if handler.generation == generation {
remainingGeneration = true
break
}
}
transportTopic := b.transportSubs[topic]
if !remainingGeneration && transportTopic != nil && transportTopic.generation == generation {
transportSub = transportTopic.subscription
delete(b.transportSubs, topic)
}
b.mu.Unlock()
if transportSub != nil {
return transportSub.Close()
}
return nil
}
// buildMessage resolves routing before serialization so invalid events never reach a codec.
func (b *Bus) buildMessage(event any) (eventscore.Message, error) {
topic, _, err := resolveTopic(event)
if err != nil {
return eventscore.Message{}, err
}
payload, err := b.codec.Marshal(event)
if err != nil {
return eventscore.Message{}, err
}
return eventscore.Message{
Topic: topic,
Payload: payload,
}, nil
}