Class KickRequestService
Base class for database services that execute EF Core operations with consistent execution behavior (transactional and read-only), retry behavior for transient failures, and standardized error mapping into DatabaseResult.
public sealed class KickRequestService : BaseService<KickRequestEntity>, IKickRequestService
- Inheritance
-
KickRequestService
- Implements
- Inherited Members
Remarks
This base type provides three primary execution paths:
- ExecuteTransactionAsync(Func<Task>, CancellationToken) and ExecuteTransactionAsync<TResult>(Func<Task<TResult>>,string,CancellationToken) create a fresh NpgsqlDbContext, begin an explicit transaction, execute the delegate, then call SaveChangesAsync(CancellationToken) and commit.
- ExecuteWriteAsync(Func<Task>,string,CancellationToken) and ExecuteWriteAsync<TResult>(Func<Task<TResult>>,string,CancellationToken) create a fresh NpgsqlDbContext, execute the delegate, then call SaveChangesAsync(CancellationToken) without starting an explicit transaction.
- ExecuteReadAsync(Func<Task>,string,CancellationToken) and ExecuteReadAsync<TResult>(Func<Task<TResult>>,string,CancellationToken) create a fresh NpgsqlDbContext but do not start an explicit transaction and do not call SaveChanges.
Standalone Execution (no ambient scope): A new context is created per attempt to avoid EF change-tracker state leaking across retries. Transient database failures are retried with exponential backoff. Optimistic concurrency conflicts (Version-based authority) and StaleStateException are never retried; they are returned as non-transient failures so the caller can re-read and decide how to proceed.
Ambient Scope Execution (inside an existing Unit of Work): When an operation detects an active FishMMO.Database.Npgsql.Services.DatabaseExecutionScope, it reuses the ambient NpgsqlDbContext and does NOT retry on transient failures. This is by design for the following reasons:
- Transaction State Corruption: PostgreSQL aborts the entire transaction on most transient failures. The connection and transaction become unusable, making retry with the same context impossible.
- Context State Pollution: The DbContext's change tracker accumulates state. Retrying with a polluted change tracker can cause duplicate key violations or incorrect updates.
- Semantic Correctness: If operation A succeeded and operation B failed transiently, retrying B alone without re-evaluating A's preconditions could violate business invariants.
When a transient failure occurs inside an ambient scope, the failure is returned immediately so the caller can restart the entire unit of work with fresh state. Savepoints are used for nested atomicity but cannot recover from connection-level failures.
Constructors
KickRequestService(INpgsqlDbContextFactory)
Initializes a new instance of KickRequestService.
public KickRequestService(INpgsqlDbContextFactory dbContextFactory)
Parameters
dbContextFactoryINpgsqlDbContextFactoryDbContext factory for creating contexts.
Exceptions
- ArgumentNullException
Thrown when dbContextFactory is null.
Fields
PollCommitWindowSeconds
How far behind its own progress a game server's kick poll reads, in seconds.
public const double PollCommitWindowSeconds = 10
Field Value
Remarks
A request's stamp is not its commit. Both writers stamp time_created with
the transaction's start (PersistAsync(string, CancellationToken), and the ban in
AccountService, which writes its kick after revoking the account's tokens and
sessions in the same transaction), and the row becomes visible only when that
transaction commits. A reader that paged by a strict (time, id) cursor could move
past a stamp before its row committed and never see it: the operator's kick silently
never landed.
So the poll re-reads this far behind the newest point it has settled and skips what it has already handled (KickRequestReadWindow). A kick is caught as long as it commits within this long of its stamp; ten seconds is a stall, not a slow write. It lives here, beside the writers, so the two sides of the contract are one number.
Methods
DeleteAsync(string, CancellationToken)
Deletes all kick requests for the specified account.
public Task<DatabaseResult<int>> DeleteAsync(string accountName, CancellationToken cancellationToken = default)
Parameters
accountNamestringAccount name whose kick requests will be deleted.
cancellationTokenCancellationTokenCancellation token.
Returns
- Task<DatabaseResult<int>>
A DatabaseResult<T> containing the number of kick requests deleted on success, or a DatabaseException on failure.
Remarks
This is an idempotent cleanup operation. Unlike entity delete methods, this method does NOT throw DatabaseEntityNotFoundException when no records exist. Instead, it returns 0 rows deleted. This design supports safe concurrent cleanup where multiple callers may attempt to delete the same records.
Uses ExecuteSqlRawAsync with execution strategy wrapping to ensure transient database failures are automatically retried.
FetchAsync(KickRequestPollQuery, CancellationToken)
Reads one page of kick requests for a game server's poll, on the database's clock.
public Task<DatabaseResult<KickRequestPage>> FetchAsync(KickRequestPollQuery query, CancellationToken cancellationToken = default)
Parameters
queryKickRequestPollQueryWhere to read from and what to skip, built by BuildQuery(int, double).
cancellationTokenCancellationTokenCancellation token.
Returns
- Task<DatabaseResult<KickRequestPage>>
The page — requests stamped at or after the start and not already handled, in
(time_created, id)order — with the database clock taken before it was read and the start it read from; or a DatabaseException on failure.
Remarks
The reader keeps its place with KickRequestReadWindow, which reads a commit window (PollCommitWindowSeconds) behind what it has settled and skips what it has handled, so a request that commits after a later-stamped one is still read. A first read (no FromUtc) starts FirstReadLookbackSeconds before the database's "now". Each request carries its account's last login (AccountLastLogin), read in the same query, so the caller needs no per-kick lookup to tell a stale kick.
HasPendingAsync(string, CancellationToken)
Checks whether a pending kick request exists for the specified account.
public Task<DatabaseResult<bool>> HasPendingAsync(string accountName, CancellationToken cancellationToken = default)
Parameters
accountNamestringAccount name to check.
cancellationTokenCancellationTokenCancellation token.
Returns
- Task<DatabaseResult<bool>>
A DatabaseResult<T> containing
trueif a pending kick request exists,falseotherwise, or a DatabaseException on failure.
Remarks
A request is pending for a bounded time after its stamp, measured by the database's clock, which wrote the stamp; never by the calling process's.
PersistAsync(string, CancellationToken)
Persists a kick request for the specified account.
public Task<DatabaseResult> PersistAsync(string accountName, CancellationToken cancellationToken = default)
Parameters
accountNamestringAccount name to kick.
cancellationTokenCancellationTokenCancellation token.
Returns
- Task<DatabaseResult>
A DatabaseResult indicating success or containing a DatabaseException on failure.
Remarks
Uses SaveChangesAsync with execution strategy wrapping to ensure transient database failures are automatically retried. Creates new kick request with current UTC timestamp.