Table of Contents

Class CharacterWriteGate

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

The one ownership check every per-character sub-entity write passes through: a row lands only while its character's session claim is still held by the writer that captured it.

public static class CharacterWriteGate
Inheritance
CharacterWriteGate
Inherited Members

Remarks

Why. The character row's own write has always carried the ownership triple (ICharacterService.PersistOwnedAsync), but its sub-entity tables were guarded only by their version, which does not identify the writer. So a write captured by a session that was released a moment later — a periodic pass still on the worker, a retry — could land after the release: over an offline tax debit, over the last trade-settlement residual, or over the next owner's first save, which restarts its versions from the rows it loaded and so loses the race it should win. A server whose lease lapsed kept doing it for a whole refresh interval.

How. Inside the write's own transaction, one statement takes a FOR SHARE lock on every named character that is live, Online and claimed under exactly the triple the writer quotes, in ascending id order, and the write then touches only those characters' rows. FOR SHARE is the weakest mode an ordinary UPDATE of the character row has to wait behind, so a release, a claim, the lease refresh and the row save all queue behind the write rather than interleaving with it; two sub-entity writes for the same character do not block each other; and the FOR KEY SHARE every character-owned foreign key takes is unaffected. A release that commits first is seen: under READ COMMITTED the locking read re-checks the predicate against the version it waited for, so the released row simply stops matching. Ascending order matches every other multi-row locker of the character table (CharacterService.LockCharacterRowsAscendingAsync), so no cycle can form.

What a refusal looks like. Rows of a live character whose claim the writer does not hold are not attempted and are reported as Unowned, which is part of Filtered — so a caller that clears its dirty marks only when nothing was filtered keeps them. A write that could touch none of its characters fails: with NotFound when none exists, and with Forbidden when they exist but none is the writer's, the code a refused character-row save already uses.

Passing no claims at all is the ungated write, kept for writers that hold none by design — character creation and deletion on the login server — and for writes already inside a unit of work that has asserted ownership itself (the item layer's). It checks only that the character exists, as every service did before.

Methods

Decide(int, int, int)

The whole-write decision, from how many characters the write named, how many of those are live, and how many of those the writer may write. Pure.

public static CharacterWriteGateDecision Decide(int named, int live, int writable)

Parameters

named int

Distinct characters the write's rows name.

live int

How many of them exist and are not deleted.

writable int

How many of the live ones the writer may write.

Returns

CharacterWriteGateDecision

The decision.

ValidateClaim(CharacterSessionLeaseData, long)

Checks the one claim a single-row owned write was handed, against the character it writes, before any work starts. Pure.

public static string? ValidateClaim(CharacterSessionLeaseData claim, long characterId)

Parameters

claim CharacterSessionLeaseData

The claim.

characterId long

The character the write is about.

Returns

string

Null when usable; otherwise why not.

ValidateClaims(IReadOnlyCollection<CharacterSessionLeaseData>?)

Checks the claims an owned write was handed before any work starts. Pure.

public static string? ValidateClaims(IReadOnlyCollection<CharacterSessionLeaseData>? claims)

Parameters

claims IReadOnlyCollection<CharacterSessionLeaseData>

Returns

string

Null when usable; otherwise why not.