Class IngressGuard
Reusable per-connection, per-operation debounce and in-flight guard. Eliminates duplicated ingress-guard boilerplate across server systems. Thread-safe: backed by ConcurrentDictionary for cross-thread access.
public sealed class IngressGuard
- Inheritance
-
IngressGuard
- Inherited Members
Constructors
IngressGuard()
Initializes a new instance of the IngressGuard class.
public IngressGuard()
Methods
Clear()
Clears all tracked entries. Safe to call during shutdown.
public void Clear()
End(long)
Releases the in-flight lock for the given guard key. Call in a finally block.
public void End(long guardKey)
Parameters
guardKeylong
Sweep(float, float, int)
Bounded sweep of stale ingress entries. Call once per frame or at a configured interval.
public void Sweep(float sweepIntervalSeconds, float entryTtlSeconds, int maxRemovals)
Parameters
sweepIntervalSecondsfloatMinimum seconds between sweep passes.
entryTtlSecondsfloatEntries older than this are considered stale.
maxRemovalsintMaximum entries to remove per sweep pass.
TryBegin(int, byte, int, out long, int)
Attempts to acquire the ingress guard for the given connection and operation. Returns false if debounce is active, the tracker is saturated, or an operation is already in-flight. Debounce timestamp is only updated AFTER a successful in-flight acquisition, preventing unintended sliding-window extension on rejected requests.
public bool TryBegin(int connectionId, byte operation, int debounceMilliseconds, out long guardKey, int globalRateMilliseconds = 0)
Parameters
connectionIdintNetwork connection identifier.
operationbyteOperation code (cast your enum to byte).
debounceMillisecondsintMinimum milliseconds between requests for this key.
guardKeylongOutput guard key to pass to End() in a finally block.
globalRateMillisecondsintOptional global per-connection rate (0 = disabled). Uses operation 0 key.
Returns
- bool
True if the guard was acquired; false if the request should be rejected.