Skip to content

Latest commit

 

History

History
49 lines (35 loc) · 1.56 KB

README.md

File metadata and controls

49 lines (35 loc) · 1.56 KB

Spread

GoDoc GitHub

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.

var topic *spread.Topic[int]

recvChan, removeRecvChan, err := topic.GetRecvChannel(20)
for number := range recvChan {
    fmt.Printf("Got from channel: %d\n", number)
}

Asynchronous

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.

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.