eventbus

package
v1.19.1 Latest Latest
Warning

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

Go to latest
Published: Aug 29, 2026 License: GPL-3.0 Imports: 4 Imported by: 0

Documentation

Overview

Package eventbus is the domain-event bus riding the asynq message queue (ADR-001 step-6 preparation). Producers append events to the outbox inside their domain transaction; the publish pump hands unpublished events to the broker and marks them published on successful enqueue; the queue worker delivers each event to every subscriber of its topic, with the broker owning retries, backoff and the dead-letter archive. Delivery is at-least-once and subscribers are expected to be idempotent (the inbox pattern). Replacing asynq with another broker changes only the Publisher adapter and the worker shell.

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type Bus

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

Bus routes outbox events to subscribers by topic.

func New

func New(outboxRepo repository.OutboxRepo, publisher Publisher) *Bus

func (*Bus) Deliver

func (b *Bus) Deliver(ctx context.Context, event Event) error

Deliver runs every subscriber of the event's topic; the queue worker calls it once per delivery task. The first failing subscriber fails the task so the broker retries the whole event — subscribers that already succeeded re-run and skip via their inbox markers.

func (*Bus) Publish

func (b *Bus) Publish(ctx context.Context, limit int) error

Publish drains up to limit unpublished outbox events onto the broker, marking each published once its enqueue succeeded. A failing enqueue stops the tick so events enter the broker in outbox order; ordering between deliveries is then the broker's (concurrent workers), which subscribers already tolerate via their per-key serialization and inbox markers. Events whose topic has no subscribers are marked published without an enqueue so an orphaned topic cannot wedge the pump.

func (*Bus) Subscribe

func (b *Bus) Subscribe(topic, consumer string, handler Handler)

Subscribe registers a consumer for a topic. The consumer name is the subscriber's identity for logging and mirrors the inbox consumer it uses for idempotency. Registration happens at composition time, before publishing and delivery start; Subscribe is not safe for concurrent use with them.

type Event

type Event struct {
	ID      int64
	Topic   string
	Key     string
	Payload string
	// TraceCarrier is the producing request's serialized trace context
	// (from the outbox row); the broker adapter resumes it so the delivery
	// joins the originating trace.
	TraceCarrier string
}

Event is the payload handed to subscribers.

type Handler

type Handler func(ctx context.Context, event Event) error

Handler processes one event. Returning an error fails the delivery task so the broker retries it; handlers must therefore be idempotent.

type Publisher

type Publisher interface {
	Publish(ctx context.Context, event Event) error
}

Publisher enqueues an event on the message broker. Publishing the same event twice must be safe: the pump replays an enqueue when marking the event published fails, so the adapter deduplicates by event ID where the broker allows and subscribers stay idempotent regardless.

Jump to

Keyboard shortcuts

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