libbus

package
v1.0.0 Latest Latest
Warning

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

Go to latest
Published: Aug 20, 2026 License: Apache-2.0 Imports: 15 Imported by: 0

Documentation

Overview

Package libbus is a high-level publish-subscribe abstraction over a message broker, offering fire-and-forget publish, streaming subscriptions, and request-reply (Serve/Request) on top of pluggable backends (NATS, SQLite, in-memory).

Index

Constants

This section is empty.

Variables

View Source
var (
	// ErrConnectionClosed is returned when an operation is attempted on a closed connection.
	ErrConnectionClosed = errors.New("connection closed")
	// ErrStreamSubscriptionFail is returned when a stream subscription fails.
	ErrStreamSubscriptionFail = errors.New("stream subscription failed")
	// ErrMessagePublish is returned when publishing a message fails for reasons other than a closed connection.
	ErrMessagePublish = errors.New("message publishing failed")
	// ErrRequestTimeout is returned when a request-reply operation times out.
	ErrRequestTimeout = errors.New("request timed out")
	ErrNoResponders   = fmt.Errorf("%w: no handler was subscribed to the subject, so the request was never delivered and is safe to retry", ErrRequestTimeout)
)

Functions

func SetupNatsInstance

func SetupNatsInstance(ctx context.Context) (string, testcontainers.Container, func(), error)

Types

type Config

type Config struct {
	NATSURL      string
	NATSPassword string
	NATSUser     string
}

type Handler

type Handler func(ctx context.Context, data []byte) ([]byte, error)

Handler processes a request and returns a response for Messenger.Serve.

type InMem

type InMem struct {
	// contains filtered or unexported fields
}

InMem is an in-memory Messenger for single-process use. It reproduces the NATS backend's observable contract.

func NewInMem

func NewInMem() *InMem

NewInMem returns a new in-memory Messenger.

func (*InMem) Close

func (p *InMem) Close() error

Close marks the messenger closed and releases resources.

func (*InMem) Publish

func (p *InMem) Publish(ctx context.Context, subject string, data []byte) error

Publish hands the message to every Stream subscriber's queue and returns. It never blocks on a consumer; a full queue drops the message.

func (*InMem) Request

func (p *InMem) Request(ctx context.Context, subject string, data []byte) ([]byte, error)

Request invokes the Serve handler registered for the subject, in the caller's goroutine. A missing handler fails immediately rather than at the deadline.

func (*InMem) Serve

func (p *InMem) Serve(ctx context.Context, subject string, handler Handler) (Subscription, error)

Serve registers a handler for the subject.

func (*InMem) Stream

func (p *InMem) Stream(ctx context.Context, subject string, ch chan<- []byte) (Subscription, error)

Stream creates a subscription to a subject; messages are delivered to ch.

type Messenger

type Messenger interface {
	// Publish sends a fire-and-forget message to a given subject.
	Publish(ctx context.Context, subject string, data []byte) error

	// Stream subscribes to a subject and delivers messages to ch until ctx is
	// cancelled.
	Stream(ctx context.Context, subject string, ch chan<- []byte) (Subscription, error)

	// Request sends a request message and waits for a reply. The context can be
	// used to set a timeout or to cancel the request.
	Request(ctx context.Context, subject string, data []byte) ([]byte, error)

	// Serve registers a handler for a subject; the returned Subscription stops it.
	Serve(ctx context.Context, subject string, handler Handler) (Subscription, error)

	// Close disconnects from the messaging server and releases its resources.
	Close() error
}

Messenger is a pub-sub/request-reply interface for distributing lightweight messages between services. Every backend guarantees that Publish to a subject with no subscribers never blocks, Stream delivers in publish order, a handler error still yields a reply, and every method returns ErrConnectionClosed after Close. Backends differ in durability, backpressure and latency, so always give Request a deadline.

func NewPubSub

func NewPubSub(ctx context.Context, cfg *Config) (Messenger, error)

func NewTestPubSub

func NewTestPubSub() (Messenger, func(), error)

NewTestPubSub starts a NATS container using SetupNatsInstance, creates a new PubSub instance, and returns it along with a cleanup function.

type SQLiteBus added in v0.4.0

type SQLiteBus struct {
	// contains filtered or unexported fields
}

SQLiteBus implements Messenger over a SQLite database. The bus_events, bus_requests and bus_replies tables must exist before use.

func NewSQLite added in v0.4.0

func NewSQLite(exec sqlExec) *SQLiteBus

NewSQLite creates a SQLite-backed Messenger over the result of dbManager.WithoutTransaction().

func NewSQLiteWithOptions added in v0.6.8

func NewSQLiteWithOptions(exec sqlExec, opt SQLiteBusOptions) *SQLiteBus

NewSQLiteWithOptions is like NewSQLite but allows tuning poll intervals.

func (*SQLiteBus) Close added in v0.4.0

func (b *SQLiteBus) Close() error

Close stops all background goroutines. The underlying database is not closed.

func (*SQLiteBus) Publish added in v0.4.0

func (b *SQLiteBus) Publish(ctx context.Context, subject string, data []byte) error

Publish inserts a row into bus_events so Stream subscribers can pick it up.

func (*SQLiteBus) Request added in v0.4.0

func (b *SQLiteBus) Request(ctx context.Context, subject string, data []byte) ([]byte, error)

Request inserts a request row and polls for the reply until ctx deadline or 10s timeout.

func (*SQLiteBus) Serve added in v0.4.0

func (b *SQLiteBus) Serve(ctx context.Context, subject string, handler Handler) (Subscription, error)

Serve registers a handler for subject. A polling goroutine picks up rows from bus_requests, calls the handler, and writes the reply to bus_replies.

func (*SQLiteBus) Stream added in v0.4.0

func (b *SQLiteBus) Stream(ctx context.Context, subject string, ch chan<- []byte) (Subscription, error)

Stream starts a polling goroutine that delivers new bus_events for subject to ch. It stops when ctx is cancelled.

type SQLiteBusOptions added in v0.6.8

type SQLiteBusOptions struct {
	EventPoll   time.Duration
	RequestPoll time.Duration
}

SQLiteBusOptions overrides poll intervals.

type Subscription

type Subscription interface {
	// Unsubscribe removes the subscription, stopping the delivery of messages.
	Unsubscribe() error
}

Subscription represents an active subscription to a subject.

Jump to

Keyboard shortcuts

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