Connection Limiter
On this page
- Source Mapping#
- 1. ConnectionGuard Defense Mechanisms#
- Burst & Short-Lived Connection Penalties (Connection.Guard.Slot.cs)#
- Progressive IP Banning & Decay#
- Zero-Allocation Subnet Tracking (Connection.Guard.Subnet.cs)#
- Bounded Endpoint Map (MaxTrackedEndpoints = 50,000)#
- 2. DatagramGuard (UdpListenerBase Protection)#
- Configuration Summary#
- Related APIs#
Namespace: Nalix.Network.RateLimiting
Assembly: Nalix.Network
Nalix.Network.RateLimiting provides two specialized transport admission limiters:
- ConnectionGuard (
src/Nalix.Network/RateLimiting/Connection.Guard.cs): Protects connection-oriented listeners (TcpListenerBase,WebSocketListenerBase, andUdpPassthroughListener). DatagramGuard(src/Nalix.Network/RateLimiting/Datagram.Guard.cs): ProtectsUdpListenerBaseagainst UDP packet floods using bounded IPv4/IPv6 sliding source windows.
Source Mapping
src/Nalix.Network/RateLimiting/Connection.Guard.cssrc/Nalix.Network/RateLimiting/Connection.Guard.Slot.cssrc/Nalix.Network/RateLimiting/Connection.Guard.Subnet.cssrc/Nalix.Network/RateLimiting/Connection.Guard.Cleanup.cssrc/Nalix.Network/RateLimiting/Datagram.Guard.cssrc/Nalix.Network/Options/ConnectionQuotaOptions.cssrc/Nalix.Network/Options/ConnectionGuardOptions.cssrc/Nalix.Network/Options/DatagramGuardOptions.cs
1. ConnectionGuard Defense Mechanisms
Burst & Short-Lived Connection Penalties (Connection.Guard.Slot.cs)
- Burst Detection: If consecutive connection attempts from the same IP arrive within
MinConnectionIntervalMs(50ms) at leastBurstThreshold(3) times in a row, the endpoint enters burst mode and its effectiveMaxConnectionsPerWindowceiling is divided byBurstPenaltyDivisor(2). - Short-Lived Connection Penalty: When a connection closes within
ShortLivedThresholdMs(2000ms) of opening,OnConnectionClosedinjects an extra synthetic timestamp into the endpoint’s rate window to penalize rapid connect/disconnect churn. - Load-Adaptive Tightening: When
EnableAdaptiveModeistrueand global load exceedsAdaptiveLoadThreshold(0.7), per-IP concurrency limits are multiplied byAdaptiveTighteningFactor(0.5).
Progressive IP Banning & Decay
When an endpoint exceeds its rate window:
- If
ConnectionGuardOptions.EnableProgressiveBanningistrue(default), consecutive violations escalate through1m$\rightarrow$5m$\rightarrow$15m$\rightarrow$1h$\rightarrow$6h$\rightarrow$24h. - Ban counts decay by
1perConnectionBanStoreOptions.BanCountDecayWindow(7days) of clean behavior and are persisted to"banned_ips.bin"byNetworkBanRepository.
Zero-Allocation Subnet Tracking (Connection.Guard.Subnet.cs)
- Groups IPv4 endpoints by
/24(uintkey) and IPv6 endpoints by/48(longkey) directly fromSocketEndpointraw bytes withoutIPAddressheap allocations. - Enforces
MaxConnectionsPerSubnet(50) andMaxSubnetConnectionsPerWindow(100).
Bounded Endpoint Map (MaxTrackedEndpoints = 50,000)
- Caps tracked IP entries at
ConnectionQuotaOptions.MaxTrackedEndpoints(50,000) and uses $O(1)$ Redis-style random sampling eviction when full, alongside the recurring"conn.limit"background cleanup job.
2. DatagramGuard (UdpListenerBase Protection)
DatagramGuard (public sealed class DatagramGuard : IDisposable) enforces per-IP packets-per-second budgets (ConnectionGuardOptions.MaxPacketPerSecond = 128) on incoming UDP datagrams:
- Maintains separate bounded maps for IPv4 (
DatagramGuardOptions.IPv4Windows = 65,536) and IPv6 (DatagramGuardOptions.IPv6Windows = 16,384). - When a source map is full,
DatagramGuardOptions.FailOpenWhenFullcontrols whether unknown sources are dropped (false, default) or allowed (true).
Configuration Summary
| Option Class | Key Settings | Reference |
|---|---|---|
ConnectionQuotaOptions | MaxConnectionsPerIpAddress = 10, MaxConnectionsPerWindow = 10, ConnectionRateWindow = 5s, MaxConnectionsPerSubnet = 50, ExemptLoopback = true, MaxTrackedEndpoints = 50,000 | Connection Quota Options |
ConnectionGuardOptions | MaxConnections = 2000, BanDuration = 5m, EnableProgressiveBanning = true, MaxErrorThreshold = 50, MaxPacketPerSecond = 128 | Connection Guard Options |
TrustedProxyOptions | StoreFileName = "trusted_proxies.txt", MaxConnectionsPerTrustedProxy = 5000, MaxAttemptsPerTrustedProxyWindow = 2000 | Trusted Proxy Options |