fix(nats): bound the ARCHIE_REACTIONS stream by age

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.
This commit is contained in:
sam committed 2026-09-19 02:16:38 +10:00
1 parent 33c6be185a
commit 8a5e99832f
4 files changed
+100 -32

No files matched your search

@@ -59,13 +59,20 @@ func Connect(ctx context.Context, cfg Config, log *slog.Logger) (*Client, error)
return nil, fmt.Errorf("nats jetstream init: %w", err)
}
stream, err := js.CreateOrUpdateStream(ctx, jetstream.StreamConfig{
streamConfig := jetstream.StreamConfig{
Name: cfg.StreamName,
Subjects: cfg.Subjects,
Storage: jetstream.FileStorage,
Retention: *cfg.Retention,
Duplicates: cfg.DedupWindow,
})
}
// Limits are applied only when the composition explicitly sets them;
// leaving MaxAge nil keeps the stream unbounded exactly as before.
if cfg.MaxAge != nil {
streamConfig.MaxAge = *cfg.MaxAge
}
stream, err := js.CreateOrUpdateStream(ctx, streamConfig)
if err != nil {
return nil, fmt.Errorf("nats create stream %q: %w", cfg.StreamName, err)
}
@@ -63,6 +63,16 @@ type Config struct {
// (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
@@ -92,7 +102,7 @@ func (c Config) Validate() error {
if c.FilterSubject == "" {
return fmt.Errorf("%w: FilterSubject is required", ErrInvalidConfig)
}
if c.PollTimeout < 0 || c.AckWait < 0 || c.DedupWindow < 0 {
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 {