Documentation
¶
Index ¶
Constants ¶
This section is empty.
Variables ¶
var ErrExclusiveLeaseHeld = errors.New("exclusive lease held by another instance")
ErrExclusiveLeaseHeld is returned by ProcessMessages when another outbox instance currently holds a valid exclusive lease for the topic.
Functions ¶
func Migrate ¶
Migrate runs the embedded pgoutbox migrations against the given pool. It is the explicit alternative to NewOutbox's auto-migration: callers that want to control when DDL runs (separate startup phase, release pipeline, etc.) should construct the outbox with WithAutoMigrate(false) and invoke Migrate themselves.
Only WithSchema is consulted from opts; other options are accepted for API symmetry but ignored.
Types ¶
type FlushContext ¶ added in v0.2.0
FlushContext is the context passed to Flusher.Flush. It embeds context.Context and exposes the transaction that ProcessMessages uses to lock and delete messages. Callers that want their writes to commit atomically with the outbox delete can enlist in that transaction via Tx().
type MessageOpts ¶
type MessageOpts struct {
Payload []byte
}
type NopFlusher ¶ added in v0.2.0
type NopFlusher struct{}
func NewNopFlusher ¶ added in v0.2.0
func NewNopFlusher() *NopFlusher
func (*NopFlusher) Flush ¶ added in v0.2.0
func (f *NopFlusher) Flush(_ FlushContext, _ []*sqlc.Message) error
type Outbox ¶
type Outbox interface {
AddFlusher(topic string, flusher Flusher)
AddMessages(ctx context.Context, tx pgx.Tx, topic string, msgs []MessageOpts) error
// ProcessMessages grabs a batch of messages for the given topic, flushes them using the registered Flusher for that
// topic, and deletes them from the outbox if the flush is successful. If the topic has an active exclusive consumer,
// the calling instance must hold the exclusive lease (via AcquireTopic) or an error is returned.
ProcessMessages(ctx context.Context, topic string, opts ...ProcessOpt) ([]*sqlc.Message, error)
// AcquireTopic blocks until this instance holds the exclusive processing lease
// for the named topic, then returns. A background goroutine automatically renews
// the lease until ctx is cancelled or ReleaseTopic is called, at which point the
// lease expires naturally and another instance can take over. AcquireTopic must
// be called before ProcessMessages for any topic that has an active exclusive
// consumer.
AcquireTopic(ctx context.Context, topic string) error
// ReleaseTopic stops renewing and immediately expires the exclusive lease
// this instance holds for topic, letting another instance acquire it right
// away instead of waiting out the lease duration. It is a no-op if this
// instance does not currently hold the lease. As with a naturally expired
// lease, a subsequent ProcessMessages call still requires an explicit
// AcquireTopic first.
ReleaseTopic(ctx context.Context, topic string) error
}
type OutboxOpt ¶
type OutboxOpt func(*outboxImplOpts)
func WithAutoMigrate ¶
WithAutoMigrate controls whether NewOutbox runs the embedded migrations on construction. Defaults to true. Set to false when the caller wants to run migrations explicitly via Migrate (for example, in a separate startup phase or release pipeline).
func WithDefaultExpiration ¶ added in v0.2.0
WithDefaultExpiration sets a fallback TTL used for topics that have no specific expiration configured via WithTopicExpiration. Any topic that appears in the topics table with a NULL expiration_nanos will be maintained using this TTL when Start is running.
func WithLogger ¶ added in v0.2.0
WithLogger attaches a zerolog logger that receives error-level messages from the background maintenance goroutines. Lease competition (another instance holding the lease) is not logged. If not set, maintenance errors are silent.
func WithSchema ¶
func WithTopicExpiration ¶ added in v0.2.0
WithTopicExpiration registers a TTL for the named topic. On Start, the TTL is written to the topics table so that any outbox instance can discover it. Messages older than ttl are eligible for deletion by the background maintenance goroutine launched by Start. Per-topic TTLs take precedence over WithDefaultExpiration.
type ProcessOpt ¶ added in v0.2.0
type ProcessOpt func(*processOpts)
ProcessOpt is a per-call option for ProcessMessages.
func WithBatchSize ¶
func WithBatchSize(n int) ProcessOpt
WithBatchSize sets the maximum number of messages ProcessMessages will acquire and hand to the Flusher in a single call. Must be > 0. Values above math.MaxInt32 are ignored and the default (1000) is used instead.