Class ChatReadWindow
Where a reader of the chat table has got to, and which rows it has already handled, on the database clock. Pure bookkeeping: no database, no network, one thread at a time.
public sealed class ChatReadWindow
- Inheritance
-
ChatReadWindow
- Inherited Members
Remarks
Why a window and not a cursor. A chat row is stamped by the database clock when its
INSERT runs, and becomes visible only when its transaction commits. Two writers can commit
in the opposite order to their stamps — and to their IDs, which are also taken at the
INSERT. A reader that pages by "everything after the last row I saw", by ID or by
(time, id), moves past a row that has not committed yet and never sees it (hot-path
audit H5). So the reader reads from Watermark, held
CommitWindow behind what it has settled, and skips by ID what it has already
handled (SnapshotSeenIds() for the query, Admit(long, DateTime) for the rows it
returns). A row is read exactly once as long as it commits within the window of its stamp;
ChatService.PumpCommitWindowSeconds is that window, shared with the writers.
Why a floor. Nothing stamped before the reader's first read is ever read. The first read starts at the database's own "now" and later reads reach back a window from their start, so without the floor the second read would replay the window before the reader started: a restarted reader would republish what it had already published.
The scene servers' pump (ChatPumpCursor, in the game's server code) keeps the same
window and adds per-recipient relevance on top; this is the window alone, for a reader that
takes every row, such as the Discord relay.
Constructors
ChatReadWindow(TimeSpan)
Creates a window with the given commit window.
public ChatReadWindow(TimeSpan commitWindow)
Parameters
commitWindowTimeSpan
Properties
CommitWindow
How long after its stamp a row may commit and still be read.
public TimeSpan CommitWindow { get; }
Property Value
LastReadStartedUtc
The database clock at the start of the last completed read, or null before the first.
public DateTime? LastReadStartedUtc { get; }
Property Value
SeenCount
Rows handled that the next read could still return.
public int SeenCount { get; }
Property Value
Watermark
The oldest stamp the next read starts from, or null before the first read, which starts at the database's own "now". Only ever moves forward.
public DateTime? Watermark { get; }
Property Value
Methods
Admit(long, DateTime)
Records one row the read returned as handled.
public bool Admit(long id, DateTime timeCreatedUtc)
Parameters
Returns
- bool
True the first time a row is seen; false for a row already handled.
CompleteRead(DateTime, bool, DateTime?)
Settles a completed read: moves the watermark and forgets rows it has passed. Called after every read that succeeded, including one that returned nothing, or an idle reader's window never advances. Not called after a read that failed, so its rows are read again.
public void CompleteRead(DateTime readStartedUtc, bool drained, DateTime? lastRowTimeUtc)
Parameters
readStartedUtcDateTimeThe database clock taken before the read's first page.
drainedboolTrue if the read caught up; false if it stopped at its page limit.
lastRowTimeUtcDateTime?The stamp of the last row read, or null if none were.
Reset()
Forgets everything, for a reader that is starting again from "now".
public void Reset()
SnapshotSeenIds()
The IDs of the rows handled that the next read could return again, for the read to skip.
public long[] SnapshotSeenIds()
Returns
- long[]