Table of Contents

Class ServerBandwidthLedger

Namespace
FishMMO.Server.Core
Assembly
FishMMO.Server.dll

Turns a server's cumulative transport counters into the minute rows it writes to server_bandwidth_minute. Pure bookkeeping, so every rule is testable without a network manager or a database.

public sealed class ServerBandwidthLedger
Inheritance
ServerBandwidthLedger
Inherited Members

Remarks

A minute row holds its minute's running total, and is written as a SET. Each snapshot is filed under the database-clock minute it was taken in. The first snapshot in a minute opens it with the traffic since the previous snapshot; a second snapshot in the same minute (a late frame, a clock step) rewrites the row with the minute's running total, which contains the first. Re-sending any row is therefore harmless — the database takes the newer total or the same one — which is what lets a failed write simply be retried next minute instead of lost, and a retried write after a lost commit reply never be counted twice.

Every byte lands in exactly one minute. The interval between two consecutive snapshots belongs to the minute of the later one. A minute never reopens: a snapshot whose database minute is earlier than the open one (the database clock stepped back) is filed under the open one.

Bounded. At most MaxPendingMinutes minutes wait for a write; beyond that the oldest are dropped and counted in DroppedMinutes, so a long outage costs memory for an hour and then costs visible, logged minutes rather than growing without limit.

Not thread-safe. The recorder touches it from one piece of work at a time (its single-flight gate), which is also what keeps two writes of one minute from racing.

Constructors

ServerBandwidthLedger(in TransportTrafficSnapshot, int)

Starts a ledger from the counters as they are when the process begins recording.

public ServerBandwidthLedger(in TransportTrafficSnapshot start, int maxPending = 60)

Parameters

start TransportTrafficSnapshot

A capture taken when recording starts; see StartingPoint(in TransportTrafficSnapshot).

maxPending int

Most minutes held for a write.

Properties

DroppedMinutes

Minutes dropped unwritten because MaxPendingMinutes were already waiting.

public long DroppedMinutes { get; }

Property Value

long

OpenMinute

The minute later snapshots add to, or null before the first recorded snapshot and after a rebaseline.

public DateTime? OpenMinute { get; }

Property Value

DateTime?

PendingCount

How many minutes are waiting for a write.

public int PendingCount { get; }

Property Value

int

Methods

Acknowledge(IReadOnlyList<ServerBandwidthSample>)

Forgets the minutes a write stored. A minute that has been re-recorded since the batch was taken (its running total grew) stays pending, so its newer total is written next.

public void Acknowledge(IReadOnlyList<ServerBandwidthSample> written)

Parameters

written IReadOnlyList<ServerBandwidthSample>

IsComplete(in TransportTrafficRates)

Whether a delta measured every column a minute row stores.

public static bool IsComplete(in TransportTrafficRates delta)

Parameters

delta TransportTrafficRates

Returns

bool

Pending()

The minutes waiting for a write, oldest first, copied so the caller may hold them across an await.

public List<ServerBandwidthSample> Pending()

Returns

List<ServerBandwidthSample>

Record(in TransportTrafficSnapshot, DateTime)

Files the traffic up to now under its minute.

public ServerBandwidthSampleOutcome Record(in TransportTrafficSnapshot now, DateTime databaseUtcAtSnapshot)

Parameters

now TransportTrafficSnapshot

A capture of the transport's counters.

databaseUtcAtSnapshot DateTime

The database clock at the moment of now.

Returns

ServerBandwidthSampleOutcome

StartingPoint(in TransportTrafficSnapshot)

The baseline a ledger starts from: start, except that a native QUIC layer that is not available yet counts as zero.

public static TransportTrafficSnapshot StartingPoint(in TransportTrafficSnapshot start)

Parameters

start TransportTrafficSnapshot

Returns

TransportTrafficSnapshot

Remarks

A server starts recording before its transport opens, when msquic has not been loaded in this process, and the transport reports that layer as Unavailable. Unavailable there is not "unknown": msquic's process-lifetime totals are counted from the library's first initialisation, so before it they are exactly zero. Starting from zero keeps the first minute — the one with every client's handshake in it — instead of losing it to a layer that was "unavailable" at one end of the interval. (The other way a native read is Unavailable, a failed read, is unreachable after the transport's ABI check, and a browser backend is never a server.)