memory

package
v0.1.0 Latest Latest
Warning

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

Go to latest
Published: Oct 18, 2026 License: Apache-2.0 Imports: 7 Imported by: ? 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 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

func NewBroker[T any](retryDelay time.Duration) *Broker[T]

NewBroker returns an open broker. retryDelay is the pause before an messaging.AtLeastOnce delivery that was not acknowledged is redelivered.

func (*Broker[T]) Close

func (broker *Broker[T]) Close() error

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

func (broker *Broker[T]) Publish(ctx context.Context, envelope messaging.Envelope[T]) error

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.

Jump to

Keyboard shortcuts

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