Documentation
¶
Overview ¶
Package sql is the SQL message-store add-on: the core's snodra.MessageStore on PostgreSQL 13 or later, or MySQL 8.0.17 or later on InnoDB with a strict sql_mode (MariaDB is not supported). It keeps the events a service publishes (the outbox), the processed-event markers, the dead letters, the processor control state, the idempotent external calls and the sagas in the service's own database, in the tables of the shared SQL layout version 1.
The transaction is the service's ¶
Store.Begin starts a transaction and returns a context that carries it: an append or a marker recorded with that context joins it, so the service's rows and the library's commit or roll back together. A service that already runs a transaction through database/sql binds it with Store.WithTx (a *sql.Tx) or Store.WithConn (a *sql.Conn the service keeps in a transaction of its own); Store.Tx returns the *sql.Tx a handler received, so the handler's own writes run in the same transaction. A store call with a context that carries none runs in a short transaction of its own. Every transaction the store runs on its own (a claim, a purge) is READ COMMITTED.
The layout ¶
The schema files are the published reference files, byte for byte (schema/postgresql/V1__snodra.sql and schema/mysql/V1__snodra.sql), embedded in the binary and checked against their published SHA-256 values. Store.InitSchema applies version 1 unless the database has it or later, under the database lock the shared layout fixes, so instances of any Snodra library starting together apply it once. Store.CheckSchema only checks it, for a service that runs its own migrations. Schema versions are additive: code that knows version 1 reads a database at version 1 or later.
The stores ¶
Every port is implemented: the outbox, the processed-event markers (with a reset's marker deletion), the dead letters, the processor control, the external calls, the sagas and their dispatch, deadlines and failures.
Times the store stamps itself (an entry's sending, a claim's lease) come from the application's clock (Store.UseClock), never the database's. On PostgreSQL an append's transaction sends pg_notify on "snodra_outbox" and [OutboxStore.OnAppend] listens on a connection of its own, so a relay claims as soon as an append commits; MySQL has no such notification and the relay polls.
The store is safe for concurrent use. It owns the *sql.DB Store.DB returns; Store.Close closes the pool, ends the listener and nothing else.
Index ¶
- Constants
- type Config
- type Driver
- type Store
- func (s *Store) Background(ctx context.Context) error
- func (s *Store) Begin(ctx context.Context) (context.Context, snodra.Tx, error)
- func (s *Store) CheckSchema(ctx context.Context) error
- func (s *Store) Close() error
- func (s *Store) Control() snodra.ProcessorControlStore
- func (s *Store) DB() *stdSQL.DB
- func (s *Store) DeadLetters() snodra.DeadLetterStore
- func (s *Store) ExternalCalls() snodra.ExternalCallStore
- func (s *Store) InitSchema(ctx context.Context, _ *snodra.Config) error
- func (s *Store) Name() string
- func (s *Store) Outbox() snodra.OutboxStore
- func (s *Store) Processed() snodra.ProcessedStore
- func (s *Store) Sagas() snodra.SagaStores
- func (s *Store) Settings() Config
- func (s *Store) Tx(ctx context.Context) (*stdSQL.Tx, bool)
- func (s *Store) UseClock(now func() time.Time)
- func (s *Store) WithConn(ctx context.Context, conn *stdSQL.Conn) context.Context
- func (s *Store) WithTx(ctx context.Context, tx *stdSQL.Tx) context.Context
Constants ¶
const ( // NamePostgres is the PostgreSQL store's name. NamePostgres = "postgres" // NameMySQL is the MySQL store's name. NameMySQL = "mysql" )
The names the store reports (snodra.MessageStore.Name).
const ( // DefaultMaxOpenConns is the default connection pool size. The store needs at least two // connections: a write that commits on its own inside the service's transaction takes a // second one. DefaultMaxOpenConns = 10 // DefaultMaxIdleConns is the default number of idle connections kept. DefaultMaxIdleConns = 5 // DefaultConnMaxLifetime is how long a pooled connection is reused. DefaultConnMaxLifetime = 30 * time.Minute // DefaultPurgeInterval is how often the store deletes what the retentions let go. DefaultPurgeInterval = time.Minute // DefaultOutboxRetention keeps sent outbox entries this long. DefaultOutboxRetention = 7 * 24 * time.Hour // DefaultProcessedRetention keeps processed-event markers this long. DefaultProcessedRetention = 30 * 24 * time.Hour // DefaultExternalCallRetention keeps external-call records this long after their last update. DefaultExternalCallRetention = 30 * 24 * time.Hour // DefaultSagaEndedRetention keeps ended sagas this long. DefaultSagaEndedRetention = 30 * 24 * time.Hour // DefaultSagaDispatchRetention keeps dispatched saga entries this long. DefaultSagaDispatchRetention = 7 * 24 * time.Hour // DefaultDeadlineRetention keeps fired or cancelled deadlines this long. DefaultDeadlineRetention = 7 * 24 * time.Hour // DefaultFailedRetention keeps what failed for good this long: entries given up on and // parked saga failures. DefaultFailedRetention = 30 * 24 * time.Hour )
The store's defaults. A Config's zero value takes them; the retentions are the shared layout's defaults, the same in every SQL add-on.
const SchemaVersion int64 = 1
SchemaVersion is the schema version this add-on reads and writes. Versions are additive: code that knows version n accepts a database at version n or later.
Variables ¶
This section is empty.
Functions ¶
This section is empty.
Types ¶
type Config ¶
type Config struct {
// URL is the database to use: postgres://user:password@host:port/database?params,
// postgresql://..., mysql://user:password@host:port/database?params, or a native
// go-sql-driver/mysql DSN. It is required.
URL string
// Driver names the database ("postgres" or "mysql"); empty reads it from URL's scheme.
Driver Driver
// MaxOpenConns is the connection pool's size (default 10, at least 2): a write that commits
// on its own inside the service's transaction takes a second connection.
MaxOpenConns int
// MaxIdleConns is how many idle connections the pool keeps (default 5).
MaxIdleConns int
// ConnMaxLifetime is how long a pooled connection is reused (default 30 minutes).
ConnMaxLifetime time.Duration
// DisableNotify turns off the PostgreSQL wake-up (pg_notify/LISTEN): appends then keep no
// listener and the relay polls. It has no effect on MySQL, which always polls.
DisableNotify bool
// PurgeInterval is how often the background purge runs (default one minute); a negative
// value turns it off and the service purges by other means.
PurgeInterval time.Duration
// OutboxRetention keeps sent outbox entries this long (default 7 days).
OutboxRetention time.Duration
// ProcessedRetention keeps processed-event markers this long (default 30 days).
ProcessedRetention time.Duration
// ExternalCallRetention keeps external-call records this long after their last update
// (default 30 days).
ExternalCallRetention time.Duration
// SagaEndedRetention keeps ended sagas this long (default 30 days).
SagaEndedRetention time.Duration
// SagaDispatchRetention keeps dispatched saga entries this long (default 7 days).
SagaDispatchRetention time.Duration
// DeadlineRetention keeps fired or cancelled deadlines this long (default 7 days).
DeadlineRetention time.Duration
// FailedRetention keeps what failed for good this long (default 30 days): saga entries given
// up on and parked saga failures.
FailedRetention time.Duration
}
Config is how the store reaches its database and how long it keeps what the retentions let go. URL is required and names the database: postgres:// or postgresql:// for PostgreSQL, mysql:// (or a native go-sql-driver DSN) for MySQL.
func FromEnv ¶
func FromEnv() Config
FromEnv returns a Config from the environment: SNODRA_STORE_SQL_URL (required), SNODRA_STORE_SQL_DRIVER (optional), SNODRA_STORE_SQL_NOTIFY (true or false, default true) and SNODRA_STORE_SQL_PURGE_INTERVAL_MS (milliseconds, default 60000; 0 turns the purge off). The other fields keep their defaults; set them on the returned value.
type Store ¶
type Store struct {
// contains filtered or unexported fields
}
Store is the SQL message store. Construct it with New. Every reference to one Store is that store; do not copy the struct.
func New ¶
New creates the store over config.URL. It does not connect: Store.InitSchema or the first operation opens the pool, and a bad URL, driver or pool size fails here with snodra.ErrInvalid.
func (*Store) Background ¶
Background runs the retention purge every PurgeInterval until ctx is done. A negative interval turns it off and it waits for ctx, as the lifecycle's background work lives as long as the app.
func (*Store) Begin ¶
Begin starts a transaction and returns a context that carries it: Outbox.Append and Processed.Record called with it join it. The transaction runs at the database's default isolation, as the service's own would.
func (*Store) CheckSchema ¶
CheckSchema checks the server and fails unless the database has schema version 1 or later: the startup path of a service that applies the schema itself. It implements snodra.StoreLifecycle.
func (*Store) Close ¶
Close closes the connection pool and stops the PostgreSQL listener. The store is not usable afterwards. Calling Close more than once is a no-op.
func (*Store) Control ¶
func (s *Store) Control() snodra.ProcessorControlStore
Control returns the processor-control port, implemented on the shared control table.
func (*Store) DB ¶
DB is the connection pool the store uses. The service may begin its own transaction on it (BeginTx, at whatever isolation it needs) and bind it with Store.WithTx; the store closes the pool when Store.Close runs.
func (*Store) DeadLetters ¶
func (s *Store) DeadLetters() snodra.DeadLetterStore
DeadLetters returns the dead-letter port, implemented on the shared dead-letter table.
func (*Store) ExternalCalls ¶
func (s *Store) ExternalCalls() snodra.ExternalCallStore
ExternalCalls returns the external-call port, implemented on the shared external-call table.
func (*Store) InitSchema ¶
InitSchema checks the server and applies schema version 1 unless the database has it or later, under the shared database lock so instances starting together apply it once. It implements snodra.StoreLifecycle.
func (*Store) Processed ¶
func (s *Store) Processed() snodra.ProcessedStore
Processed returns the processed-event markers.
func (*Store) Sagas ¶
func (s *Store) Sagas() snodra.SagaStores
Sagas returns the saga ports, implemented on the shared saga, dispatch, deadline and failure tables and the processed-event markers.
func (*Store) Tx ¶
Tx returns the *sql.Tx carried by ctx: the one Store.Begin started, or the service's own bound with Store.WithTx. ok is false for a plain context and for a context carrying only a service's *sql.Conn (the service has that connection already). A handler runs its own writes with it, so they commit with the library's:
tx, ok := store.Tx(ctx)
if !ok {
return errors.New("no message-store transaction in this context")
}
_, err := tx.ExecContext(ctx, "INSERT INTO orders (id) VALUES ($1)", order.ID)
func (*Store) UseClock ¶
UseClock sets the application's clock: what the store stamps the times it sets itself with (an outbox entry's sending, a claim's lease, a purge bound). A nil clock restores the wall clock. The clock must not call the store.
func (*Store) WithConn ¶
WithConn binds the service's own *sql.Conn to ctx: an append or a marker recorded with the returned context runs on that connection, so a transaction the service keeps open on it (BEGIN, then its writes, then COMMIT) commits the library's writes with it. Only those two writes use the connection: the outbox's claims and marks, the purge and the marker reads run on the store's pool as they always do. The store does not begin, commit or roll back anything on the connection. A connection that is closed or not in a transaction is the service's to manage; the store sends its write as it is.
func (*Store) WithTx ¶
WithTx binds the service's own *sql.Tx to ctx: an append or a marker recorded with the returned context joins that transaction, so the service's rows and the library's commit or roll back together. The transaction must be one on this store's database; the store does not commit or roll it back.