Help us shape the future of Go! Take the 2026 Go Developer Survey

pubsub

package
v0.20.1 Latest Latest
Warning

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

Go to latest
Published: Dec 5, 2025 License: MIT Imports: 5 Imported by: 3

Documentation

Overview

Package pubsub provides a simple publish/subscribe mechanism.

It supports both synchronous and asynchronous subscriptions.

Example
// Create a new topic.
t := New[int]()

// Subscribe to changes.
changes := t.Subscribe(nil)
go func() {
	for change := range changes {
		fmt.Println("change:", change)
	}
}()

// Publish a value.
t.Publish(1)

// Publish a value and wait for it to be received.
t.Publish(2)

time.Sleep(time.Millisecond * 100)
Output:
change: 1
change: 2

Index

Examples

Constants

View Source
const AckTimeout = time.Second * 30

AckTimeout is the time to wait for an ack before panicking.

This is a last-ditch effort to avoid deadlocks.

View Source
const PublishTimeout = time.Second * 10

PublishTimeout is the time to wait for a publish before panicking.

Variables

This section is empty.

Functions

This section is empty.

Types

type Message added in v0.12.0

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

Message is a message that must be acknowledge by the receiver.

func (*Message[T]) Ack added in v0.12.0

func (a *Message[T]) Ack()

func (*Message[T]) Nack added in v0.13.0

func (a *Message[T]) Nack(err error)

type Topic

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

func New

func New[T any]() *Topic[T]

New creates a new topic that can be used to publish and subscribe to messages.

func (*Topic[T]) Close

func (s *Topic[T]) Close() error

Close the topic, blocking until all subscribers have been closed.

func (*Topic[T]) Publish

func (s *Topic[T]) Publish(t T)

Publish a message to the topic.

func (*Topic[T]) PublishSync added in v0.12.0

func (s *Topic[T]) PublishSync(t T) error

PublishSync publishes a message to the topic and blocks until all subscriber channels have acked the message.

func (*Topic[T]) Subscribe

func (s *Topic[T]) Subscribe(c chan T) chan T

Subscribe a channel to the topic.

The channel will be closed when the topic is closed.

If "c" is nil a new channel of size 16 will be created.

func (*Topic[T]) SubscribeSync added in v0.12.0

func (s *Topic[T]) SubscribeSync(c chan Message[T]) chan Message[T]

SubscribeSync creates a synchronous subscription to the topic.

Each message must be acked by the subscriber.

A synchronous publish will block until the message has been acked by all subscribers.

The channel will be closed when the topic is closed. If "c" is nil a new channel of size 16 will be created.

func (*Topic[T]) Unsubscribe

func (s *Topic[T]) Unsubscribe(c chan T)

Unsubscribe a channel from the topic, closing the channel.

func (*Topic[T]) UnsubscribeSync added in v0.12.0

func (s *Topic[T]) UnsubscribeSync(c chan Message[T])

UnsubscribeSync a synchronised subscription from the topic, closing the channel.

func (*Topic[T]) Wait added in v0.11.0

func (s *Topic[T]) Wait() chan struct{}

Wait that returns a channel that will be closed when the Topic is closed.

Jump to

Keyboard shortcuts

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