pubsub

package
v0.8.0 Latest Latest
Warning

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

Go to latest
Published: Oct 4, 2026 License: MIT Imports: 10 Imported by: 0 modules, 0 packages

The first number is the count of modules that contain a package that imports this one. The module count is a better indicator of popularity than the package count. A module with 100 importing packages is counted only once instead of 100 times, reflecting the fact that the choice to use the package was likely made only once, not 100 times.
A '?' means that the value hasn't been computed yet.

Documentation

Overview

Package pubsub is publish/subscribe on grpcproc processes. A topic is a process: it keeps its subscribers and the last few events, and sends each event it is given to every subscriber, on any node. It uses only grpcproc's public API, so it can be ignored or replaced.

A topic owned by the process that publishes to it ends when that process does, and its subscribers receive the reason in a Down:

placed, err := pubsub.SpawnOwned[*orderspb.Placed](p, pubsub.Config{Buffer: 100}, grpcproc.WithName("placed"))
…
err = placed.Publish(ctx, p, &orderspb.Placed{Id: id})

A process on any node subscribes by the topic's name:

placed := pubsub.Named[*orderspb.Placed]("orders", "placed")
sub, err := placed.Subscribe(ctx, p)
for {
	m, err := p.Receive()
	…	// an event is a message from sub.From; a Down with sub.Ref, the end of them
}

A process subscribes to a topic of another node through a relay on its own node, which the first such subscriber starts and the last one's leaving ends. The topic sends each event once to each node, however many subscribers the node has.

Events are delivered as grpcproc delivers any message: at most once, and in order from one sender, so in the order the topic received them.

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type Config

type Config struct {
	// Buffer is how many of the last events the topic keeps. A new
	// subscriber receives them before any event published after it
	// subscribed. Zero keeps none.
	Buffer int
	// Notify, when set, is sent a *pubsubv1.Demand when the first
	// subscriber comes and when the last one leaves: for a producer that
	// works only while someone listens.
	Notify grpcproc.Target
}

Config configures a topic.

type Subscription

type Subscription struct {
	// From is the process the events come from: the topic, or, for a topic
	// of another node, the relay on the subscriber's node.
	From grpcproc.PID
	// Ref is the subscriber's monitor of From. The Down with Ref means no
	// more events come: its reason is the topic's exit reason, or
	// noconnection when the topic's node became unreachable.
	Ref grpcproc.Ref
}

Subscription is a process's subscription to a topic.

func (Subscription) Cancel

func (s Subscription) Cancel[M proto.Message](p *grpcproc.Process[M])

Cancel ends the subscription: p stops monitoring s.From and tells it to send no more. Events already in p's mailbox stay there.

type Topic

type Topic[E proto.Message] struct {
	// contains filtered or unexported fields
}

Topic is the address of a topic whose events are E.

func Named

func Named[E proto.Message](node, name string) Topic[E]

Named addresses the topic registered as name on node.

func Spawn

func Spawn[E proto.Message](n *grpcproc.Node, cfg Config, opts ...grpcproc.SpawnOption) (Topic[E], error)

Spawn starts a topic of E on n. It lasts until it is asked to exit or n stops: a bus for the whole node, whoever publishes to it.

func SpawnOwned

func SpawnOwned[E, M proto.Message](p *grpcproc.Process[M], cfg Config, opts ...grpcproc.SpawnOption) (Topic[E], error)

SpawnOwned starts a topic of E owned by p: linked to p, it ends when p does, with p's reason, which its subscribers receive in their Down.

func TopicOf

func TopicOf[E proto.Message](t grpcproc.Target) Topic[E]

TopicOf addresses a topic by its PID or its Name.

func (Topic[E]) Addr

func (t Topic[E]) Addr() grpcproc.Addr[proto.Message]

Addr returns the address of the topic's process, to monitor it, link to it, or ask it to exit.

func (Topic[E]) Publish

func (t Topic[E]) Publish(ctx context.Context, from grpcproc.Caller, e E) error

Publish sends e to the topic, which sends it on to every subscriber. from is the Node or a Process, as for grpcproc.Addr.Send; the subscribers receive e with the metadata it was published with.

func (Topic[E]) String

func (t Topic[E]) String() string

func (Topic[E]) Subscribe

func (t Topic[E]) Subscribe[M proto.Message](ctx context.Context, p *grpcproc.Process[M]) (Subscription, error)

Subscribe subscribes p to t. When it returns, the events t keeps are in p's mailbox, and those published after them follow, as messages from s.From. p's mailbox must accept E. A second Subscribe to the same topic is the same subscription.

ctx bounds the whole of it: for a topic of another node, the relay's own subscription to the topic too, when p is the relay's first subscriber. If ctx ends first, Subscribe tells the topic to forget p, in case it got the request all the same; events it sent before that may still reach p.

Jump to

Keyboard shortcuts

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