Skip to content

feat(microservices): add maxConnectionAttempts to ServerMqtt - #17731

Closed
Jia-ben00 wants to merge 1 commit into
nestjs:masterfrom
Jia-ben00:feat/mqtt-max-connection-attempts
Closed

Jia-ben00 wants to merge 1 commit into
nestjs:masterfrom
Jia-ben00:feat/mqtt-max-connection-attempts

Conversation

@Jia-ben00

Copy link
Copy Markdown

PR Checklist

Please check if your PR fulfills the following requirements:

PR Type

What kind of change does this PR introduce?

  • Bugfix
  • Feature
  • Code style update (formatting, local variables)
  • Refactoring (no functional changes, no api changes)
  • Build related changes
  • CI related changes
  • Other... Please describe:

What is the current behavior?

ServerMqtt has no way to bound startup retries. When the MQTT broker is unavailable, listen() waits forever and the microservice never fails fast — unlike ServerRMQ, which supports maxConnectionAttempts and reports the failure to the bootstrap callback once the limit is exhausted.

Issue Number: #17674

What is the new behavior?

Adds a maxConnectionAttempts option to ServerMqtt, mirroring the existing ServerRMQ behavior:

app.connectMicroservice<MicroserviceOptions>({
  transport: Transport.MQTT,
  options: {
    url: 'mqtt://localhost:1883',
    maxConnectionAttempts: 3,
  },
});

When the broker is unreachable and the number of connection failures reaches maxConnectionAttempts, the server closes the client and rejects app.listen() with the underlying error (falling back to CONNECTION_FAILED_MESSAGE). Default remains -1 (infinite retries), preserving the current behavior.

Does this PR introduce a breaking change?

  • Yes
  • No

Other information

Implementation mirrors ServerRMQ:

  • INFINITE_CONNECTION_ATTEMPTS = -1 and connectionAttempts counter
  • the error listener increments the counter and, once the limit is reached, calls this.close() and the listen() callback

Verification:

  • npx vitest run packages/microservices — 63 files, 772 tests passed
  • npx oxlint on changed files — 0 errors
  • npx tsc -b packages/microservices — clean

@kamilmysliwiec

Copy link
Copy Markdown
Member

lets track this here #17734

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.

2 participants