Reliable MQTT auto-reconnect #1

Merged
zyberzero merged 3 commits from mqtt-auto-reconnect into main 2026-11-03 17:56:35 +00:00
Owner

Why

The service was seen staying disconnected from the broker. The old reconnect loop polled IsConnected every 5 s (hardcoded), never logged disconnects, and used MQTTnet's default 100 s connect timeout. The docs also pointed to WithReconnectDelay, which only exists on ManagedMqttClient.

What

  • Spec: new SPEC.md §5.2 Connection & reconnect, plus matching PLAN.md design and verification steps and IMPLEMENTATION.md increment 6.6.
  • Config: Mqtt.ReconnectBackoffSeconds (default [1, 2, 5, 15, 30]), KeepAliveSeconds (15) and ConnectTimeoutSeconds (10).
  • MqttPublisher:
    • Each disconnect is logged with its reason (e.g. SessionTakenOver, KeepAliveTimeout) and triggers a reconnect loop with capped backoff that resets on success.
    • Only the loop calls ConnectAsync, so connects never run concurrently.
    • An idle safety check covers missed disconnect events.
    • Queued publishes drain immediately after a reconnect.

Note

One likely cause of the original incident is a ClientId collision: a dev instance and the deployed instance both using frame-processor against the same broker would keep kicking each other off. The new disconnect log will show SessionTakenOver if so. If it does, use a distinct Mqtt__ClientId locally.

Testing

  • dotnet build and dotnet test pass (174 tests).
  • Startup validation still fails fast on bad Mqtt options.
  • Not yet run live (no Docker or broker was available): PLAN.md verification items 7, 7a and 7b (broker restart, half-open link, ClientId collision). IMPLEMENTATION.md 6.6 stays unchecked until they pass.

🤖 Generated with Claude Code

## Why The service was seen staying disconnected from the broker. The old reconnect loop polled `IsConnected` every 5 s (hardcoded), never logged disconnects, and used MQTTnet's default 100 s connect timeout. The docs also pointed to `WithReconnectDelay`, which only exists on `ManagedMqttClient`. ## What - **Spec:** new SPEC.md §5.2 *Connection & reconnect*, plus matching PLAN.md design and verification steps and IMPLEMENTATION.md increment 6.6. - **Config:** `Mqtt.ReconnectBackoffSeconds` (default `[1, 2, 5, 15, 30]`), `KeepAliveSeconds` (15) and `ConnectTimeoutSeconds` (10). - **MqttPublisher:** - Each disconnect is logged with its reason (e.g. `SessionTakenOver`, `KeepAliveTimeout`) and triggers a reconnect loop with capped backoff that resets on success. - Only the loop calls `ConnectAsync`, so connects never run concurrently. - An idle safety check covers missed disconnect events. - Queued publishes drain immediately after a reconnect. ## Note One likely cause of the original incident is a **ClientId collision**: a dev instance and the deployed instance both using `frame-processor` against the same broker would keep kicking each other off. The new disconnect log will show `SessionTakenOver` if so. If it does, use a distinct `Mqtt__ClientId` locally. ## Testing - `dotnet build` and `dotnet test` pass (174 tests). - Startup validation still fails fast on bad Mqtt options. - **Not yet run live** (no Docker or broker was available): PLAN.md verification items 7, 7a and 7b (broker restart, half-open link, ClientId collision). IMPLEMENTATION.md 6.6 stays unchecked until they pass. 🤖 Generated with [Claude Code](https://claude.com/claude-code)
zyberzero added 3 commits 2026-11-03 17:52:16 +00:00
Add SPEC.md §5.2 describing reconnect requirements (backoff, keep-alive
detection, disconnect logging, unique ClientId), the new Mqtt config
keys, the matching PLAN.md design and verification steps, and
IMPLEMENTATION.md increment 6.6. Also drop the stale WithReconnectDelay
reference, which only exists on ManagedMqttClient.

Co-Authored-By: Claude Opus 5.5 (1M context) <noreply@anthropic.com>
Co-Authored-By: Claude Opus 5.5 (1M context) <noreply@anthropic.com>
Replace the hardcoded 5 s IsConnected poll with a loop driven by
DisconnectedAsync (plus an idle safety check), retrying on the
ReconnectBackoffSeconds schedule. Disconnects are now logged with their
reason, and keep-alive and connect timeout come from config so dead
links are detected and connect attempts can't block for 100 s.

Co-Authored-By: Claude Opus 5.5 (1M context) <noreply@anthropic.com>
zyberzero merged commit 675d206234 into main 2026-11-03 17:56:35 +00:00
Sign in to join this conversation.
No Reviewers
No labels
1 Participants
Notifications
Due Date
No due date set.
Dependencies

No dependencies set.

Reference: zyberzero/frame-processor#1