pubsub

package module
v0.0.0-...-59bceb0 Latest Latest
Warning

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

Go to latest
Published: Apr 28, 2022 License: MIT Imports: 3 Imported by: 3

README

pubsub

A Go package implementing a topic-based publish-subscribe system using channels.

Usage

Get the package:

$ go get github.com/sauerbraten/pubsub

Import the package:

import (
	"github.com/sauerbraten/pubsub"
)

Subscribers receive updates on channels provided to them when they subscribe to a topic. Topics are automatically created when you subscribe to them and they do not exist yet. In that case, a Publisher type is returned as well, providing methods to publish updates on the new topic. Topics are removed when a subscriber unsubscribes from it and there are no other subscribers left. Publishers include a stop channel from which reading only succeeds after the topic was removed.

Documentation

Full documentation at godoc.org.

Example

See example/main.go.

Documentation

Overview

Package pubsub implements a topic-based publish-subscribe system using channels.

Subscribers receive updates on channels provided to them when they subscribe to a topic. Topics are automatically created when you subscribe to them and they do not exist yet. In that case, a Publisher type is returned as well, providing methods to publish updates on the new topic. Topics are removed when a subscriber unsubscribes from it and there are no other subscribers left. Publishers include a stop channel from which reading only succeeds after the topic was removed.

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type Broker

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

Broker handles subscribing and unsubcribing to topics.

func NewBroker

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

NewBroker creates a broker and starts a goroutine handling message distribution.

func (*Broker[U]) Subscribe

func (b *Broker[U]) Subscribe(topic string) (updates chan U, newPublisher *Publisher[U])

Subscribe returns a new channel on which to receive updates on a certain topic. Subscribe makes sure the topic exists by creating it if neccessary. When a new topic was created, a corresponding publisher is returned, otherwise newPublisher is nil.

func (*Broker[U]) Unsubscribe

func (b *Broker[U]) Unsubscribe(updates chan U, topic string) error

Unsubscribe removes the specified channel from the topic, meaning there will be no more messages sent to updates. Unsubscribe will close updates.

type Publisher

type Publisher[U any] struct {
	// Stop will be closed by the broker when a subscriber unsubscribes and the topic is removed because there
	// are no subscribers left. This means you know to stop publishing updates when reading from Stop succeeds.
	// In that case, you should call Publisher.Close().
	Stop <-chan struct{}
	// contains filtered or unexported fields
}

Publisher provides methods to send updates to all subscribers of a certain topic.

func (*Publisher[U]) Close

func (p *Publisher[U]) Close()

Close tells the broker there will be no more updates coming from p. Calling Publish() after Close() returns immediately. Calling Close() makes the broker unsubscribe all subscribers and telling them updates on the topic have ended.

func (*Publisher[U]) Publish

func (p *Publisher[U]) Publish(update U)

Publish notifies p's broker that there is an update on p's topic and blocks until the broker received the notification. Publish then blocks until the broker received the update. Calling Publish() after Close() returns immediately. Use p's Stop channel to know when the broker stopped listening.

func (*Publisher[U]) Topic

func (p *Publisher[U]) Topic() string

Topic returns the topic this publisher is meant to publish updates on.

Directories

Path Synopsis

Jump to

Keyboard shortcuts

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