Skip to content

feat: add opt-in automatic recovery for promise and callback APIs#833

Open
ShiriNmi1520 wants to merge 6 commits intoamqp-node:mainfrom
ShiriNmi1520:recoveryConnection
Open

feat: add opt-in automatic recovery for promise and callback APIs#833
ShiriNmi1520 wants to merge 6 commits intoamqp-node:mainfrom
ShiriNmi1520:recoveryConnection

Conversation

@ShiriNmi1520
Copy link

Summary

This PR adds a generic, opt-in recovery mechanism to amqplib for both public APIs:

  • Promise API (require('amqplib'))
  • Callback API (require('amqplib/callback_api'))

Recovery is enabled only when connect(..., { recovery: ... }) is provided, so the default behaviour remains unchanged.

Motivation

Fix #25

What changed

  • Added shared recovery engine: lib/recovery.js
  • Wired recovery into:
    • channel_api.js
    • callback_api.js
  • Added docs in README.md for usage/options/events
  • Added unit tests in test/recovery.js

Recovery options

  • initialDelay
  • maxDelay
  • factor
  • jitter
  • maxRetries (also accepts maxAttempts)
  • setup(model, client[, done]) called after each successful connect/reconnect

Mermaid sequence chart

sequenceDiagram
    autonumber
    participant App
    participant Client as amqplib (recovering client)
    participant Broker as RabbitMQ

    App->>Client: connect(url, { recovery })
    Client->>Broker: open TCP + AMQP handshake
    Broker-->>Client: open-ok
    Client->>Client: run recovery.setup(...)
    Client-->>App: emit "connect"

    Note over Broker,Client: normal operation

    Broker-x Client: socket/connection closed
    Client-->>App: emit "disconnect"
    Client-->>App: emit "reconnectScheduled"

    loop retry with backoff+jitter
        Client->>Broker: reconnect + handshake
        alt success
            Broker-->>Client: open-ok
            Client->>Client: run recovery.setup(...)
            Client-->>App: emit "connect"
        else failed and retries remain
            Client-->>App: emit "connectFailed"
            Client-->>App: emit "reconnectScheduled"
        end
    end

    alt retries exhausted
        Client-->>App: emit "reconnectFailed"
    end
Loading

Backward compatibility

  • No behavior change unless recovery is explicitly configured.

Notes / limitations

  • This provides connection-level recovery and setup replay.
  • It does not guarantee transparent replay of in-flight channel operations across disconnects.

@ShiriNmi1520 ShiriNmi1520 marked this pull request as draft February 13, 2026 07:59
@ShiriNmi1520 ShiriNmi1520 marked this pull request as ready for review February 13, 2026 08:03
Signed-off-by: Ivan <[email protected]>
@cressie176
Copy link
Collaborator

Thank you for this @ShiriNmi1520, it looks like a very comprehensive piece of work. I've have some time off next week and will review.

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.

Support Auto-reconnection

2 participants

Comments