pubsub

package
v1.12.0 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Mar 8, 2026 License: MIT Imports: 2 Imported by: 0

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type Broker

type Broker[T any] struct {
	// contains filtered or unexported fields
}

Broker manages the publisher/subscriber pattern in a thread-safe, generic way.

func NewBroker

func NewBroker[T any]() *Broker[T]

NewBroker creates a new Broker with default settings.

func NewBrokerWithOptions

func NewBrokerWithOptions[T any](bufferSize int) *Broker[T]

NewBrokerWithOptions creates a new Broker with custom buffer size.

func (*Broker[T]) GetSubscriberCount

func (b *Broker[T]) GetSubscriberCount() int

GetSubscriberCount returns the current number of active subscribers.

func (*Broker[T]) Publish

func (b *Broker[T]) Publish(t EventType, payload T)

Publish broadcasts an event to all subscribers. It is non-blocking: if a subscriber is slow/full, they miss the message.

func (*Broker[T]) Shutdown

func (b *Broker[T]) Shutdown()

Shutdown closes the broker and all subscriber channels.

func (*Broker[T]) Subscribe

func (b *Broker[T]) Subscribe() (<-chan Event[T], func())

Subscribe adds a new subscriber and returns the channel + an unsubscribe function. The caller is responsible for consuming the channel.

func (*Broker[T]) SubscribeWithContext added in v1.7.0

func (b *Broker[T]) SubscribeWithContext(ctx context.Context) <-chan Event[T]

SubscribeWithContext is a helper that unsubscribes automatically when context dies.

type Event

type Event[T any] struct {
	Type    EventType
	Payload T
}

Event represents an event in the lifecycle of a resource

type EventType

type EventType string

EventType identifies the type of event

const (
	CreatedEvent EventType = "created"
	UpdatedEvent EventType = "updated"
	DeletedEvent EventType = "deleted"
)

type Publisher

type Publisher[T any] interface {
	Publish(EventType, T)
}

type Subscriber added in v1.7.0

type Subscriber[T any] interface {
	SubscribeWithContext(context.Context) <-chan Event[T]
}

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL