Class IngressGuard
Reusable per-connection, per-operation debounce and in-flight guard. Eliminates duplicated ingress-guard boilerplate across server systems. Thread-safe: every operation takes one short lock, so End(long) may be called from the worker that finished the operation.
public sealed class IngressGuard
- Inheritance
-
IngressGuard
- Inherited Members
Remarks
Clock. Debounce windows and marker ages are local durations, so they are timed on MonotonicClock. On the wall clock a step backwards held every debounce window closed for the length of the step — every guarded request from every player refused — and a step forwards lifted them all at once.
Expiry. Debounce entries are kept in the order they were last set, and so are in-flight markers, so both sweeps read from the oldest end and stop at the first entry that is not due. The previous sweep enumerated the whole dictionary from its head on every pass and removed at most a capped number, and the in-flight backstop enumerated every marker on every pass.
Capacity. A debounce entry whose window has closed decides nothing — a request against it is allowed exactly as if it were absent — and is only kept so a busy key is not reallocated on every request. So when the tracker is full, closed entries are reclaimed on the spot, and only a key that would need a new entry while FishMMO.Server.Core.IngressGuard.MaxTrackerEntries windows are all genuinely open is refused. It used to refuse every request from everyone once the count reached the cap, which ordinary traffic reaches on a busy scene well before the periodic sweep catches up.
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 whose window closed longer ago than this are stale.
maxRemovalsintMaximum entries to remove per sweep pass; the rest stay at the head for the next.
Remarks
Reads from the oldest end and stops at the first entry not yet due, so a pass costs one comparison plus one per entry it removes. An entry whose operation is still running keeps its debounce entry — dropping it would let the next request past the debounce check while the first is unfinished — and goes to the back to be looked at again.
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, an operation is already in-flight, or — only under a flood — the tracker is full of open debounce windows and this key would need a new one. Debounce timestamps are 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.