Bind to a stream¶
The JetStream backend binds to a stream and never creates one. A bus that created its own would let a
typo silently create a stream with no retention policy.
What the stream must be¶
- Existing.
Openlooks the stream up byJetStreamSettings.Stream. - File-backed.
Openrefuses a memory stream, because the backend declaresSurvivesRestart. - Carrying the bus's subjects. Every publish through the backend is a JetStream publish that waits
for the server's acknowledgement, so the stream must carry every subject the bus will publish to, and
every subject a consumer's
Patternfilters on.
Create it as the operator, with whatever tool you use. With go/nats:
cli, err := client.Connect(ctx, gonats.ClientSettings{Name: "operator", URL: url}, creds)
// ...
_, err = cli.Stream(ctx, client.StreamSettings{
Name: "ORDERS",
Subjects: []string{"orders.>"},
MaxMsgs: 1_000_000,
})
go/nats's Stream refuses a stream with none of MaxAge, MaxMsgs or MaxBytes set, and defaults to
file storage unless Memory is true.
Bind¶
backend := nats.JetStream(nats.JetStreamSettings{
Settings: nats.Settings{
Client: gonats.ClientSettings{Name: "orders", URL: url},
Logger: logger,
},
Stream: "ORDERS",
})
Handle the refusals¶
Open runs inside Bus.Start, so both surface there.
switch {
case errors.Is(err, nats.ErrNoStream):
// the stream does not exist; create it, or fix the name
case errors.Is(err, nats.ErrMemoryStream):
// the stream is memory-backed; recreate it with file storage
}
Each error is wrapped with the stream name. See Errors.
Know what the bus leaves alone¶
The bus creates consumers on the stream under each subscription's DurableName and never deletes
them. It does not change the stream's configuration.