Documentation
¶
Overview ¶
Package outbox publishes messages to a message broker reliably, with the transactional outbox pattern.
Outbox.Store saves messages in the same transaction as the business data that produced them, so a message exists if and only if that data was committed. A relay, registered on a worker with Outbox.AddRelay, then publishes the stored messages through a Publisher that wraps any broker client: Kafka, NATS, RabbitMQ, or another. Messages with the same topic and key are published one at a time, in the order they were stored. A message that cannot be published is retried for about a month; then it is dead, and its key halts until an operator retries or deletes it, so later messages never overtake it.
Publishing is at least once: a message can be published twice, for example if the worker crashes after the broker accepted it. Every delivery carries an ID that stays the same across retries, so consumers can deduplicate. The receiving side needs no package: a consumer that enqueues a task for each message, with the message ID as taskmill.EnqueueOptions.DedupeKey, creates at most one task per message within the dedupe window.
Example ¶
This example stores an event with the business change that produced it, and relays stored events to a broker.
package main
import (
"context"
"log"
"github.com/jackc/pgx/v5"
"github.com/jackc/pgx/v5/pgxpool"
"github.com/code19m/taskmill"
"github.com/code19m/taskmill/outbox"
)
func main() {
ctx := context.Background()
var pool *pgxpool.Pool // the application's pool
var client *taskmill.Client // from taskmill.NewClient
ob := outbox.New(client)
err := pgx.BeginFunc(ctx, pool, func(tx pgx.Tx) error {
if _, err := tx.Exec(ctx, "UPDATE orders SET status = 'paid' WHERE id = 42"); err != nil {
return err
}
return ob.Store(ctx, taskmill.Pgx(tx), outbox.Message{
Topic: "orders",
Key: []byte("42"),
Value: []byte(`{"type": "order.paid", "order_id": 42}`),
})
})
if err != nil {
log.Fatal(err)
}
var handlers taskmill.Handlers
ob.AddRelay(&handlers, outbox.PublisherFunc(func(_ context.Context, ds []outbox.Delivery) []error {
errs := make([]error, len(ds)) // nil: published
for _, d := range ds {
log.Printf("publish %s to %s with key %s", d.ID, d.Topic, d.Key) // your broker client here
}
return errs
}))
worker, err := taskmill.NewWorker(client, &taskmill.WorkerConfig{Handlers: &handlers}) // serves outbox.Queue
if err != nil {
log.Fatal(err)
}
_ = worker.Run(ctx)
}
Output:
Index ¶
Examples ¶
Constants ¶
const Queue = "outbox"
Queue is the taskmill queue that holds stored messages. A worker with a relay serves it by default.
Variables ¶
This section is empty.
Functions ¶
This section is empty.
Types ¶
type Delivery ¶
type Delivery struct {
Message
// ID is unique across services and stays the same when a delivery is
// retried, so consumers can deduplicate by it. It is a UUIDv7.
ID string
}
Delivery is a stored message on its way to the broker.
type Message ¶
type Message struct {
// Topic is the broker destination, such as a Kafka topic. Required.
Topic string
// Key orders messages: messages with the same topic and key are
// published one at a time, in the order they were stored. Messages
// without a key are published in any order.
Key []byte
// Value is the message body. It is stored base64-encoded in a task
// payload, whose 1 MiB limit allows about 750 KiB of Value.
Value []byte
// Headers are passed to the broker with the message.
Headers map[string][]byte
}
Message is an event to publish.
type Outbox ¶
type Outbox struct {
// contains filtered or unexported fields
}
Outbox stores messages and relays them to a broker. Create one with New and share it: it is safe for concurrent use.
func (*Outbox) AddRelay ¶
AddRelay registers the batch handler that publishes stored messages through pub. A worker with no Queues set serves Queue for it. A nil pub is reported by taskmill.NewWorker.
func (*Outbox) Store ¶
Store saves msgs in the outbox inside the caller's transaction, so they are published only if the transaction commits. A message without a topic fails with an error that wraps taskmill.ErrInvalidArgument.
type Publisher ¶
Publisher sends deliveries to a broker. It returns one error per delivery, in the same order: nil marks that delivery as published, an error retries it later, and an error wrapped with taskmill.Permanent makes it dead at once. A batch holds at most 100 deliveries, never two with the same topic and key. Each call has 1 minute: then its context is canceled, and the deliveries without a nil result are retried.
type PublisherFunc ¶
PublisherFunc adapts a function to the Publisher interface.