Draft
Conversation
Token bucket rate limiter that rejects new connections exceeding configurable global and per-IP thresholds by closing the TCP socket before the AMQP/MQTT handshake, avoiding CPU waste. Config options: connection_rate_limit, connection_rate_limit_per_ip (both default to 0/unlimited).
This comment was marked as resolved.
This comment was marked as resolved.
Check per-IP limit before global limit so a rejected per-IP connection doesn't consume a global token unfairly. Cap tracked IPs at 100k to prevent unbounded hash growth from large numbers of unique source IPs.
This comment was marked as resolved.
This comment was marked as resolved.
carlhoerberg
reviewed
Mar 2, 2026
- Remove pointer-based consume_token, modify instance vars directly - Evict oldest entry instead of rejecting when IP table is full - Rate-limit the table-full log warning - Remove dead @log_suppressed field
This comment was marked as resolved.
This comment was marked as resolved.
- Merge @per_ip_tokens and @per_ip_last_refill into a single hash with a PerIPState record to eliminate sync risk - Use Hash#first_key for O(1) eviction instead of min_by O(n) scan - Document the per-IP token consumption trade-off in allow? comment
This comment was marked as outdated.
This comment was marked as outdated.
Guard all mutable state with a Mutex so the limiter is safe when listener fibers and the stats_loop fiber run on separate OS threads under preview_mt.
|
Code Review No issues found. The implementation is solid:
|
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Summary
connection_rate_limit(global max new connections/sec) andconnection_rate_limit_per_ip(per source IP), both default to 0 (unlimited)Motivation
When LavinMQ is CPU-saturated, handshake timeouts cause clients to disconnect and immediately reconnect, creating a death spiral. Each reconnection goes through the full AMQP handshake (confirm_header, start, authenticate, tune, open), consuming CPU that should serve existing connections. Rejecting at the TCP level is orders of magnitude cheaper.
Test plan
make test SPEC=spec/connection_rate_limit_spec.cr(11 specs)connection_rate_limit = 10, connect many clients, verify excess connections are rejected immediately