Table of Contents

Class ServerBandwidthService

Namespace
FishMMO.Database.Npgsql.Services
Assembly
FishMMO-DB.dll

Writes a server process's own minute rows to server_bandwidth_minute.

public sealed class ServerBandwidthService : BaseService<ServerBandwidthMinuteEntity>, IServerBandwidthService
Inheritance
ServerBandwidthService
Implements
Inherited Members

Remarks

See IServerBandwidthService for why the key arrives finished.

Constructors

ServerBandwidthService(INpgsqlDbContextFactory)

public ServerBandwidthService(INpgsqlDbContextFactory dbContextFactory)

Parameters

dbContextFactory INpgsqlDbContextFactory

Fields

MaxSamplesPerWrite

The most rows one write takes: every minute a process may hold for retry, plus the one it has just sampled. A caller holding more has a bug, not a backlog.

public const int MaxSamplesPerWrite = 61

Field Value

int

MaxServerNameLength

The longest server name the column takes, matching the three server tables.

public const int MaxServerNameLength = 100

Field Value

int

Methods

FetchDatabaseUtcNowAsync(CancellationToken)

The database's UTC wall time, for keying the next minute row.

public Task<DatabaseResult<DateTime>> FetchDatabaseUtcNowAsync(CancellationToken cancellationToken = default)

Parameters

cancellationToken CancellationToken

Cancellation token.

Returns

Task<DatabaseResult<DateTime>>

RecordAsync(int, string, Guid, IReadOnlyList<ServerBandwidthSample>, CancellationToken)

Writes a process's minute rows: INSERT … ON CONFLICT DO UPDATE SET col = EXCLUDED.col.

public Task<DatabaseResult<int>> RecordAsync(int serverKind, string serverName, Guid instanceId, IReadOnlyList<ServerBandwidthSample> samples, CancellationToken cancellationToken = default)

Parameters

serverKind int

The tier (ServerBandwidthKind).

serverName string

The server's configured name.

instanceId Guid

The process's instance id, taken once at start.

samples IReadOnlyList<ServerBandwidthSample>

The rows, at most one per minute.

cancellationToken CancellationToken

Cancellation token.

Returns

Task<DatabaseResult<int>>

The number of rows inserted or replaced.

Remarks

Idempotent: writing the same rows twice leaves the table as writing them once, so the caller may re-send rows whose previous write failed or whose reply was lost. A row for a minute already stored REPLACES it (the row carries the minute's running total), unless its interval is shorter than the stored one — the running total of one minute only grows, so a shorter one is an older write arriving late and is ignored.

Failure cases: VALIDATION_ERROR for a bad kind, name, empty instance id, a duplicate minute in the batch, or a row whose Invalid() says why; otherwise the database's own error.

Validate(int, string, Guid, IReadOnlyList<ServerBandwidthSample>)

Why a write cannot be attempted, or null. Pure, so the rules are testable.

public static string? Validate(int serverKind, string serverName, Guid instanceId, IReadOnlyList<ServerBandwidthSample> samples)

Parameters

serverKind int
serverName string
instanceId Guid
samples IReadOnlyList<ServerBandwidthSample>

Returns

string