Table of Contents

Class WorldClockControlService

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

The world clock row: reads, the scene servers' seed, and the admin writes. See IWorldClockControlService for the contract.

public sealed class WorldClockControlService : BaseService<WorldClockControlEntity>, IWorldClockControlService
Inheritance
WorldClockControlService
Implements
Inherited Members

Remarks

All clock arithmetic is SQL against clock_timestamp(). The world time a write replaces, the anchor it writes and the instant it stamps all come from one evaluation of the database clock inside the statement that writes them. A world time computed in this process and sent back would carry this host's skew and the round trip, and two admins writing at once would each overwrite the other's re-anchor.

Why the write is a CTE that locks the row. The o CTE takes the row with FOR UPDATE; under READ COMMITTED a second writer waits there and then sees the FIRST writer's row, not the one its snapshot started with. The database clock is read in the next CTE (k), which projects only once a locked row has been returned, so the waiting writer measures elapsed time from after the wait, against the anchor the first writer left. Two concurrent shifts of +1h therefore move the world 2h, exactly.

Idempotency. ExecuteWriteAsync retries a dropped connection, and a connection can drop after the commit but before the reply. Each write takes a key once, outside the retried delegate, writes it to request_key, and skips a row already carrying it; a write that matches nothing then looks to see whether its own key is there, and if so reports the row as written (a replay) instead of shifting the world a second time.

Constructors

WorldClockControlService(INpgsqlDbContextFactory)

Initializes the service.

public WorldClockControlService(INpgsqlDbContextFactory dbContextFactory)

Parameters

dbContextFactory INpgsqlDbContextFactory

DbContext factory for creating contexts.

Methods

EnsureSeededAsync(long, CancellationToken)

Creates the row if there is none (INSERT … ON CONFLICT (id) DO NOTHING): world time = the database's now − the epoch (floored at zero), real-time pace, revision 1. An existing row is left exactly as it is, epoch included.

public Task<DatabaseResult<WorldClockReading>> EnsureSeededAsync(long epochUnixSeconds, CancellationToken cancellationToken = default)

Parameters

epochUnixSeconds long

The calendar epoch the scene servers use, Unix seconds.

cancellationToken CancellationToken

Cancellation token.

Returns

Task<DatabaseResult<WorldClockReading>>

The row as it now stands (Exists is true on success). Compare its EpochUnixSeconds with your own: a row seeded earlier keeps the epoch it was seeded with.

HoldAsync(string, CancellationToken)

Holds world time where it is now (pace 0). The running pace becomes the resume pace; holding an already held clock keeps the resume pace it had.

public Task<DatabaseResult<WorldClockReading>> HoldAsync(string actor, CancellationToken cancellationToken = default)

Parameters

actor string
cancellationToken CancellationToken

Returns

Task<DatabaseResult<WorldClockReading>>

ReadAsync(CancellationToken)

Reads the database clock and the row in one round trip. Succeeds with Exists false when there is no row yet.

public Task<DatabaseResult<WorldClockReading>> ReadAsync(CancellationToken cancellationToken = default)

Parameters

cancellationToken CancellationToken

Returns

Task<DatabaseResult<WorldClockReading>>

ResumeAsync(string, CancellationToken)

Resumes at the resume pace from where world time stands (while held, that is the held time). On a clock that is already running it re-applies the resume pace.

public Task<DatabaseResult<WorldClockReading>> ResumeAsync(string actor, CancellationToken cancellationToken = default)

Parameters

actor string
cancellationToken CancellationToken

Returns

Task<DatabaseResult<WorldClockReading>>

SetAsync(long, string, CancellationToken)

Sets world time to worldMs at this database instant, keeping the pace.

public Task<DatabaseResult<WorldClockReading>> SetAsync(long worldMs, string actor, CancellationToken cancellationToken = default)

Parameters

worldMs long

World milliseconds since the epoch; at least zero, and a calendar date (year ≤ 9999).

actor string

The ACCOUNT making the change (recorded as updated_by).

cancellationToken CancellationToken

Cancellation token.

Returns

Task<DatabaseResult<WorldClockReading>>

The new reading, with ReplacedWorldMs and ReplacedRate set.

SetRateAsync(double, string, CancellationToken)

Changes the pace from this instant without moving world time. Above zero it also becomes the resume pace; zero is a hold (the running pace is kept as the resume pace).

public Task<DatabaseResult<WorldClockReading>> SetRateAsync(double rate, string actor, CancellationToken cancellationToken = default)

Parameters

rate double

World seconds per real second, 0 to MaxRate. Negative, NaN and infinity are refused.

actor string

The ACCOUNT making the change.

cancellationToken CancellationToken

Cancellation token.

Returns

Task<DatabaseResult<WorldClockReading>>

The new reading.

ShiftAsync(long, string, CancellationToken)

Moves world time by deltaMs from world-now, keeping the pace.

public Task<DatabaseResult<WorldClockReading>> ShiftAsync(long deltaMs, string actor, CancellationToken cancellationToken = default)

Parameters

deltaMs long

Signed milliseconds. Refused when the result would fall before the epoch or past year 9999.

actor string

The ACCOUNT making the change.

cancellationToken CancellationToken

Cancellation token.

Returns

Task<DatabaseResult<WorldClockReading>>

The new reading.