pubsub

package
v2.39.5 Latest Latest
Warning

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

Go to latest
Published: Nov 24, 2026 License: AGPL-3.0 Imports: 21 Imported by: 1 module, 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

Index

Constants

View Source
const (
	// BackendPostgres labels metrics produced by the PostgreSQL pubsub.
	BackendPostgres = "postgres"
	// BackendNATS labels metrics produced by the NATS pubsub.
	BackendNATS = "nats"
)
View Source
const (
	ColossalThreshold   = 7600
	MessageSizeNormal   = "normal"
	MessageSizeColossal = "colossal"
)

We'll track messages as size "normal" and "colossal", where the latter are messages larger than 7600 bytes, or 95% of the postgres notify limit. If we see a lot of colossal packets that's an indication that we might be trying to send too much data over the pubsub and are in danger of failing to publish.

These are exported so other pubsub implementations (e.g. the NATS pubsub) classify message size identically, keeping the messages_total "size" label consistent across backends.

View Source
const BufferSize = 2048

BufferSize is the maximum number of unhandled messages we will buffer for a subscriber before dropping messages.

View Source
const (
	EventPubsubWatchdog = "pubsub_watchdog"
)
View Source
const LatencyMessageLength = 36

LatencyMessageLength is the length of a UUIDv4 encoded to hex.

Variables

View Source
var ErrDroppedMessages = xerrors.New("dropped messages")

ErrDroppedMessages is sent to ListenerWithErr if messages are dropped or might have been dropped.

Functions

This section is empty.

Types

type BackendMetrics

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

BackendMetrics records into the shared Metrics with a fixed `backend` label value. Backends hold one of these instead of the shared Metrics so the only thing they can do is record their own series.

func (*BackendMetrics) AddEvent

func (b *BackendMetrics) AddEvent(event string)

AddEvent, RemoveEvent, AddSubscriber, and RemoveSubscriber maintain the current_events and current_subscribers gauges.

func (*BackendMetrics) AddSubscriber

func (b *BackendMetrics) AddSubscriber(event string)

func (*BackendMetrics) MarkConnected

func (b *BackendMetrics) MarkConnected()

MarkConnected sets the connected gauge to 1.

func (*BackendMetrics) MarkDisconnected

func (b *BackendMetrics) MarkDisconnected()

MarkDisconnected sets the connected gauge to 0.

func (*BackendMetrics) RecordDisconnect

func (b *BackendMetrics) RecordDisconnect()

RecordDisconnect increments the unexpected-disconnect counter.

func (*BackendMetrics) RecordPublishFailure

func (b *BackendMetrics) RecordPublishFailure(event string)

RecordPublishFailure records a failed Publish.

func (*BackendMetrics) RecordPublishSuccess

func (b *BackendMetrics) RecordPublishSuccess(event string, n int)

RecordPublishSuccess records a successful Publish of n bytes.

func (*BackendMetrics) RecordReceived

func (b *BackendMetrics) RecordReceived(event string, data []byte)

RecordReceived records metrics for a single received message. Size is classified using the shared thresholds so the messages_total "size" label is consistent across backends.

func (*BackendMetrics) RecordSubscribeFailure

func (b *BackendMetrics) RecordSubscribeFailure(event string)

RecordSubscribeFailure records a failed Subscribe/SubscribeWithErr.

func (*BackendMetrics) RecordSubscribeSuccess

func (b *BackendMetrics) RecordSubscribeSuccess(event string)

RecordSubscribeSuccess records a successful Subscribe/SubscribeWithErr.

func (*BackendMetrics) RemoveEvent

func (b *BackendMetrics) RemoveEvent(event string)

func (*BackendMetrics) RemoveSubscriber

func (b *BackendMetrics) RemoveSubscriber(event string)

func (*BackendMetrics) StartLatencyLoop

func (b *BackendMetrics) StartLatencyLoop(p Pubsub)

StartLatencyLoop spawns the background latency loop against p. The first measurement runs immediately so a fresh observation exists before the first interval elapses. It must be called at most once. Call StopLatencyLoop to cancel the loop and wait for it to exit.

func (*BackendMetrics) StopLatencyLoop

func (b *BackendMetrics) StopLatencyLoop()

StopLatencyLoop cancels the background latency loop and waits for it to exit. When StartLatencyLoop was never called (e.g. a pubsub built via newWithoutListener) there is no loop to wait for, so it returns immediately.

type LatencyMeasurer added in v2.12.0

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

LatencyMeasurer is used to measure the send & receive latencies of the underlying Pubsub implementation. We use these measurements to export metrics which can indicate when a Pubsub implementation's queue is overloaded and/or full.

func NewLatencyMeasurer added in v2.12.0

func NewLatencyMeasurer(logger slog.Logger) *LatencyMeasurer

func (*LatencyMeasurer) Measure added in v2.12.0

func (lm *LatencyMeasurer) Measure(ctx context.Context, p Pubsub) (send, recv time.Duration, err error)

Measure takes a given Pubsub implementation, publishes a message & immediately receives it, and returns the observed latency.

type Listener

type Listener func(ctx context.Context, message []byte)

Listener represents a pubsub handler.

type ListenerWithErr

