Wire github.com/nats-io/nats-server/v2 into production startup so a single-process deployment gets reaction delivery and task distribution over an in-process server rather than a separate one (docs/prds/embedded-nats.md). Meaning change: [nats].url empty used to mean "no NATS, use the SQLite ClaimNext flow"; it now means "start an embedded NATS server". The old behaviour is the explicit opt-out [nats].mode = "off". mode resolves from url when unset (url set -> external, url empty -> embedded), and agent.mode="nats" / [containers] now require mode="external" (an embedded loopback server cannot be reached by agent workers or containers). The daemon owns the embedded server's lifecycle: StartEmbedded before dialing the client, Shutdown after the client closes (LIFO cleanups). The embedded server binds loopback on a random port, enables JetStream in a store dir under the data directory, and leaves its logger nil so a startup failure surfaces as a Connect error rather than an os.Exit. Retention becomes a nats.Config field (default WorkQueuePolicy) so a future reaction stream can fan out under LimitsPolicy instead of the hardcoded competing-consumer policy that starves a second consumer on overlapping subjects.
130 lines
4.1 KiB
Go
130 lines
4.1 KiB
Go
package nats
|
|
|
|
import (
|
|
"fmt"
|
|
"time"
|
|
|
|
"github.com/nats-io/nats.go/jetstream"
|
|
)
|
|
|
|
// Defaults for Config. Previously these were unexported package constants,
|
|
// which made stream naming and timeouts untunable per deployment.
|
|
const (
|
|
DefaultStreamName = "ARCHIE_TASKS"
|
|
DefaultConsumerName = "archie-daemon"
|
|
DefaultDedupWindow = 2 * time.Minute
|
|
DefaultPollTimeout = 2 * time.Second
|
|
DefaultAckWait = 5 * time.Minute
|
|
DefaultMaxDeliver = 3
|
|
DefaultInactiveTTL = 24 * time.Hour
|
|
)
|
|
|
|
// Config describes how to reach NATS and how the stream and consumer should
|
|
// behave. The zero value is not usable; call [Config.withDefaults] via
|
|
// [Connect], which fills every unset field.
|
|
type Config struct {
|
|
// URL is the NATS server address. Required.
|
|
URL string
|
|
|
|
// Token authenticates the connection. Empty means no token auth.
|
|
Token string
|
|
|
|
// StreamName is the JetStream stream holding the bound subjects.
|
|
StreamName string
|
|
|
|
// Subjects are the subjects the stream carries. Required.
|
|
//
|
|
// Composition supplies these rather than the package hardcoding them: the
|
|
// bus must not know which subjects belong to which domain, and archied
|
|
// and archie-agent previously declared the same stream separately, free
|
|
// to disagree about its subjects, retention and dedup window.
|
|
Subjects []string
|
|
|
|
// ConsumerName is the durable pull consumer Fetch reads from.
|
|
ConsumerName string
|
|
|
|
// FilterSubject restricts that consumer to a subset of the stream --
|
|
// archied consumes task subjects, archie-agent consumes agent subjects.
|
|
// Required.
|
|
FilterSubject string
|
|
|
|
// Retention is the stream's retention policy. Nil defaults to
|
|
// jetstream.WorkQueuePolicy (each message claimed by exactly one
|
|
// consumer), which is correct for ARCHIE_TASKS task distribution. A
|
|
// fan-out reaction stream passes jetstream.LimitsPolicy so every
|
|
// registered consumer sees every matching event; under WorkQueuePolicy a
|
|
// second consumer on an overlapping filter subject silently receives
|
|
// nothing (the trap documented in CLAUDE.md). The pointer form is what
|
|
// lets "unset" default to WorkQueuePolicy when the policy's zero value
|
|
// (LimitsPolicy) is a valid explicit choice.
|
|
Retention *jetstream.RetentionPolicy
|
|
|
|
// DedupWindow is how long JetStream remembers a Nats-Msg-Id, suppressing
|
|
// republished duplicates of the same issue within the window.
|
|
DedupWindow time.Duration
|
|
|
|
// PollTimeout bounds a single Fetch. On expiry Fetch reports ErrNoMessage.
|
|
PollTimeout time.Duration
|
|
|
|
// AckWait is how long a delivered message may go unacknowledged before
|
|
// JetStream redelivers it. It must exceed the longest stage runtime.
|
|
AckWait time.Duration
|
|
|
|
// MaxDeliver caps redelivery attempts before a message is dropped.
|
|
MaxDeliver int
|
|
|
|
// InactiveTTL removes the consumer after this much inactivity.
|
|
InactiveTTL time.Duration
|
|
}
|
|
|
|
// Validate reports whether the config can produce a usable client.
|
|
func (c Config) Validate() error {
|
|
if c.URL == "" {
|
|
return fmt.Errorf("%w: URL is required", ErrInvalidConfig)
|
|
}
|
|
if len(c.Subjects) == 0 {
|
|
return fmt.Errorf("%w: Subjects is required", ErrInvalidConfig)
|
|
}
|
|
if c.FilterSubject == "" {
|
|
return fmt.Errorf("%w: FilterSubject is required", ErrInvalidConfig)
|
|
}
|
|
if c.PollTimeout < 0 || c.AckWait < 0 || c.DedupWindow < 0 {
|
|
return fmt.Errorf("%w: durations must not be negative", ErrInvalidConfig)
|
|
}
|
|
if c.MaxDeliver < 0 {
|
|
return fmt.Errorf("%w: MaxDeliver must not be negative", ErrInvalidConfig)
|
|
}
|
|
return nil
|
|
}
|
|
|
|
// withDefaults returns a copy with every unset field populated, so the rest of
|
|
// the package never has to test for zero values.
|
|
func (c Config) withDefaults() Config {
|
|
if c.StreamName == "" {
|
|
c.StreamName = DefaultStreamName
|
|
}
|
|
if c.ConsumerName == "" {
|
|
c.ConsumerName = DefaultConsumerName
|
|
}
|
|
if c.DedupWindow == 0 {
|
|
c.DedupWindow = DefaultDedupWindow
|
|
}
|
|
if c.PollTimeout == 0 {
|
|
c.PollTimeout = DefaultPollTimeout
|
|
}
|
|
if c.AckWait == 0 {
|
|
c.AckWait = DefaultAckWait
|
|
}
|
|
if c.MaxDeliver == 0 {
|
|
c.MaxDeliver = DefaultMaxDeliver
|
|
}
|
|
if c.InactiveTTL == 0 {
|
|
c.InactiveTTL = DefaultInactiveTTL
|
|
}
|
|
if c.Retention == nil {
|
|
policy := jetstream.WorkQueuePolicy
|
|
c.Retention = &policy
|
|
}
|
|
return c
|
|
}
|