Documentation
¶
Overview ¶
Package memory provides a bounded, process-local implementation of the messaging Publisher and Subscriber ports.
Broker is a reference transport for tests, local composition and in-process event fan-out. It does not persist messages.
Index ¶
Examples ¶
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
This section is empty.
Types ¶
type Broker ¶
type Broker[T any] struct { // contains filtered or unexported fields }
Broker is a bounded, process-local messaging.Publisher and messaging.Subscriber. It is safe for concurrent use.
Each consumer, identified by subject and consumer name, has a buffer of Subscription.MaxInFlight envelopes. Publish delivers to the consumers of the envelope's subject in consumer-name order and blocks while a consumer's buffer is full. The broker never persists messages.
Example ¶
FR-MSG-12, FR-MSG-16
package main
import (
"context"
"fmt"
"time"
"github.com/justusschock/messaging"
"github.com/justusschock/messaging/memory"
)
func main() {
ctx, cancel := context.WithCancel(context.Background())
defer cancel()
broker := memory.NewBroker[string](10 * time.Millisecond)
defer func() { _ = broker.Close() }()
subscription := messaging.Subscription{
Subject: "orders.created",
Consumer: "audit",
Semantics: messaging.AtLeastOnce,
MaxInFlight: 8,
}
received := make(chan string, 1)
stopped := make(chan error, 1)
go func() {
stopped <- broker.Subscribe(ctx, subscription, func(ctx context.Context, delivery messaging.Delivery[string]) error {
select {
case received <- delivery.Envelope().Payload:
default: // a duplicate from the publish loop below
}
return delivery.Ack(ctx)
})
}()
envelope, err := messaging.NewEnvelope("orders.created", "order-42")
if err != nil {
fmt.Println(err)
return
}
// Subscribe registers the consumer asynchronously, and an envelope
// published before registration has no consumer. Publish until the
// subscriber has received one.
for delivered := false; !delivered; {
if err := broker.Publish(ctx, envelope); err != nil {
fmt.Println(err)
return
}
select {
case payload := <-received:
fmt.Println("received", payload)
delivered = true
case <-time.After(time.Millisecond):
}
}
cancel()
fmt.Println("subscriber stopped:", <-stopped)
}
Output: received order-42 subscriber stopped: context canceled
func NewBroker ¶
NewBroker returns an open broker. retryDelay is the pause before an messaging.AtLeastOnce delivery that was not acknowledged is redelivered.
func (*Broker[T]) Close ¶
Close closes the broker, ending active subscriptions with messaging.ErrClosed and waking blocked publishers. Later Publish and Subscribe calls return messaging.ErrClosed. Close is idempotent and always returns nil.
func (*Broker[T]) Publish ¶
Publish validates the envelope and enqueues a clone for every current consumer of its subject. It blocks while a consumer's buffer is full until capacity frees up, the consumer leaves, or ctx ends (returning ctx.Err()). It returns messaging.ErrClosed after Broker.Close.
func (*Broker[T]) Subscribe ¶
func (broker *Broker[T]) Subscribe(ctx context.Context, subscription messaging.Subscription, handler messaging.Handler[T]) error
Subscribe registers the consumer and runs handler for each delivery until ctx ends (returning ctx.Err()) or the broker closes (returning messaging.ErrClosed). It returns messaging.ErrConsumerExists if the subject already has an active consumer with the same name.
Under messaging.AtMostOnce the handler is called once per envelope and its result is ignored. Under messaging.AtLeastOnce an attempt that is not acknowledged (handler error, Nack, or no settlement) is retried after the broker's retry delay with an incremented attempt number. Deliveries are processed sequentially.