type ListenerWithErr func(ctx context.Context, message []byte, err error)

ListenerWithErr represents a pubsub handler that can also receive error indications

type MemoryPubsub

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

MemoryPubsub is an in-memory Pubsub implementation. It's an exported type so that our test code can do type checks.

func (*MemoryPubsub) Close

func (*MemoryPubsub) Close() error

func (*MemoryPubsub) Publish

func (m *MemoryPubsub) Publish(event string, message []byte) error

func (*MemoryPubsub) Subscribe

func (m *MemoryPubsub) Subscribe(event string, listener Listener) (cancel func(), err error)

func (*MemoryPubsub) SubscribeWithErr

func (m *MemoryPubsub) SubscribeWithErr(event string, listener ListenerWithErr) (cancel func(), err error)

type Metrics

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

Metrics owns the Prometheus instruments shared by all pubsub backends. Each backend obtains a BackendMetrics handle via ForBackend that records into these instruments with its own `backend` label value.

func NewMetrics

func NewMetrics(reg prometheus.Registerer) *Metrics

NewMetrics builds the shared pubsub instruments, registering them with reg when it is non-nil and otherwise creating them unregistered (no-op).

func (*Metrics) ForBackend

func (m *Metrics) ForBackend(logger slog.Logger, backend string) *BackendMetrics

ForBackend returns a recording handle bound to the given backend. Each handle has its own LatencyMeasurer so probe channels never clash across backends.

type MsgQueue added in v2.34.0

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

MsgQueue implements a fixed length queue with the ability to replace elements after they are queued (but before they are dequeued).

The purpose of this data structure is to build something that works a bit like a golang channel, but if the queue is full, then we can replace the last element with an error so that the subscriber can get notified that some messages were dropped, all without blocking.

func NewMsgQueue added in v2.34.0

func NewMsgQueue(ctx context.Context, l Listener, le ListenerWithErr) *MsgQueue

func (*MsgQueue) Close added in v2.34.0

func (q *MsgQueue) Close()

func (*MsgQueue) Dropped added in v2.34.0

func (q *MsgQueue) Dropped()

Dropped records an error in the queue that messages might have been Dropped

func (*MsgQueue) Enqueue added in v2.34.0

func (q *MsgQueue) Enqueue(msg []byte)

type PGPubsub added in v2.8.0

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

PGPubsub is a pubsub implementation using PostgreSQL.

func New

func New(startCtx context.Context, logger slog.Logger, db *sql.DB, connectURL string, metrics *Metrics) (*PGPubsub, error)

New creates a new Pubsub implementation using a PostgreSQL connection. The metrics argument is the shared pubsub metrics; pass nil to disable metrics (an unregistered set is created internally).

func (*PGPubsub) Close added in v2.8.0

func (p *PGPubsub) Close() error

Close closes the pubsub instance.

func (*PGPubsub) Publish added in v2.8.0

func (p *PGPubsub) Publish(event string, message []byte) error

func (*PGPubsub) Subscribe added in v2.8.0

func (p *PGPubsub) Subscribe(event string, listener Listener) (cancel func(), err error)

Subscribe calls the listener when an event matching the name is received.

func (*PGPubsub) SubscribeWithErr added in v2.8.0

func (p *PGPubsub) SubscribeWithErr(event string, listener ListenerWithErr) (cancel func(), err error)

type Publisher added in v2.33.0

type Publisher interface {
	Publish(event string, message []byte) error
}

type Pubsub

type Pubsub interface {
	Subscriber
	Publisher
	Close() error
}

Pubsub is a generic interface for broadcasting and receiving messages. Implementors should assume high-availability with the backing implementation.

func NewInMemory

func NewInMemory() Pubsub

type Subscriber added in v2.33.0

type Subscriber interface {
	Subscribe(event string, listener Listener) (cancel func(), err error)
	SubscribeWithErr(event string, listener ListenerWithErr) (cancel func(), err error)
}

type Watchdog added in v2.8.0

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

func NewWatchdog added in v2.8.0

func NewWatchdog(ctx context.Context, logger slog.Logger, ps Pubsub) *Watchdog

func NewWatchdogWithClock added in v2.8.0

func NewWatchdogWithClock(ctx context.Context, logger slog.Logger, ps Pubsub, c quartz.Clock) *Watchdog

NewWatchdogWithClock returns a watchdog with the given clock. Product code should always call NewWatchDog.

func (*Watchdog) Close added in v2.8.0

func (w *Watchdog) Close() error

func (*Watchdog) Timeout added in v2.8.0

func (w *Watchdog) Timeout() <-chan struct{}

Timeout returns a channel that is closed if the watchdog times out. Note that the Timeout() chan will NOT be closed if the Watchdog is Close'd or its context expires, so it is important to read from the Timeout() chan in a select e.g.

w := NewWatchDog(ctx, logger, ps) select { case <-ctx.Done(): case <-w.Timeout():

   FreakOut()
}

Directories

Path Synopsis
package psmock contains a mocked implementation of the pubsub.Pubsub interface for use in tests
package psmock contains a mocked implementation of the pubsub.Pubsub interface for use in tests

Jump to

Keyboard shortcuts

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