fix(sync): read a missing Retry-After as null, not 0
Number(null) and Number('') are 0: a 429 without a Retry-After header
made initialMerge resend the same batch three times with no wait instead
of waiting 60 s. The header is now read by parseRetryAfter (syncRules.ts,
tested under node), and the smoke guard locks its use in postBatch.
Found by the autopilot self-review of PR #159.
Refs #151
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
parent
859376b54a
commit
e784494270
4 changed files with 28 additions and 2 deletions
|
|
@ -20,6 +20,7 @@ import {
|
|||
MAX_PUSH_BATCHES,
|
||||
classifyOutbox,
|
||||
isSynced,
|
||||
parseRetryAfter,
|
||||
sendInBatches,
|
||||
type PostResponse,
|
||||
} from '@/src/services/syncRules';
|
||||
|
|
@ -70,8 +71,7 @@ async function postBatch(batch: SyncOperation[], headers: Record<string, string>
|
|||
});
|
||||
if (!res.ok) {
|
||||
console.warn(`[sync] push failed with status ${res.status}`);
|
||||
const header = Number(res.headers.get('Retry-After'));
|
||||
return { status: res.status, retryAfter: Number.isFinite(header) && header >= 0 ? header : null, body: null };
|
||||
return { status: res.status, retryAfter: parseRetryAfter(res.headers.get('Retry-After')), body: null };
|
||||
}
|
||||
const body = await res.json().catch(() => null);
|
||||
return { status: res.status, retryAfter: null, body };
|
||||
|
|
|
|||
|
|
@ -346,6 +346,18 @@ export function classifyOutbox(
|
|||
return { markIds, keptRefusals, droppedRefusals: dropped.length, droppedByType };
|
||||
}
|
||||
|
||||
/**
|
||||
* Retry-After header in seconds, or null when absent, empty, negative or not
|
||||
* a number of seconds (an HTTP date included). Number(null) and Number('')
|
||||
* are 0, hence the explicit checks: a missing header must mean null (wait
|
||||
* RETRY_AFTER_MAX_S), not 0.
|
||||
*/
|
||||
export function parseRetryAfter(header: string | null | undefined): number | null {
|
||||
if (header == null || header.trim() === '') return null;
|
||||
const seconds = Number(header);
|
||||
return Number.isFinite(seconds) && seconds >= 0 ? seconds : null;
|
||||
}
|
||||
|
||||
/** Response of one POST, as seen by sendInBatches. */
|
||||
export interface PostResponse {
|
||||
status: number;
|
||||
|
|
|
|||
|
|
@ -209,6 +209,8 @@ check('syncClient.ts passes the #151 push bounds to sendInBatches', () => {
|
|||
assert.match(merge, /size: BATCH_SIZE,/);
|
||||
assert.match(body('pushChanges'), /\.orderBy\(asc\(syncOutbox\.createdAt\)\)/);
|
||||
assert.match(body('pushChanges'), /classifyOutbox\(unsynced, results,/);
|
||||
// Number(null) is 0: a missing Retry-After must go through parseRetryAfter
|
||||
assert.match(src, /retryAfter: parseRetryAfter\(res\.headers\.get\('Retry-After'\)\)/);
|
||||
});
|
||||
|
||||
// --- Build toolchain guards ---
|
||||
|
|
|
|||
|
|
@ -26,6 +26,7 @@ import {
|
|||
RETRY_AFTER_MAX_S,
|
||||
MAX_429_RETRIES,
|
||||
parseResults,
|
||||
parseRetryAfter,
|
||||
isSynced,
|
||||
classifyOutbox,
|
||||
sendInBatches,
|
||||
|
|
@ -379,6 +380,17 @@ test('parseResults: the results array, or null for another shape', () => {
|
|||
assert.equal(parseResults({ results: [{ idempotencyKey: 'a', status: 'applied' }, null] }), null);
|
||||
});
|
||||
|
||||
test('parseRetryAfter: seconds, or null when absent, empty, negative or a date', () => {
|
||||
assert.equal(parseRetryAfter('7'), 7);
|
||||
assert.equal(parseRetryAfter('0'), 0);
|
||||
assert.equal(parseRetryAfter(null), null);
|
||||
assert.equal(parseRetryAfter(undefined), null);
|
||||
assert.equal(parseRetryAfter(''), null);
|
||||
assert.equal(parseRetryAfter(' '), null);
|
||||
assert.equal(parseRetryAfter('-5'), null);
|
||||
assert.equal(parseRetryAfter('Wed, 21 Oct 2026 07:28:00 GMT'), null);
|
||||
});
|
||||
|
||||
// Fake transport: one response (or thrown error) per call, in order.
|
||||
function transport(responses) {
|
||||
const calls = [];
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue