https://github.com/egemengol/spread
An in-process and in-memory PubSub, Broadcast, EventBus or Fanout implementation with type-safe topics implemented with generics. Respects context.
https://github.com/egemengol/spread
event-bus event-sourcing eventbus eventsourcing fanout generics in-process pub-sub pubsub type-safe
Last synced: 5 months ago
JSON representation
An in-process and in-memory PubSub, Broadcast, EventBus or Fanout implementation with type-safe topics implemented with generics. Respects context.
- Host: GitHub
- URL: https://github.com/egemengol/spread
- Owner: egemengol
- License: apache-2.0
- Created: 2024-02-25T17:49:15.000Z (over 2 years ago)
- Default Branch: main
- Last Pushed: 2024-03-08T10:46:11.000Z (over 2 years ago)
- Last Synced: 2025-08-09T21:43:22.245Z (10 months ago)
- Topics: event-bus, event-sourcing, eventbus, eventsourcing, fanout, generics, in-process, pub-sub, pubsub, type-safe
- Language: Go
- Homepage:
- Size: 20.5 KB
- Stars: 9
- Watchers: 1
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
README
## Spread
[](http://godoc.org/github.com/egemengol/spread)
[](https://github.com/egemengol/spread)
An in-process and in-memory PubSub, Broadcast, EventBus or Fanout implementation with type-safe topics implemented with generics. Respects context.
### Subscriber Patterns
These patterns can be used for a given topic at the same time with multiple instances.
#### Channel-based
Every `recvChan` gets its own channel for reading.
```golang
var topic *spread.Topic[int]
recvChan, removeRecvChan, err := topic.GetRecvChannel(20)
for number := range recvChan {
fmt.Printf("Got from channel: %d\n", number)
}
```
#### Asynchronous
```golang
var topic *spread.Topic[int]
topic.HandleAsync(func(_ctx context.Context, number int) {
fmt.Printf("Handling in async handler: %d\n", number)
})
```
#### Synchronous
This blocks the topic's progress so better to keep it non-blocking.
```golang
var topic *spread.Topic[int]
topic.HandleSync(func(number int) {
fmt.Printf("Handling in sync handler: %d\n", number)
})
```
### Performance Characteristics
- 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.