Documentation
¶
Overview ¶
Package bus embeds a NATS server with JetStream inside the Outpost daemon to give every managed app a durable pub/sub event bus using CloudEvents 1.0.
Index ¶
- Constants
- func AppOwner(subject string) string
- func ConsumerName(app, filterSubject string) string
- func MatchSubject(pattern, subject string) bool
- func ValidateAppName(app string) error
- func ValidatePattern(pattern string) error
- func ValidateSubject(subject string) error
- type AppDecl
- type ConsumerInfo
- type DLQEntry
- type Event
- type EventQuery
- type EventRecord
- type Options
- type Overview
- type PublishRequest
- type PublishResult
- type ReplayResult
- type Service
- func (s *Service) AdminConn() *nats.Conn
- func (s *Service) Close() error
- func (s *Service) Consumers(ctx context.Context, streamName string) ([]ConsumerInfo, error)
- func (s *Service) CredsPath(app string) (string, bool)
- func (s *Service) DLQList(ctx context.Context, app string, limit int) ([]DLQEntry, error)
- func (s *Service) DLQReplay(ctx context.Context, id string) (ReplayResult, error)
- func (s *Service) EnsureApp(ctx context.Context, app string, subscribes []string) (credsPath string, rev string, err error)
- func (s *Service) EnsureInternalConsumer(ctx context.Context, owner, consumerName, filterSubject string) (jetstream.Consumer, error)
- func (s *Service) Events(ctx context.Context, q EventQuery) ([]EventRecord, error)
- func (s *Service) JetStream() jetstream.JetStream
- func (s *Service) Overview(ctx context.Context) (Overview, error)
- func (s *Service) Publish(ctx context.Context, req PublishRequest) (PublishResult, error)
- func (s *Service) Resolve(service string) (map[string]string, string)
- func (s *Service) Streams(ctx context.Context) ([]StreamInfo, error)
- func (s *Service) SyncApps(ctx context.Context, decls []AppDecl) error
- func (s *Service) Tail(ctx context.Context, subject string, fn func(EventRecord) error) error
- func (s *Service) URL() string
- type StreamInfo
Constants ¶
const ( EnvURL = "OUTPOST_BUS_URL" EnvCreds = "OUTPOST_BUS_CREDS" EnvApp = "OUTPOST_APP" )
Environment variables injected into every managed app when the bus is running.
const ( // StreamEvents holds all app events on outpost.<app>.<object>.<action>[...]. StreamEvents = "OUTPOST" // StreamDLQ holds dead-lettered events on outpost.dlq.<app>. StreamDLQ = "OUTPOST_DLQ" )
JetStream stream names.
const CloudEventsSpecVersion = "1.0"
CloudEventsSpecVersion is the CloudEvents specification version Outpost uses.
const DefaultAckWait = 30 * time.Second
DefaultAckWait is how long a consumer has to ack a delivered message.
const DefaultDuplicateWindow = 2 * time.Minute
DefaultDuplicateWindow is the JetStream publisher-side deduplication window.
const DefaultMaxDeliver = 5
DefaultMaxDeliver is 5 failed deliveries before an event moves to the DLQ.
const DefaultRetention = 7 * 24 * time.Hour
DefaultRetention is 7 days, per the event bus spec.
const MaxSubjectTokens = 6
MaxSubjectTokens is the longest event subject the OUTPOST stream stores.
Variables ¶
This section is empty.
Functions ¶
func ConsumerName ¶
ConsumerName returns the deterministic JetStream durable consumer name for app's subscription to filterSubject.
func MatchSubject ¶
MatchSubject reports whether a concrete NATS subject matches a NATS pattern (supporting single-token '*' and trailing multi-token '>').
func ValidateAppName ¶
ValidateAppName reports whether app can be given a place on the bus. Its name becomes a subject token in outpost.<app>.> and a file name under creds/, so a dot, a wildcard or a path separator in it would reach into another app's subjects or out of that directory; and outpost.dlq.* is the dead-letter queue's.
func ValidatePattern ¶
ValidatePattern checks a subject filter an operator asked to watch, such as outpost.> or outpost.*.notification.>: it has to stay under outpost., with whole tokens, '*' and a trailing '>' only.
func ValidateSubject ¶
ValidateSubject checks that subject is a valid concrete Outpost event subject (outpost.<app>.<object>.<action>[...]) and not in the reserved DLQ namespace.
Types ¶
type AppDecl ¶
AppDecl declares an app's bus subscriptions for credential minting and durable consumer provisioning.
type ConsumerInfo ¶
type ConsumerInfo struct {
Stream string `json:"stream"`
Name string `json:"name"`
App string `json:"app,omitempty"`
FilterSubject string `json:"filter_subject"`
Delivered uint64 `json:"delivered"`
AckFloor uint64 `json:"ack_floor"`
NumAckPending int `json:"num_ack_pending"`
NumPending uint64 `json:"num_pending"`
NumRedelivered int `json:"num_redelivered"`
NumWaiting int `json:"num_waiting"`
MaxDeliver int `json:"max_deliver"`
Created time.Time `json:"created"`
}
ConsumerInfo summarizes a durable JetStream consumer and its current lag.
type DLQEntry ¶
type DLQEntry struct {
ID string `json:"id"`
Sequence uint64 `json:"sequence"`
App string `json:"app"`
Consumer string `json:"consumer"`
OriginalSubject string `json:"original_subject"`
OriginalSeq uint64 `json:"original_seq"`
Deliveries uint64 `json:"deliveries"`
FailedAt time.Time `json:"failed_at"`
Event Event `json:"event"`
Raw string `json:"raw,omitempty"`
}
DLQEntry is one dead-lettered event stored in OUTPOST_DLQ under outpost.dlq.<app>.
type Event ¶
type Event struct {
SpecVersion string `json:"specversion"`
ID string `json:"id"`
Source string `json:"source"`
Type string `json:"type"`
Time time.Time `json:"time"`
Subject string `json:"subject,omitempty"`
DataContentType string `json:"datacontenttype,omitempty"`
Data json.RawMessage `json:"data,omitempty"`
OutpostUser string `json:"outpostuser,omitempty"`
Extensions map[string]any `json:"-"`
}
Event is a CloudEvents 1.0 envelope in JSON structured mode.
func (Event) MarshalJSON ¶
MarshalJSON encodes Event in CloudEvents 1.0 JSON structured mode, preserving any extra extension attributes at the top level.
func (*Event) UnmarshalJSON ¶
UnmarshalJSON decodes a CloudEvents 1.0 JSON structured payload, keeping any unknown extension attributes in Extensions.
type EventQuery ¶
type EventQuery struct {
Stream string `json:"stream,omitempty"`
Subject string `json:"subject,omitempty"`
User string `json:"user,omitempty"`
Type string `json:"type,omitempty"`
Contains string `json:"contains,omitempty"`
Since time.Time `json:"since,omitzero"`
Until time.Time `json:"until,omitzero"`
Limit int `json:"limit,omitempty"`
}
EventQuery filters browsed events.
type EventRecord ¶
type EventRecord struct {
Stream string `json:"stream,omitempty"`
Sequence uint64 `json:"sequence,omitempty"`
Subject string `json:"subject"`
PublishedAt time.Time `json:"published_at"`
Event Event `json:"event"`
Raw string `json:"raw,omitempty"`
}
EventRecord is a single event read from a stream or live tail.
type Options ¶
type Options struct {
// DataDir is where JetStream storage, NKey seeds, and app .creds files live.
DataDir string
// Listen is host:port for NATS client connections (default 127.0.0.1:47904).
// Use "127.0.0.1:0" or ":0" for an ephemeral port in tests.
Listen string
// Retention is how long streams retain events (default 7 days).
Retention time.Duration
// MaxDeliver is how many failed deliveries move an event to the DLQ (default 5).
MaxDeliver int
// AckWait is how long consumers have to ack an event before redelivery (default 30s).
AckWait time.Duration
// Logf receives operational log lines.
Logf func(format string, v ...any)
}
Options configures the embedded event bus.
type Overview ¶
type Overview struct {
URL string `json:"url"`
Running bool `json:"running"`
Streams []StreamInfo `json:"streams"`
}
Overview is the bus status and its streams.
type PublishRequest ¶
type PublishRequest struct {
Subject string `json:"subject"`
Source string `json:"source,omitempty"`
Type string `json:"type,omitempty"`
SubjectID string `json:"subject_id,omitempty"`
User string `json:"user,omitempty"`
Data json.RawMessage `json:"data,omitempty"`
}
PublishRequest is the input to Publish for test or operator events.
type PublishResult ¶
type PublishResult struct {
ID string `json:"id"`
Subject string `json:"subject"`
Stream string `json:"stream"`
Sequence uint64 `json:"sequence"`
Event Event `json:"event"`
}
PublishResult is the confirmation returned by Publish.
type ReplayResult ¶
type ReplayResult struct {
DLQID string `json:"dlq_id"`
EventID string `json:"event_id"`
App string `json:"app"`
Subject string `json:"subject"`
Sequence uint64 `json:"sequence"`
}
ReplayResult describes a replayed dead-letter entry.
type Service ¶
type Service struct {
// contains filtered or unexported fields
}
Service owns the embedded NATS server, JetStream streams, per-app credentials, durable consumers, and dead-letter router. It is safe for concurrent use.
func Open ¶
Open starts the embedded NATS server with JetStream, initializes the OUTPOST and OUTPOST_DLQ streams, and starts the dead-letter advisory router.
func (*Service) AdminConn ¶
AdminConn returns the daemon's internal NATS connection for built-in subscribers such as the notification service.
func (*Service) Close ¶
Close stops the DLQ router, closes the internal NATS client, and shuts down the embedded NATS server cleanly.
func (*Service) DLQList ¶
DLQList returns dead-letter entries, optionally filtered to one app, newest first.
func (*Service) DLQReplay ¶
DLQReplay republishes a dead-letter entry back onto its original subject on OUTPOST and removes it from OUTPOST_DLQ. id may be the DLQ entry ID, the CloudEvents event ID, or the DLQ stream sequence number.
func (*Service) EnsureApp ¶
func (s *Service) EnsureApp(ctx context.Context, app string, subscribes []string) (credsPath string, rev string, err error)
EnsureApp ensures credentials and durable consumers exist for a single app.
func (*Service) EnsureInternalConsumer ¶
func (s *Service) EnsureInternalConsumer(ctx context.Context, owner, consumerName, filterSubject string) (jetstream.Consumer, error)
EnsureInternalConsumer creates or updates a durable pull consumer on OUTPOST for an internal Outpost component (such as the shared push notification service), returning the consumer handle.
func (*Service) Events ¶
func (s *Service) Events(ctx context.Context, q EventQuery) ([]EventRecord, error)
Events browses and searches stored events in a stream (default OUTPOST), returning newest matching events first.
func (*Service) Publish ¶
func (s *Service) Publish(ctx context.Context, req PublishRequest) (PublishResult, error)
Publish publishes a CloudEvents 1.0 event to the bus on behalf of an operator or test tool, filling standard CloudEvents defaults where omitted.
func (*Service) Resolve ¶
Resolve returns the OUTPOST_BUS_URL, OUTPOST_BUS_CREDS, and OUTPOST_APP environment variables for service, matching supervisor.Secrets.Resolve. If SyncApps hasn't seen service yet, it provisions default publish-only creds.
func (*Service) Streams ¶
func (s *Service) Streams(ctx context.Context) ([]StreamInfo, error)
Streams lists every JetStream stream on the Outpost bus.
func (*Service) SyncApps ¶
SyncApps reconciles per-app NATS credentials and durable JetStream consumers to match the declared app manifests. Apps removed from decls have their old user keys revoked and creds files removed.
type StreamInfo ¶
type StreamInfo struct {
Name string `json:"name"`
Description string `json:"description,omitempty"`
Subjects []string `json:"subjects"`
Messages uint64 `json:"messages"`
Bytes uint64 `json:"bytes"`
FirstSeq uint64 `json:"first_seq"`
LastSeq uint64 `json:"last_seq"`
FirstTime time.Time `json:"first_time,omitzero"`
LastTime time.Time `json:"last_time,omitzero"`
Consumers int `json:"consumers"`
MaxAge time.Duration `json:"max_age"`
}
StreamInfo summarizes a JetStream stream's configuration and live state.