Documentation
¶
Index ¶
Constants ¶
This section is empty.
Variables ¶
var ErrTopicClosed = fmt.Errorf("topic is closed")
Functions ¶
Types ¶
type AsyncTopic ¶
type AsyncTopic[T any] struct { // contains filtered or unexported fields }
AsyncTopic allows any message T to be broadcast to subscribers. Publishing as well as subscribing happens asynchronously (as non-blocking as possible). Closing the topic guarantees that published message will be delivered and no further messages nor subscribers will be accepted. Delivery order is NOT guaranteed.
func NewAsyncTopic ¶
func NewAsyncTopic[T any](opts ...TopicOption) *AsyncTopic[T]
NewAsyncTopic creates an AsyncTopic.
func (*AsyncTopic[T]) Close ¶
func (t *AsyncTopic[T]) Close()
Close terminates background go routines and prevents further publishing and subscribing. All published messages are garanteed to be delivered once Close returns. This is idempotent.
func (*AsyncTopic[T]) Publish ¶
func (t *AsyncTopic[T]) Publish(msg T) error
Publish broadcasts a msg to all subscribers asynchronously.
func (*AsyncTopic[T]) Subscribe ¶
func (t *AsyncTopic[T]) Subscribe(fn Subscriber[T]) error
Subscribe registers a Subscriber func asynchronously.
type Publishable ¶
type Subscribable ¶
type Subscribable[T any] interface { Subscribe(Subscriber[T]) error }
type Subscriber ¶
Subscriber is a func that processes a message and returns true if it should continue processing more messages.
func Buffered ¶
func Buffered[T any](subscriber Subscriber[T]) Subscriber[T]
Buffered returns a subscriber that buffers messages if they can't be delivered immediately. There is no artificial limit to how many items can be buffered. This is bounded only by available memory. This is useful if message publishing is surge prone and message processing is slow or unpredictable (for example: subscriber makes network request). IMPORTANT: messages are considered delivered even it they are still in the buffer which means that buffered subscribers are NOT COVERED by the publishing promise. Message average processing rate must still be higher than the average message publishing rate otherwise it will eventually lead to memory issues. You will need to find a better strategy to deal with such scenario.
func Forever ¶
func Forever[T any](fn func(T)) Subscriber[T]
Forever wraps a subscriber that will never stop consuming messages. This helps avoiding subscribers that always return TRUE.
func NoOp ¶
func NoOp[T any]() Subscriber[T]
NoOp creates a subscriber that does absolutely nothing forever. This is mostly useful for testing.
func Once ¶
func Once[T any](fn func(T)) Subscriber[T]
Once wraps a subscriber that will consume only one message. This helps avoiding subscribers that always return FALSE.
type SyncTopic ¶
type SyncTopic[T any] struct { // contains filtered or unexported fields }
SyncTopic is the simplest and most naive topic. It allows any message T to be broadcast to subscribers. Publishing and Subscribing happens synchronously (block).
func NewSyncTopic ¶
func NewSyncTopic[T any](opts ...TopicOption) *SyncTopic[T]
NewSyncTopic creates a SyncTopic with the specified options.
func (*SyncTopic[T]) Close ¶
func (t *SyncTopic[T]) Close()
Close will prevent further publishing and subscribing.
func (*SyncTopic[T]) Subscribe ¶
func (t *SyncTopic[T]) Subscribe(fn Subscriber[T]) error
Subscribe adds a Subscriber func that will consume future published messages.
type Topic ¶
type Topic[T any] interface { Publishable[T] Subscribable[T] }
Topic is just a convenience interface you can expect all topics to implement.
type TopicOption ¶
type TopicOption func(*TopicOptions)
func WithOnClose ¶
func WithOnClose(fn func()) TopicOption
func WithOnSubscribe ¶
func WithOnSubscribe(fn func()) TopicOption
type TopicOptions ¶
type TopicOptions struct {
// contains filtered or unexported fields
}
TopicOptions holds common options for topics.