Documentation
¶
Overview ¶
Package nats implements the backend contract of gitlab.com/phpboyscout/go/messaging over NATS, twice.
Core is core NATS: volatile, at-most-once, the shape the in-memory backend was built to mimic. JetStream is bound to a stream: it survives a restart and offers at-least-once, with the attempt count the server's, the lease a heartbeat from receipt, and a publish that is stored before it returns. They are two backends rather than one with a flag because Capabilities.SurvivesRestart is declared, not discovered, and the two answer it differently.
A NATS server restart can spend an attempt nobody saw: a redelivery racing a consumer's retirement is counted as sent and reaches no client, and a server restarted before the next pull never offers it again. On the last attempt that message is exhausted without Exhausted counting it, so JetStream declares Capabilities.RestartCanSpendUnseenAttempt (messaging spec 0012). The gap is nats-server's; messaging-nats issue 3 carries the report.
JetStream declares Capabilities.DelaysRedelivery, so a subscription's Backoff and Delivery.NackAfter work here: a decline with a delay is a NakWithDelay, which the server keeps across its own restart. A message waiting out its delay holds one of the consumer's MaxAckPending slots, and this module leaves the server's default of 1000, so a burst of long-delayed declines can stop the consumer being offered anything new until they fall due.
Settings.Format writes what a backend sends, CloudEvents binary for the NATS binding when unset, and Settings.Reads is the chain it reads with, binary then structured when unset. A reply is written in its request's format, and headers no format takes reach a handler as Delivery.Foreign (messaging spec 0014).
This module leans on gitlab.com/phpboyscout/go/nats for everything that module already does — the connection, reconnection, the draining close, credentials, the pending-limit bound, the JetStream context — and writes the consuming, publishing and acknowledging against nats.go through the context it hands out. It is an adapter between two contracts, not a NATS client, and github.com/nats-io appears nowhere else in the estate.
Specified by https://gitlab.com/phpboyscout/go/messaging/-/wikis/specs/0003-the-second-backend
Index ¶
Examples ¶
Constants ¶
This section is empty.
Variables ¶
var ErrClosed = errors.NewSentinel("messaging_nats.closed", "messaging-nats: the session is closed")
ErrClosed is returned by Subscribe after Close, which would otherwise leave a source running beyond its session.
var ErrMemoryStream = errors.NewSentinel("messaging_nats.memory_stream",
"messaging-nats: the stream is memory-storage, which does not survive a restart; this backend declares that it does")
ErrMemoryStream is returned by Open when the bound stream is memory-storage: the backend declares SurvivesRestart, and a memory stream would make that a lie the deployment told (0003 D9).
var ErrNoStream = errors.NewSentinel("messaging_nats.no_stream",
"messaging-nats: the stream this backend is bound to does not exist; a bus binds to a stream and never creates one")
ErrNoStream is returned by Open when the stream the backend is bound to does not exist. The bus binds, and never creates (0005 D9, 0003 D9): a bus that created its own stream would let a typo silently create one with no retention policy.
var ErrSessionClosed = errors.NewSentinel("messaging-nats.session_closed",
"messaging-nats: the session is closed")
ErrSessionClosed ends a request still waiting when its session closes, so it returns at once rather than at its deadline (0011 D3).
Functions ¶
func Core ¶
Core is the core-NATS backend: volatile, at-most-once, shedding at its bound under NATS's own policy. It is the shape the in-memory backend was built to mimic, and passing the suite proves the mimicry faithful rather than the contract general (0003 Notes, on 0001 D9).
Example ¶
The volatile backend: at-most-once over core NATS, nothing kept.
package main
import (
"log/slog"
"os"
gonats "gitlab.com/phpboyscout/go/nats"
nats "gitlab.com/phpboyscout/go/messaging-nats"
)
func main() {
backend := nats.Core(nats.Settings{
Client: gonats.ClientSettings{Name: "orders", URL: "nats://localhost:4222"},
Logger: slog.New(slog.NewTextHandler(os.Stderr, nil)),
})
_ = backend.Capabilities().SurvivesRestart // false
}
Output:
func JetStream ¶
func JetStream(s JetStreamSettings) messaging.Backend
JetStream is the backend bound to a stream: it survives a restart and offers at-least-once, with the attempt count the server's, the lease a heartbeat from receipt, and a publish that is stored before it returns. Its at-most-once path is core NATS on the same client (0003 D11), so a subscriber that asks for nothing durable gets nothing durable, and no consumer, no cursor and no replay.
Example ¶
The durable backend: bound to a stream the operator created, file-backed. Subscribe at-least-once under a DurableName to keep a place in it across restarts.
package main
import (
gonats "gitlab.com/phpboyscout/go/nats"
nats "gitlab.com/phpboyscout/go/messaging-nats"
)
func main() {
backend := nats.JetStream(nats.JetStreamSettings{
Settings: nats.Settings{
Client: gonats.ClientSettings{Name: "orders", URL: "nats://localhost:4222"},
},
Stream: "ORDERS",
})
_ = backend.Capabilities().SurvivesRestart // true
}
Output:
Types ¶
type JetStreamSettings ¶
type JetStreamSettings struct {
Settings
// Stream is the operator's stream, which must exist and be file-backed.
Stream string
}
JetStreamSettings is Settings plus the stream to bind to.
type Settings ¶
type Settings struct {
Client gonats.ClientSettings
Credentials gonats.CredentialSource
Options []client.Option
Logger *slog.Logger
// Format writes every event, request and reply this backend sends. Unset,
// it is CloudEvents binary for the NATS binding (0014 D6).
Format envelope.Format
// Reads is the chain a received message is read with, strictly: one
// format must claim it. Unset, it is binary for the NATS binding, then
// structured.
Reads []envelope.Format
}
Settings is what a backend connects with. The client's own settings and credential source are go/nats's, unchanged, because this module is an adapter between two contracts and not a NATS client; Options is where a test hands in an in-process server.