An in-process and in-memory PubSub, Broadcast, EventBus or Fanout implementation with type-safe topics implemented with generics. Respects context.
These patterns can be used for a given topic at the same time with multiple instances.
Every recvChan
gets its own channel for reading.
var topic *spread.Topic[int]
recvChan, removeRecvChan, err := topic.GetRecvChannel(20)
for number := range recvChan {
fmt.Printf("Got from channel: %d\n", number)
}
var topic *spread.Topic[int]
topic.HandleAsync(func(_ctx context.Context, number int) {
fmt.Printf("Handling in async handler: %d\n", number)
})
This blocks the topic's progress so better to keep it non-blocking.
var topic *spread.Topic[int]
topic.HandleSync(func(number int) {
fmt.Printf("Handling in sync handler: %d\n", number)
})
- Every topic has a inbound channel with a dedicated goroutine for broadcasting.
- Synchronous handlers in
HandleSync
get executed in this goroutine. - Asynchronous handlers or receiver channels that cannot keep up (with full buffers) get eliminated from the subscribers.
- Publishing is the same as sending to a buffered channel, blocks when full.