package outbound import ( "context" "encoding/json" "errors" "time" "github.com/google/uuid" ) // ErrEventDestinationMissing reports that the event bus has no destination // for an event, for example a deleted topic. The relay runs // EventPublisher.Prepare again before its next pass. var ErrEventDestinationMissing = errors.New("event destination missing") // OutboxBacklog describes the unpublished events at one moment. type OutboxBacklog struct { // Pending counts unpublished events. Pending int64 // OldestAge is the age of the earliest unpublished event by position, // measured by the database clock; zero without pending events. OldestAge time.Duration } // OutboxEvent is a committed outbox row that the relay publishes. Payload is // the stored jsonb object; PostgreSQL normalizes it, so it can differ in key // order and whitespace from what the writer appended. type OutboxEvent struct { EventID uuid.UUID EventType string SchemaVersion int32 AggregateType string AggregateID uuid.UUID AggregateVersion int64 Payload json.RawMessage // OccurredAt is the informational insert time. It does not define order. OccurredAt time.Time } // PublishOutboxBatch publishes a locked batch and returns the IDs of the // events that the event bus acknowledged. It must not return an ID it did not // see acknowledged. type PublishOutboxBatch func(ctx context.Context, events []OutboxEvent) (acknowledged []uuid.UUID) // OutboxRelayStore hands pending outbox events to the relay under row locks. type OutboxRelayStore interface { // ClaimPending opens one transaction and locks up to limit pending events: // for each aggregate only its earliest unpublished event, skipping rows // that another relay holds. It calls publish with the events in position // order while it holds the locks, marks exactly the returned IDs as // published, and commits. With no pending event it returns without // calling publish. // // If ctx ends or the commit fails after publish returned, nothing is // marked and the acknowledged events are published again on a later // pass with the same event IDs. An error from the store never says // whether the event bus received the batch. ClaimPending(ctx context.Context, limit int, publish PublishOutboxBatch) error // Backlog reads the size and age of the unpublished events without // locking them. Backlog(ctx context.Context) (OutboxBacklog, error) } // EventPublisher delivers outbox events to the event bus. type EventPublisher interface { // Prepare makes the destinations ready, for example by creating missing // topics. It is idempotent; the relay calls it before its first publish // and repeats it with backoff until it succeeds. Prepare(ctx context.Context) error // Publish sends the events and waits until the event bus acknowledges // each one or ctx ends. The result has one entry per event in input // order: nil means durably acknowledged. An error matches // ErrEventDestinationMissing when the destination does not exist. An // event that is not acknowledged may still reach the event bus later, so // a retry can deliver it twice with the same event ID. Publish(ctx context.Context, events []OutboxEvent) []error }