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 (*Broker[U]) Subscribe ¶
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 ¶
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.