Skip to content

fix: Recover NATS receiver after broker outages - #266

Merged
ecv merged 1 commit into
mainfrom
fix/natsreceiver-self-recovery
Nov 9, 2026
Merged

ecv merged 1 commit into
mainfrom
fix/natsreceiver-self-recovery

Conversation

@ecv

@ecv ecv commented Nov 9, 2026 •

Copy link
Copy Markdown
Contributor

After a NATS broker outage of more than about two minutes, the NATS receiver stopped consuming for good while the collector kept reporting healthy, so logs piled up until someone restarted the pod by hand. Now the receiver keeps reconnecting until the brokers return and resumes by itself. If the connection still closes, or the stream consumer is deleted or missing when the brokers come back, the collector exits so Kubernetes restarts it.

Test plan

  • Unit tests, vet and lint pass for the receiver module, with the race detector on
  • New tests cover unlimited reconnects, a closed connection, a deleted consumer, a consumer missing after the broker returns, an outage that must not exit, and a clean shutdown; the failure paths fail on the old code
  • After release, a staging broker replacement longer than two minutes leaves the receiver consuming without a restart

Fixes #265

A broker outage longer than nats.go's default reconnect window (60 tries,
2s apart) closed the connection for good. The receiver set no closed
handler and reported nothing, so it stopped consuming while the collector
stayed healthy. A consumer deleted on the server, or missing when the
broker came back, ended the same way.

Key features/changes:
- Reconnect without a limit, so a broker replacement of any length
  recovers by itself
- Report a fatal status to the collector, so it exits and Kubernetes
  restarts it, when the connection closes after a successful Start, when
  JetStream ends the consume with consumer deleted or bad request, or when
  a missed heartbeat finds the consumer or stream gone
- A lookup that fails for another reason, such as the broker still being
  down, stays a warning
- Install the closed handler at connect, and report nothing before Start
  succeeds or once Shutdown begins
- Close instead of drain when Shutdown runs mid-reconnect
- Add tests for unlimited reconnects, a closed connection, a deleted
  consumer, a consumer missing after the broker returns, an outage that
  must not report fatal, and a clean shutdown
@ecv ecv changed the title fix: recover the NATS receiver after long broker outages fix: Recover NATS receiver after broker outages Nov 9, 2026
@ecv
ecv force-pushed the fix/natsreceiver-self-recovery branch from 93c1901 to ac02cf8 Compare November 9, 2026 13:07
@ecv
ecv marked this pull request as ready for review November 9, 2026 13:20
@ecv
ecv requested a review from a team as a code owner November 9, 2026 13:20
@ecv
ecv requested a review from andrewb3000 November 9, 2026 13:20
@ecv
ecv enabled auto-merge November 9, 2026 13:20
@ecv

ecv commented Nov 9, 2026

Copy link
Copy Markdown
Contributor Author

Review loop: adversarial and conventions reviews returned merge. Agreed fixes applied at ac02cf8 (bad request and missing consumer now fatal, closed handler set at connect) and confirmed by re-review, including tamper tests. The conventions re-review could not run Go locally; CI covers it, 30 of 30 checks green. Residual risk: a newly elected JetStream leader might briefly report a consumer missing during a rolling restart, which would cost one extra collector restart. The staging check after release covers it.

@ecv
ecv merged commit b7e4d2e into main Nov 9, 2026
30 checks passed
@ecv
ecv deleted the fix/natsreceiver-self-recovery branch November 9, 2026 13:22
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

NATS receiver stops consuming for good after a long broker outage

2 participants