Table of Contents

Class FixedWindowCounter<TKey>

Namespace
FishMMO.Auth.Core.Collections
Assembly
FishMMO-ServerAuth.dll

Per-key event counter over a fixed window that opens at the key's first event, with expiry swept head-first in the order windows opened.

public sealed class FixedWindowCounter<TKey> where TKey : notnull

Type Parameters

TKey

Counted key type.

Inheritance
FixedWindowCounter<TKey>
Inherited Members

Remarks

The shape behind every "N failures within T of the first one" lockout and every "at most N per T" burst limiter here: a key's window opens at its first event, counts events until it is Window old, and then closes — the next event opens a fresh window. A window never moves once opened, so the order windows opened in is exactly the order they expire in, and that is the order the internal list keeps.

Why not a dictionary swept from its head. The trackers this replaces were ConcurrentDictionary instances swept by enumerating at most N entries per pass. An enumeration always starts at the same place, so once the first N entries were live the sweep never reached anything behind them: expired entries leaked until a capacity cap was hit, after which new keys stopped being tracked at all and the lockout they implement silently switched off. Here a sweep looks only at the oldest window, removes it if it has closed, and stops at the first one that has not — O(removed + 1), and it always reaches the oldest entry first.

An expired window that the sweep has not reached yet is never counted: every read treats it as absent and every write replaces it, so the sweep only reclaims memory and has no effect on any answer.

Time is monotonic seconds — a MonotonicClock.NowSeconds reading, from the authenticator's clock or the game server's. A window is a local duration, and a wall clock stepped by NTP would close every window at once or hold them all open. One counter must only ever be given readings from one clock.

Thread-safe. Every operation takes one short lock; nothing is allocated except when a key opens a window.

Constructors

FixedWindowCounter(TimeSpan, IEqualityComparer<TKey>?)

Initializes a counter whose windows last window.

public FixedWindowCounter(TimeSpan window, IEqualityComparer<TKey>? comparer = null)

Parameters

window TimeSpan

How long a window stays open after its first event. Must be positive.

comparer IEqualityComparer<TKey>

Optional key comparer.

Properties

Count

Keys currently held, including closed windows the sweep has not reached yet.

public int Count { get; }

Property Value

int

Window

How long a window stays open after its first event.

public TimeSpan Window { get; }

Property Value

TimeSpan

Methods

Clear()

Drops every window.

public void Clear()

GetCount(TKey, double)

Events counted in key's open window, or 0 when it has none.

public int GetCount(TKey key, double nowSeconds)

Parameters

key TKey
nowSeconds double

Returns

int

Remarks

A closed window found on the way is removed.

Increment(TKey, double, int)

Counts one event for key, opening a fresh window when none is open.

public int Increment(TKey key, double nowSeconds, int maxKeys = 2147483647)

Parameters

key TKey

Counted key.

nowSeconds double

Current monotonic time in seconds.

maxKeys int

Capacity for keys with no open window. A key that already has one is always counted, so a lockout already under way keeps accruing at capacity; only a new key is refused.

Returns

int

The count in the key's window after this event, or 0 when the key was refused for capacity.

IsOpen(double, double)

Whether a window opened at openedSeconds is still open at nowSeconds.

public bool IsOpen(double openedSeconds, double nowSeconds)

Parameters

openedSeconds double
nowSeconds double

Returns

bool

Remarks

The single rule every read, write and sweep uses. A window is open for exactly Window: closed at opened + window, not a tick after it.

Remove(TKey)

Drops key's window, open or not.

public bool Remove(TKey key)

Parameters

key TKey

Returns

bool

true when the key was held.

SecondsUntilClose(TKey, double)

Seconds until key's open window closes, or 0 when it has none.

public double SecondsUntilClose(TKey key, double nowSeconds)

Parameters

key TKey
nowSeconds double

Returns

double

Remarks

For a lockout this is how long the refusal still has to run, which is what a player told they are locked out needs to know. A closed window found on the way is removed.

SweepExpired(double, int)

Removes closed windows, oldest first, stopping at the first one still open.

public int SweepExpired(double nowSeconds, int maxRemove)

Parameters

nowSeconds double

Current monotonic time in seconds.

maxRemove int

Most windows to remove in this call.

Returns

int

Windows removed.

TryIncrement(TKey, double, int, int)

Counts one event for key only while its window holds fewer than limit events.

public bool TryIncrement(TKey key, double nowSeconds, int limit, int maxKeys = 2147483647)

Parameters

key TKey

Counted key.

nowSeconds double

Current monotonic time in seconds.

limit int

Events allowed per window.

maxKeys int

Capacity for keys with no open window; see Increment(TKey, double, int).

Returns

bool

true when the event was counted; false when the window is full or the key was refused for capacity.

Remarks

A refused event does not touch the window — it neither counts nor extends it — so a caller hammering a full window waits out the same window as everyone else.