Review fix for PR #875. ARCHIE_REACTIONS is provisioned under LimitsPolicy, which retains every message until a limit is reached. With no limit configured, acknowledged reactions accumulated without bound and continuous traffic could grow the stream until it exhausted disk. nats.Config gains MaxAge *time.Duration, using the same pointer form as Retention so "unset" (nil) is distinguishable from an explicit zero. It is applied to the stream config only when set, so every existing caller keeps its current behaviour. ARCHIE_REACTIONS sets MaxAge = 24h. A reaction is a producer-only wake event whose authoritative state is re-read at pass time, so a dropped reaction delays work until the next poll (default 60s) rather than losing it; 24h tolerates a consumer down for a full maintenance window and matches the package's existing DefaultInactiveTTL. ARCHIE_TASKS is deliberately left unbounded. It uses WorkQueuePolicy, which discards a message on acknowledgement, so its size is bounded by outstanding work; a limit there could discard a task that has not been delivered yet, which is a worse failure than the disk risk. The table asserts that non-change.
156 lines
5.5 KiB
Go
156 lines
5.5 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"
|
|
// DefaultReactionStreamName is the fan-out stream carrying reaction
|
|
// subjects. It coexists with DefaultStreamName (ARCHIE_TASKS), which
|
|
// stays work-queue for task distribution.
|
|
DefaultReactionStreamName = "ARCHIE_REACTIONS"
|
|
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
|
|
|
|
// MaxAge is the maximum age of messages the stream retains. Nil leaves
|
|
// the stream unbounded by age, which is correct for ARCHIE_TASKS: under
|
|
// WorkQueuePolicy acked messages are discarded, so its only bound is
|
|
// outstanding work. The pointer form distinguishes "unset" (nil,
|
|
// unbounded, today's behaviour) from an explicit zero (also unbounded,
|
|
// but expressed on purpose), exactly like Retention. The fan-out
|
|
// reaction stream sets a finite MaxAge so acknowledged reactions cannot
|
|
// accumulate forever.
|
|
MaxAge *time.Duration
|
|
|
|
// 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 || (c.MaxAge != nil && *c.MaxAge < 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
|
|
}
|
|
|
|
// FanOutRetention returns a pointer to jetstream.LimitsPolicy for use as
|
|
// Config.Retention. It exists so composition can select fan-out retention
|
|
// without importing the NATS SDK: the retention policy type stays behind this
|
|
// package's boundary, like every other JetStream type. LimitsPolicy is the one
|
|
// policy that cannot be expressed by leaving Retention unset (withDefaults
|
|
// treats nil as WorkQueuePolicy), so the choice must be visible at the call
|
|
// site.
|
|
func FanOutRetention() *jetstream.RetentionPolicy {
|
|
policy := jetstream.LimitsPolicy
|
|
return &policy
|
|
}
|