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 ¶
- type Config
- type Subscription
- type Topic
- func Named[E proto.Message](node, name string) Topic[E]
- func Spawn[E proto.Message](n *grpcproc.Node, cfg Config, opts ...grpcproc.SpawnOption) (Topic[E], error)
- func SpawnOwned[E, M proto.Message](p *grpcproc.Process[M], cfg Config, opts ...grpcproc.SpawnOption) (Topic[E], error)
- func TopicOf[E proto.Message](t grpcproc.Target) Topic[E]
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.
type Topic ¶
Topic is the address of a topic whose events are E.
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 (Topic[E]) Addr ¶
Addr returns the address of the topic's process, to monitor it, link to it, or ask it to exit.
func (Topic[E]) Publish ¶
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]) 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.