pubsub

package
v0.0.0-...-8ed97ff Latest Latest
Warning

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

Go to latest
Published: Nov 26, 2024 License: ISC Imports: 3 Imported by: 0

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func Send

func Send[T any](ch chan<- T, events ...T)

Send sends events to a channel.

Types

type ConcurrentQueue

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

ConcurrentQueue is a concurrent-safe FIFO queue with unbounded capacity. Clients interact with the queue by pushing items into the in channel and popping items from the out channel. There is a goroutine that manages moving items from the in channel to the out channel in the correct order that must be started by calling Start().

func NewConcurrentQueue

func NewConcurrentQueue[T any]() *ConcurrentQueue[T]

NewConcurrentQueue constructs a ConcurrentQueue.

func (*ConcurrentQueue[T]) Close

func (cq *ConcurrentQueue[T]) Close()

Close closes the incoming channel.

func (*ConcurrentQueue[T]) In

func (cq *ConcurrentQueue[T]) In() chan<- T

In returns a channel that can be used to push new items into the queue.

func (*ConcurrentQueue[T]) Out

func (cq *ConcurrentQueue[T]) Out() <-chan T

Out returns a channel that can be used to pop items from the queue.

func (*ConcurrentQueue[T]) Start

func (cq *ConcurrentQueue[T]) Start()

Start begins a goroutine that manages moving items from the in channel to the out channel. The queue tries to move items directly to the out channel minimize overhead, but if the out channel is full it pushes items to an overflow queue. This must be called before using the queue.

func (*ConcurrentQueue[T]) Stop

func (cq *ConcurrentQueue[T]) Stop()

Stop interrupts the background goroutine immediately and waits for it to return.

type Publisher

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

Publisher allows you to publish events to multiple subscribers.

func NewPublisher

func NewPublisher[T any]() *Publisher[T]

NewPublisher creates a new publisher.

func (*Publisher[T]) Publish

func (p *Publisher[T]) Publish(events ...T)

Publish publishes an event to all subscribers. It will block until all subscribers have received the event or the context has been cancelled.

func (*Publisher[T]) Subscribe

func (p *Publisher[T]) Subscribe(cq *ConcurrentQueue[T])

Subscribe subscribes to the publisher.

func (*Publisher[T]) Subscribers

func (p *Publisher[T]) Subscribers() []*ConcurrentQueue[T]

Subscribers returns a list of all subscribers.

func (*Publisher[T]) Unsubscribe

func (p *Publisher[T]) Unsubscribe(cq *ConcurrentQueue[T])

Unsubscribe unsubscribes from the publisher. Any pending sends to the channel will be cancelled.

Jump to

Keyboard shortcuts

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