fix(sync): keep the notice window on its timer and survive a throwing WebSocket (#175)
Self-review findings. A notice landing after the nominal end of the window but before its (late) timer armed a second window timer; the first one then cleared the handle of the second, and the next notice synced inside a window. The window now lasts until its single timer fires. A WebSocket constructor that throws is handled as a failed connection instead of an unhandled rejection that left the state on "connecting". Refs #175 Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
parent
4a30d646b4
commit
e00bcf6e8a
2 changed files with 58 additions and 8 deletions
|
|
@ -157,7 +157,6 @@ export function createSyncSocket(deps: SyncSocketDeps): SyncSocket {
|
|||
let socket: SocketHandle | null = null;
|
||||
let retryTimer: unknown = null;
|
||||
let windowTimer: unknown = null;
|
||||
let windowEnd = 0;
|
||||
let noticePending = false;
|
||||
let lastSyncAt: number | null = null;
|
||||
|
||||
|
|
@ -183,7 +182,6 @@ export function createSyncSocket(deps: SyncSocketDeps): SyncSocket {
|
|||
function clearWindow() {
|
||||
if (windowTimer !== null) deps.clearTimer(windowTimer);
|
||||
windowTimer = null;
|
||||
windowEnd = 0;
|
||||
noticePending = false;
|
||||
}
|
||||
|
||||
|
|
@ -219,22 +217,21 @@ export function createSyncSocket(deps: SyncSocketDeps): SyncSocket {
|
|||
|
||||
// Window of the notices: the first one syncs at once, the next ones within
|
||||
// NOTICE_WINDOW_MS fold into one sync at its end, which opens a new window.
|
||||
// The window lasts until its timer fires, even a late one: a single timer
|
||||
// is ever armed, so no notice can sync inside a window.
|
||||
function armWindow() {
|
||||
windowEnd = deps.now() + NOTICE_WINDOW_MS;
|
||||
windowTimer = deps.setTimer(() => {
|
||||
windowTimer = null;
|
||||
if (noticePending) {
|
||||
noticePending = false;
|
||||
launchSync();
|
||||
armWindow();
|
||||
} else {
|
||||
windowEnd = 0;
|
||||
}
|
||||
}, NOTICE_WINDOW_MS);
|
||||
}
|
||||
|
||||
function notice() {
|
||||
if (windowTimer !== null && deps.now() < windowEnd) {
|
||||
if (windowTimer !== null) {
|
||||
noticePending = true;
|
||||
return;
|
||||
}
|
||||
|
|
@ -277,7 +274,7 @@ export function createSyncSocket(deps: SyncSocketDeps): SyncSocket {
|
|||
return;
|
||||
}
|
||||
state = 'connecting';
|
||||
socket = deps.connect(r.ticket, {
|
||||
const handlers: SocketHandlers = {
|
||||
open() {
|
||||
if (g !== gen) return;
|
||||
state = 'open';
|
||||
|
|
@ -302,7 +299,13 @@ export function createSyncSocket(deps: SyncSocketDeps): SyncSocket {
|
|||
log('closed');
|
||||
fail();
|
||||
},
|
||||
});
|
||||
};
|
||||
try {
|
||||
socket = deps.connect(r.ticket, handlers);
|
||||
} catch {
|
||||
// The WebSocket constructor refused: same as a failed connection
|
||||
fail();
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
|
|
|
|||
|
|
@ -191,6 +191,25 @@ test('reconnection delays: 1, 2, 4 … s, capped at 60 s', async () => {
|
|||
assert.deepEqual(delays, [1000, 2000, 4000, 8000, 16000, 32000, 60000, 60000, 60000, 60000]);
|
||||
});
|
||||
|
||||
test('a throwing WebSocket constructor counts as a failure', async () => {
|
||||
const h = harness();
|
||||
let calls = 0;
|
||||
const s = createSyncSocket({
|
||||
now: h.clock.now, setTimer: h.clock.setTimer, clearTimer: h.clock.clearTimer, random: () => 0.5,
|
||||
canOpen: () => true,
|
||||
requestTicket: async () => ({ kind: 'ok', ticket: 't' }),
|
||||
connect: () => {
|
||||
calls++;
|
||||
throw new Error('bad url');
|
||||
},
|
||||
sync: () => {},
|
||||
});
|
||||
s.start();
|
||||
await h.clock.advance(3000);
|
||||
assert.equal(calls, 3);
|
||||
assert.equal(s.state(), 'waiting');
|
||||
});
|
||||
|
||||
test('a failed ticket (network, 5xx) reconnects with backoff too', async () => {
|
||||
const h = harness({ tickets: () => ({ kind: 'failed' }) });
|
||||
h.socket.start();
|
||||
|
|
@ -479,6 +498,34 @@ test('a steady stream of notices: never more than one sync per 20 s', async () =
|
|||
for (const g of gaps(h.syncs)) assert.ok(g >= NOTICE_WINDOW_MS, `gap ${g} under 20 s`);
|
||||
});
|
||||
|
||||
test('late timers (busy JS thread): still one sync per 20 s at most', async () => {
|
||||
// Every timer fires 500 ms late, as on a busy JS thread: a notice can land
|
||||
// after the nominal end of the window but before its timer (review of #175)
|
||||
const h = harness();
|
||||
const setTimer = h.clock.setTimer;
|
||||
h.clock.setTimer = (fn, ms) => setTimer(fn, ms + 500);
|
||||
const s = createSyncSocket({
|
||||
now: h.clock.now, setTimer: h.clock.setTimer, clearTimer: h.clock.clearTimer, random: () => 0.5,
|
||||
canOpen: () => true,
|
||||
requestTicket: async () => ({ kind: 'ok', ticket: 't' }),
|
||||
connect(ticket, handlers) {
|
||||
h.sockets.push({ handlers, close() {} });
|
||||
return { close() {} };
|
||||
},
|
||||
sync: () => h.syncs.push(h.clock.now()),
|
||||
});
|
||||
s.start();
|
||||
await flush();
|
||||
h.last.handlers.open();
|
||||
h.last.handlers.message(SYNC);
|
||||
await h.clock.advance(NOTICE_WINDOW_MS + 200);
|
||||
for (let t = 0; t < 100_000; t += 1000) {
|
||||
h.last.handlers.message(SYNC);
|
||||
await h.clock.advance(1000);
|
||||
}
|
||||
for (const g of gaps(h.syncs)) assert.ok(g >= NOTICE_WINDOW_MS, `gap ${g} under 20 s`);
|
||||
});
|
||||
|
||||
test('unknown or unreadable messages are ignored', async () => {
|
||||
const h = harness();
|
||||
await openSocket(h);
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue