bus

package
v0.8.0 Latest Latest
Warning

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

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

Documentation

Index

Constants

View Source
const (
	DefaultChannelSize          = 100
	DefaultCompressionThreshold = 1024
)

Variables

This section is empty.

Functions

func DecodeLocalChannel

func DecodeLocalChannel(b []byte) (string, error)

Reads Channel.Local back out of a serialized payload without decoding the message.

func Deserialize

func Deserialize(b []byte, maxSize int) (proto.Message, error)

maxSize caps the result after decompression; zero is unlimited.

func DeserializePayload

func DeserializePayload[T proto.Message](buf []byte) (T, error)

func SerializePayload

func SerializePayload(m proto.Message) ([]byte, error)

Types

type BusOption

type BusOption func(*BusOpts)

func WithBusCompression

func WithBusCompression(c CompressionOpts) BusOption

type BusOpts

type BusOpts struct {
	Compression CompressionOpts
}

type Channel

type Channel struct {
	// Flat '|'-delimited key, for brokers with no subject hierarchy.
	Legacy string
	// Hierarchical subject, e.g. SRV.<service>.<topic>.
	Server string
	// Demultiplexes several logical channels sharing one Server subject. Carried
	// in the payload, not alongside it.
	Local string
}

One destination in three forms. A transport routes on whichever it supports and ignores the rest.

type CompressionOpts

type CompressionOpts struct {
	// Zero disables compression; otherwise the gzip level.
	Quality int
	// Payload bytes below which compression is skipped.
	Threshold int
	// Caps an inbound payload after decompression, bounding amplification from a
	// hostile publisher. Zero is unlimited.
	MaxDecompressedSize int
}

CompressionOpts applies to publishing only. Decompression is driven by a flag on the wire, so a reader needs no configuration.

type EmptySubscription

type EmptySubscription[MessageType any] struct{}

func (EmptySubscription[MessageType]) Channel

func (s EmptySubscription[MessageType]) Channel() <-chan MessageType

func (EmptySubscription[MessageType]) Close

func (s EmptySubscription[MessageType]) Close() error

type MessageBus

type MessageBus interface {
	Publish(ctx context.Context, channel Channel, msg proto.Message) error
	Subscribe(ctx context.Context, channel Channel, channelSize int) (Reader, error)
	SubscribeQueue(ctx context.Context, channel Channel, channelSize int) (Reader, error)
	// Caps an inbound payload after decompression. Zero is unlimited.
	MaxDecompressedSize() int
}

func New

func New(t Transport, opts ...BusOption) MessageBus

func NewTestBus

func NewTestBus(bus MessageBus, opts ...TestBusOption) MessageBus

type PublishHandler

type PublishHandler func(ctx context.Context, channel Channel, msg proto.Message) error

type PublishInterceptor

type PublishInterceptor func(next PublishHandler) PublishHandler

type ReadHandler

type ReadHandler func() ([]byte, bool)

type Reader

type Reader interface {
	// Reports false once the reader is closed.
	Read() ([]byte, bool)
	Close() error
}

type SubscribeInterceptor

type SubscribeInterceptor func(ctx context.Context, channel Channel, next ReadHandler) ReadHandler

type Subscription

type Subscription[MessageType proto.Message] interface {
	Channel() <-chan MessageType
	Close() error
}

func Subscribe

func Subscribe[MessageType proto.Message](
	ctx context.Context,
	bus MessageBus,
	channel Channel,
	channelSize int,
) (Subscription[MessageType], error)

func SubscribeQueue

func SubscribeQueue[MessageType proto.Message](
	ctx context.Context,
	bus MessageBus,
	channel Channel,
	channelSize int,
) (Subscription[MessageType], error)

type TestBusOption

type TestBusOption func(*TestBusOpts)

type TestBusOpts

type TestBusOpts struct {
	PublishInterceptors   []PublishInterceptor
	SubscribeInterceptors []SubscribeInterceptor
}

type Transport

type Transport interface {
	Publish(ctx context.Context, channel Channel, payload []byte) error
	Subscribe(ctx context.Context, channel Channel, channelSize int) (Reader, error)
	SubscribeQueue(ctx context.Context, channel Channel, channelSize int) (Reader, error)
}

Transport is the seam a message broker implements. Payloads are opaque: encoding, compression and the decompression cap belong to the MessageBus wrapping it, so an implementation must not interpret them.

Directories

Path Synopsis
Package bustest is the psrpc bus conformance harness.
Package bustest is the psrpc bus conformance harness.
dockerutil
Package dockerutil holds the container plumbing shared by the broker-backed bustest servers.
Package dockerutil holds the container plumbing shared by the broker-backed bustest servers.
Package localbus provides an in-process psrpc bus.
Package localbus provides an in-process psrpc bus.
Package natsbus provides a psrpc bus backed by NATS.
Package natsbus provides a psrpc bus backed by NATS.
natstest
Package natstest registers a Docker-backed NATS broker with bustest.
Package natstest registers a Docker-backed NATS broker with bustest.
Package redisbus provides a psrpc bus backed by Redis pub/sub.
Package redisbus provides a psrpc bus backed by Redis pub/sub.
redistest
Package redistest registers a Docker-backed Redis broker with bustest.
Package redistest registers a Docker-backed Redis broker with bustest.

Jump to

Keyboard shortcuts

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