Documentation
¶
Index ¶
- Constants
- Variables
- type BackendMetrics
- func (b *BackendMetrics) AddEvent(event string)
- func (b *BackendMetrics) AddSubscriber(event string)
- func (b *BackendMetrics) MarkConnected()
- func (b *BackendMetrics) MarkDisconnected()
- func (b *BackendMetrics) RecordDisconnect()
- func (b *BackendMetrics) RecordPublishFailure(event string)
- func (b *BackendMetrics) RecordPublishSuccess(event string, n int)
- func (b *BackendMetrics) RecordReceived(event string, data []byte)
- func (b *BackendMetrics) RecordSubscribeFailure(event string)
- func (b *BackendMetrics) RecordSubscribeSuccess(event string)
- func (b *BackendMetrics) RemoveEvent(event string)
- func (b *BackendMetrics) RemoveSubscriber(event string)
- func (b *BackendMetrics) StartLatencyLoop(p Pubsub)
- func (b *BackendMetrics) StopLatencyLoop()
- type LatencyMeasurer
- type Listener
- type ListenerWithErr
- type MemoryPubsub
- func (*MemoryPubsub) Close() error
- func (m *MemoryPubsub) Publish(event string, message []byte) error
- func (m *MemoryPubsub) Subscribe(event string, listener Listener) (cancel func(), err error)
- func (m *MemoryPubsub) SubscribeWithErr(event string, listener ListenerWithErr) (cancel func(), err error)
- type Metrics
- type MsgQueue
- type PGPubsub
- type Publisher
- type Pubsub
- type Subscriber
- type Watchdog
Constants ¶
const ( // BackendPostgres labels metrics produced by the PostgreSQL pubsub. BackendPostgres = "postgres" // BackendNATS labels metrics produced by the NATS pubsub. BackendNATS = "nats" )
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.
const BufferSize = 2048
BufferSize is the maximum number of unhandled messages we will buffer for a subscriber before dropping messages.
const (
EventPubsubWatchdog = "pubsub_watchdog"
)
const LatencyMessageLength = 36
LatencyMessageLength is the length of a UUIDv4 encoded to hex.
Variables ¶
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
type ListenerWithErr ¶
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) 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
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) Subscribe ¶ added in v2.8.0
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 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 Watchdog ¶ added in v2.8.0
type Watchdog struct {
// contains filtered or unexported fields
}
func NewWatchdog ¶ added in v2.8.0
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) 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() }