perf(auction): reduce polling with verified socket recovery

Keep fixed 30-second safety polling with separate recovery polling, confirm persistent heartbeat gaps, and refresh shared history after durable writes.
This commit is contained in:
dahyeon committed 2026-11-12 01:49:27 +09:00
1 parent e349684523
commit 23a0a3f8dd
20 files changed
+790 -154

No files matched your search

+75
View File
@@ -0,0 +1,75 @@
import assert from 'node:assert/strict';
import test from 'node:test';
import { setImmediate } from 'node:timers/promises';
import {
environmentManager,
QueryClient,
QueryObserver,
} from '@tanstack/react-query';
import {
AUCTION_SAFETY_POLL_INTERVAL_MS,
startAuctionRecoveryPolling,
} from '../src/api/auction-polling.ts';
test('repeated short recovery periods cannot postpone the safety HTTP poll', async (t) => {
t.mock.timers.enable({ apis: ['Date', 'setTimeout', 'setInterval'], now: 0 });
const wasServer = environmentManager.isServer();
environmentManager.setIsServer(() => false);
const client = new QueryClient({
defaultOptions: { queries: { retry: false, gcTime: Infinity } },
});
const requests: number[] = [];
const observer = new QueryObserver(client, {
queryKey: ['auction', 'match-1'],
queryFn: async () => {
requests.push(Date.now());
return { revision: 1 };
},
refetchInterval: AUCTION_SAFETY_POLL_INTERVAL_MS,
});
const unsubscribe = observer.subscribe(() => {});
let stopRecovery: (() => void) | undefined;
t.after(() => {
stopRecovery?.();
unsubscribe();
client.clear();
environmentManager.setIsServer(() => wasServer);
});
await setImmediate();
for (let attempt = 0; attempt < 6; attempt += 1) {
stopRecovery = startAuctionRecoveryPolling(
() => { void observer.refetch({ cancelRefetch: false }); },
() => true,
);
t.mock.timers.tick(1000);
await setImmediate();
stopRecovery();
t.mock.timers.tick(9000);
await setImmediate();
}
assert.deepEqual(requests, [0, 30_000, 60_000]);
});
test('recovery polls every five seconds while visible and stops after cleanup', (t) => {
t.mock.timers.enable({ apis: ['setInterval'] });
let visible = true;
let requests = 0;
const stop = startAuctionRecoveryPolling(
() => { requests += 1; },
() => visible,
);
t.after(stop);
t.mock.timers.tick(5000);
assert.equal(requests, 1);
visible = false;
t.mock.timers.tick(10_000);
assert.equal(requests, 1);
visible = true;
t.mock.timers.tick(5000);
assert.equal(requests, 2);
stop();
t.mock.timers.tick(10_000);
assert.equal(requests, 2);
});
+18
View File
@@ -72,6 +72,24 @@ test('bounds retries and honors retry delay without rejoining the room', async (
assert.deepEqual(waits, [1500, 1500]);
});
test('reports failure when all replies remain behind the required revision', async () => {
let failed = 0;
let applied = 0;
const sync = createAuctionResyncRequester(
async () => ({ ok: true, state: state(10) }),
() => {
applied++;
},
noWait,
() => {
failed++;
},
);
await sync.request(12, 'life');
assert.equal(applied, 3);
assert.equal(failed, 1);
});
test('drops an old connection reply after cancellation and permits a new request', async () => {
let finish!: (reply: AuctionResyncReply) => void;
let calls = 0;
+183
View File
@@ -6,9 +6,192 @@ import {
shouldApplyIncomingAuctionState,
shouldApplyPolledAuctionState,
shouldResyncForBidDelta,
resolveCurrentBidPointAmount,
shouldResyncForAuctionIdentity,
createAuctionHeartbeatMonitor,
} from "../src/api/auction-state.ts";
import type { ApiAuction } from "../src/api/types.ts";
test("Redis-only snapshots retain the bid amount without a legacy alias", () => {
assert.equal(
resolveCurrentBidPointAmount({
currentHighestBidPointAmount: 310,
} as ApiAuction),
310,
);
assert.equal(
resolveCurrentBidPointAmount({ currentBid: 120 } as ApiAuction),
120,
);
assert.equal(
resolveCurrentBidPointAmount({
currentHighestBidPointAmount: 0,
currentBid: 120,
} as ApiAuction),
0,
);
assert.equal(
resolveCurrentBidPointAmount({
currentHighestBidPointAmount: NaN,
} as ApiAuction),
0,
);
});
test("heartbeats detect missed state and separately versioned side effects", () => {
const state = {
auctionLifecycleId: "life",
revision: 10,
matchSideEffectRevision: 2,
} as ApiAuction;
assert.equal(
shouldResyncForAuctionIdentity(state, {
auctionLifecycleId: "life",
revision: 11,
}),
true,
);
assert.equal(
shouldResyncForAuctionIdentity(state, {
auctionLifecycleId: "life",
revision: 10,
matchSideEffectRevision: 3,
}),
true,
);
assert.equal(
shouldResyncForAuctionIdentity(state, {
auctionLifecycleId: "life",
revision: 10,
matchSideEffectRevision: 2,
}),
false,
);
assert.equal(
shouldResyncForAuctionIdentity(state, {
auctionLifecycleId: "new-life",
revision: 1,
}),
true,
);
});
test("a delayed heartbeat cannot pull an advanced socket backward", () => {
const advanced = { auctionLifecycleId: "life", revision: 12 } as ApiAuction;
assert.equal(
shouldResyncForAuctionIdentity(advanced, {
auctionLifecycleId: "life",
revision: 11,
matchSideEffectRevision: 9,
}),
false,
);
});
test("expired complete runtimes and legacy archives do not repeatedly resync", () => {
assert.equal(
shouldResyncForAuctionIdentity(
{ auctionLifecycleId: "life", phase: "complete" } as ApiAuction,
null,
),
false,
);
assert.equal(
shouldResyncForAuctionIdentity({ phase: "bidding" } as ApiAuction, null),
false,
);
assert.equal(
shouldResyncForAuctionIdentity(
{
auctionEngineVersion: "legacy-renamed",
auctionLifecycleId: "legacy-auction:1",
phase: "bidding",
} as ApiAuction,
null,
),
false,
);
assert.equal(
shouldResyncForAuctionIdentity(
{
auctionEngineVersion: "redis-v1",
auctionLifecycleId: "life",
phase: "bidding",
} as ApiAuction,
null,
),
true,
);
});
test("a new in-flight bid does not confirm an earlier gap that already closed", () => {
const monitor = createAuctionHeartbeatMonitor();
const client = {
auctionEngineVersion: "redis-v1",
auctionLifecycleId: "life",
revision: 10,
} as ApiAuction;
assert.equal(
monitor.inspect(client, { auctionLifecycleId: "life", revision: 11 })
.needsResync,
false,
);
const caughtUp = { ...client, revision: 11 };
assert.equal(
monitor.inspect(caughtUp, { auctionLifecycleId: "life", revision: 12 })
.needsResync,
false,
);
assert.equal(
monitor.inspect(caughtUp, { auctionLifecycleId: "life", revision: 13 })
.needsResync,
true,
);
});
test("heartbeat failures require two misses and reset after a healthy reply", () => {
const monitor = createAuctionHeartbeatMonitor();
assert.equal(monitor.recordFailure(), false);
assert.equal(monitor.recordFailure(), true);
monitor.inspect(
{ auctionEngineVersion: "legacy-renamed" } as ApiAuction,
null,
);
assert.equal(monitor.recordFailure(), false);
monitor.reset();
assert.equal(monitor.recordFailure(), false);
});
test("heartbeat confirmation covers side effects and missing runtimes", () => {
const monitor = createAuctionHeartbeatMonitor();
const client = {
auctionEngineVersion: "redis-v1",
auctionLifecycleId: "life",
revision: 10,
matchSideEffectRevision: 1,
} as ApiAuction;
const identity = {
auctionLifecycleId: "life",
revision: 10,
matchSideEffectRevision: 2,
};
assert.equal(monitor.inspect(client, identity).needsResync, false);
assert.equal(
monitor.inspect({ ...client, matchSideEffectRevision: 2 }, identity)
.isCurrent,
true,
);
assert.equal(monitor.inspect(client, null).needsResync, false);
assert.equal(monitor.inspect(client, null).needsResync, true);
monitor.reset();
const legacy = {
...client,
auctionEngineVersion: "legacy-renamed",
} as ApiAuction;
assert.equal(monitor.inspect(legacy, null).isCurrent, true);
assert.equal(monitor.inspect(legacy, null).needsResync, false);
});
const current = {
auctionLifecycleId: "life",
revision: 10,