Class ExpiringKeyTracker<TKey>
- Namespace
- FishMMO.Server.Core.Collections
- Assembly
- FishMMO.Server.dll
Queue/index tracker for expiring keyed entries. Uses head-first sweeps to avoid full dictionary enumeration under heavy load.
public sealed class ExpiringKeyTracker<TKey>
Type Parameters
TKeyTracker key type.
- Inheritance
-
ExpiringKeyTracker<TKey>
- Inherited Members
Constructors
ExpiringKeyTracker(IEqualityComparer<TKey>)
Initializes a new tracker with an optional key comparer.
public ExpiringKeyTracker(IEqualityComparer<TKey> comparer = null)
Parameters
comparerIEqualityComparer<TKey>
Properties
Count
Current number of tracked keys.
public int Count { get; }
Property Value
Methods
Clear()
Clears all tracked entries.
public void Clear()
Remove(TKey)
Removes a key from the tracker if present. Useful for cancelling a debounce window when the operation it was guarding has already failed and the caller wants to allow an immediate retry rather than make the user wait out the window.
public bool Remove(TKey key)
Parameters
keyTKeyTracker key to remove.
Returns
- bool
trueif the key existed and was removed; otherwisefalse.
SweepExpired(DateTime, int, int)
Sweeps expired keys with bounded scan and removal limits.
public int SweepExpired(DateTime nowUtc, int maxScan, int maxRemove)
Parameters
nowUtcDateTimeCurrent UTC timestamp.
maxScanintMaximum queue nodes to inspect this sweep.
maxRemoveintMaximum keys to remove this sweep.
Returns
- int
Number of entries removed.
SweepExpired(double, int, int)
Sweeps expired keys, for a tracker timed on MonotonicClock. See TryBegin(TKey, double, TimeSpan).
public int SweepExpired(double nowSeconds, int maxScan, int maxRemove)
Parameters
nowSecondsdoubleCurrent NowSeconds reading.
maxScanintMaximum queue nodes to inspect this sweep.
maxRemoveintMaximum keys to remove this sweep.
Returns
- int
Number of entries removed.
TryBegin(TKey, DateTime, TimeSpan)
Attempts to begin a debounce/rate-limit window for a key.
public bool TryBegin(TKey key, DateTime nowUtc, TimeSpan duration)
Parameters
Returns
- bool
trueif allowed now; otherwisefalse.
TryBegin(TKey, double, TimeSpan)
Attempts to begin a debounce/rate-limit window for a key, timed on MonotonicClock.
public bool TryBegin(TKey key, double nowSeconds, TimeSpan duration)
Parameters
keyTKeyTracker key.
nowSecondsdoubleCurrent NowSeconds reading.
durationTimeSpanWindow duration.
Returns
- bool
trueif allowed now; otherwisefalse.
Remarks
A debounce is a duration, and on DateTime.UtcNow a host clock stepped back an hour
held every recently seen key inside its window for that hour: an account refused for three
seconds was refused for sixty minutes. The tracker only ever compares the instants it is
given with one another, so the monotonic reading is carried in the same field, offset from
an arbitrary origin.
One tracker, one clock. Driving an instance through both this overload and the DateTime one compares unrelated numbers; every tracker in the codebase uses exactly one of them.