Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Add mqtt.max_connections per-node connection limit#16367

Merged
michaelklishin merged 3 commits into
mainfrom
feature/gh-16347-mqtt-connections-max
May 12, 2026
Merged

Add mqtt.max_connections per-node connection limit#16367
michaelklishin merged 3 commits into
mainfrom
feature/gh-16347-mqtt-connections-max

Conversation

@lukebakken
Copy link
Copy Markdown
Collaborator

@lukebakken lukebakken commented May 9, 2026

Closes #16347 (partial - adds mqtt.max_connections).

What this PR does

Adds mqtt.max_connections, a per-node connection limit for the MQTT plugin. When the limit is reached, the broker sends a CONNACK with return code not_authorized (MQTT 3.x/4) or reason code quota_exceeded (MQTT 5) and closes the connection.

How it works

The limit is checked in rabbit_mqtt_processor:process_connect/5 as the first step in the CONNECT packet handler, before authentication. It does not operate at the Ranch transport layer, so the TCP listen queue is unaffected and Ranch continues accepting connections normally.

The active connection count uses ets:info(persistent_term:get(?PG_SCOPE), size). The MQTT plugin creates a node-local PG scope in rabbit_mqtt_sup; each connection joins it via pg:join/3 in register_client_id/4, using {VHost, ClientId} as the group key. Since the MQTT spec requires unique client IDs per vhost, which RabbitMQ enforces, each group has exactly one member. The PG scope ETS table therefore has one row per active connection node-wide, covering all listeners and transports (plain TCP, TLS, and Web MQTT). ets:info/2 reads this count in O(1) - important given that MQTT nodes can host a very large number of connections.

The check runs before register_client_id/4, so the current connection is not yet counted; the limit comparison is >= max_connections.

Configuration

mqtt.max_connections = 1000

The default is infinity (no limit). The setting accepts a non-negative integer or infinity.

Testing

Adds node_connection_limit to the limit group in auth_SUITE. The test sets the limit to 0 via RPC so the first CONNECT attempt is rejected, covering both MQTT v4 and v5 via the existing expected_connection_limit_error/1 helper.

Copy link
Copy Markdown
Member

@ansd ansd left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

If mqtt.max_connections is set, does the limit as proposed in your PR work correctly in any of the following cases:

Because Ranch creates independent connection supervisors for every unique protocol, IP family, and port combination, ranch:info(RanchRef) only counts connections for that specific listener. In the scenarios above, the limit will multiply rather than act as a global node limit.

Additionally, the MQTT implementation in RabbitMQ is designed to scale to over 1 million concurrent connections per node. The connection limit check should be done as efficiently as possible.

To summarise, just following the same pattern for MQTT as done for Stream connections may not be sufficient. MQTT works differently because:

  1. Connections can be mapped to vhosts by port.
  2. MQTT use cases typically need to handle an order of magnitude more connections than Stream use cases.

My idea is to use the following to query the current number of MQTT connections on a given node:

ets:info(persistent_term:get(?PG_SCOPE), size).

This approximation should be good enough since the MQTT spec defines:

Each Client connecting to the Server has a unique ClientID.

RabbitMQ enforces this. This solution is more correct globally and more efficient. What do you think?

@lukebakken lukebakken force-pushed the feature/gh-16347-mqtt-connections-max branch 3 times, most recently from baa588d to 17ae4f2 Compare May 11, 2026 15:17
@lukebakken lukebakken requested a review from ansd May 11, 2026 15:33
Copy link
Copy Markdown
Member

@ansd ansd left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thank you @lukebakken !
Your new approach is simpler, more efficient, and more correct.

@michaelklishin
Copy link
Copy Markdown
Collaborator

michaelklishin commented May 11, 2026

The test coverage of this feature is very limited, I will add a few more basic tests.

lukebakken and others added 2 commits May 11, 2026 16:49
Add the `mqtt.max_connections` configuration key, which sets a
node-wide limit on the number of concurrent MQTT connections.

The limit is checked in `rabbit_mqtt_processor:process_connect/5`
as the first step in the CONNECT packet handler, before
authentication. When exceeded, the broker returns a CONNACK with
reason code `quota_exceeded` (MQTT 5) or return code
`not_authorized` (MQTT 3.x/4).

The active connection count is obtained via
`ets:info(persistent_term:get(?PG_SCOPE), size)`. The MQTT plugin
creates a node-local PG scope in `rabbit_mqtt_sup`; each connection
joins it via `pg:join/3` in `register_client_id/4`, using
`{VHost, ClientId}` as the group key. Since the MQTT spec requires
unique client IDs per vhost, which RabbitMQ enforces, each group
has exactly one member. The PG scope ETS table therefore has one
row per active connection node-wide, covering all listeners and
transports (plain TCP, TLS, and Web MQTT). `ets:info/2` reads this
count in O(1) - important given that MQTT nodes can host a very
large number of connections.

The check runs before `register_client_id/4`, so the current
connection is not yet counted; the limit comparison is `>= Limit`.

When `mqtt.max_connections` is absent, `application:get_env/3`
returns `infinity` and no check is performed.

A `node_connection_limit` test is added to the `limit` group in
`auth_SUITE`.
@michaelklishin michaelklishin force-pushed the feature/gh-16347-mqtt-connections-max branch from 6aaa47a to 7584804 Compare May 11, 2026 23:58
@michaelklishin michaelklishin force-pushed the feature/gh-16347-mqtt-connections-max branch from 7584804 to a4d56ad Compare May 12, 2026 00:12
@michaelklishin michaelklishin merged commit dce464a into main May 12, 2026
360 of 361 checks passed
@michaelklishin michaelklishin deleted the feature/gh-16347-mqtt-connections-max branch May 12, 2026 03:25
Comment thread deps/rabbitmq_web_mqtt/test/web_mqtt_command_SUITE.erl
michaelklishin added a commit that referenced this pull request May 12, 2026
(cherry picked from commit 1291866)
michaelklishin added a commit that referenced this pull request May 12, 2026
pull Bot pushed a commit to skaschimer/rabbitmq-server that referenced this pull request May 12, 2026
michaelklishin added a commit that referenced this pull request May 13, 2026
Add `mqtt.max_connections` per-node connection limit (backport #16367)
michaelklishin added a commit that referenced this pull request May 13, 2026
Add `mqtt.max_connections` per-node connection limit (backport #16367) (backport #16391)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Improve connection limits, introduce per-protocol connection limits

3 participants