Unified connection & request security gate (IP-level + fd-level, multi-strategy limiting + blacklist).
More...
|
| | ConnectionLimiter (const int &maxConn=20, const int &idleTimeout=60) |
| | Constructor. More...
|
| |
| void | setConnectStrategy (const RateLimitType &type) |
| | Set the strategy used for connection-rate limiting. More...
|
| |
| void | setRequestStrategy (const RateLimitType &type) |
| | Set the strategy used for fd-level request limiting. More...
|
| |
| void | setPathStrategy (const RateLimitType &type) |
| | Set the strategy used for path-level extra limiting. More...
|
| |
| void | setPathLimit (const std::string &path, const int ×, const int &secs) |
| | Configure extra rate limits for a specific path (path-level rule). More...
|
| |
| DefenseDecision | allowConnect (const std::string &ip, const int &fd, const int ×, const int &secs) |
| | Security decision for a newly accepted connection (IP-level gate). More...
|
| |
| DefenseDecision | allowRequest (const std::string &ip, const int &fd, const std::string_view &path, const int ×, const int &secs) |
| | Security decision for a single request on an existing connection. More...
|
| |
| void | clearIP (const std::string &ip, const int &fd) |
| | Reclaim state for an fd when the connection is closed. More...
|
| |
| bool | connectionDetect (const std::string &ip, const int &fd) |
| | Detect and cleanup an idle/zombie connection. More...
|
| |
Unified connection & request security gate (IP-level + fd-level, multi-strategy limiting + blacklist).
ConnectionLimiter is a "Security Gate". All connections and requests must pass through it before business logic runs.
This class does NOT directly perform side effects (close/send/sleep). Instead, it returns a DefenseDecision for the caller to enforce.
Design Overview
Layered defense model:
- IP-level defense:
- Concurrent connection limit (maxConnections)
- Connection rate limit (connectRate)
- IP risk scoring (badScore)
- Temporary blacklist with TTL (blacklist)
- fd-level defense:
- Per-connection request rate (requestRate)
- Per-path extra limits (pathRate)
- Activity tracking (lastActivity) for zombie detection
Decision Semantics
allowConnect / allowRequest return DefenseDecision:
- ALLOW (0): proceed
- DROP (1): ignore silently (request stage only)
- CLOSE (2): close immediately (may escalate / ban)
- Note
- Connect stage usually uses only ALLOW / CLOSE.
- DROP is mainly used in request stage.
Strategies
Supported algorithms (see RateLimitType):
- Cooldown
- FixedWindow
- SlidingWindow
- TokenBucket
Defaults:
- connectStrategy: Cooldown
- requestStrategy: SlidingWindow
- pathStrategy: SlidingWindow
Thread Safety
- Warning
- This class is NOT thread-safe by itself. Concurrent access to internal tables (table/pathConfig/blacklist) must be protected externally (e.g. single event-loop thread, or a mutex).
Lifecycle
- allowConnect:
- Decide whether a new connection is allowed
- On ALLOW, register the fd under the IP
- allowRequest:
- Decide whether a request on an existing fd is allowed
- clearIP:
- Called when a connection is closed, to reclaim state and counters
- connectionDetect:
- Detect and cleanup idle/zombie connections (should be called by a timer)