시간 도메인과 정지 중 메시지·베팅 경계 정리
This commit is contained in:
1 parent
10cddbb565
commit
abceee8315
108 files changed
+3504
-1473
No files matched your search
@@ -1,32 +1,37 @@
|
||||
import { performance } from 'node:perf_hooks';
|
||||
|
||||
import { gatewayProfileCapabilities } from '@sammo-ts/common';
|
||||
import { GamePrisma } from '@sammo-ts/infra';
|
||||
|
||||
import type { ProfileStatusSource } from '../auth/profileStatusSource.js';
|
||||
|
||||
interface TurnDaemonLeaseSource {
|
||||
turnDaemonLease: {
|
||||
findUnique(input: {
|
||||
where: { profile: string };
|
||||
select: { leaseUntil: true };
|
||||
}): Promise<{ leaseUntil: Date } | null>;
|
||||
};
|
||||
$queryRaw<T>(query: GamePrisma.Sql): Promise<T>;
|
||||
}
|
||||
|
||||
export const loadTurnEngineRunning = async (
|
||||
source: ProfileStatusSource | undefined,
|
||||
db: TurnDaemonLeaseSource,
|
||||
profileName: string,
|
||||
now = new Date()
|
||||
now?: Date
|
||||
): Promise<boolean | null> => {
|
||||
if (!source) return null;
|
||||
try {
|
||||
const status = await source.get(profileName);
|
||||
if (status === null) return null;
|
||||
if (!gatewayProfileCapabilities(status).turnsRunning) return false;
|
||||
const lease = await db.turnDaemonLease.findUnique({
|
||||
where: { profile: profileName },
|
||||
select: { leaseUntil: true },
|
||||
});
|
||||
return lease !== null && lease.leaseUntil.getTime() > now.getTime();
|
||||
const wallNow = now
|
||||
? GamePrisma.sql`${now}`
|
||||
: GamePrisma.sql`(CURRENT_TIMESTAMP AT TIME ZONE 'UTC')`;
|
||||
const rows = await db.$queryRaw<Array<{ running: boolean }>>(GamePrisma.sql`
|
||||
SELECT EXISTS (
|
||||
SELECT 1
|
||||
FROM turn_daemon_lease
|
||||
WHERE profile = ${profileName}
|
||||
AND lease_until > ${wallNow}
|
||||
) AS running
|
||||
`);
|
||||
return rows[0]?.running ?? false;
|
||||
} catch {
|
||||
return null;
|
||||
}
|
||||
@@ -42,7 +47,7 @@ export class CachedTurnEngineStatus {
|
||||
private readonly db: TurnDaemonLeaseSource,
|
||||
private readonly profileName: string,
|
||||
private readonly cacheMs = 2_000,
|
||||
private readonly now = () => Date.now()
|
||||
private readonly now = () => performance.now()
|
||||
) {}
|
||||
|
||||
get(): Promise<boolean | null> {
|
||||
|
||||
Reference in new issue
Block a user