From b49d965cdd74266823e8311040b4b08afc7274a0 Mon Sep 17 00:00:00 2001 From: frederico santos Date: Wed, 14 Oct 2026 13:56:35 +0200 Subject: [PATCH] fix(client): retry HTTP 429 with Retry-After/backoff Co-Authored-By: Claude Sonnet 5.5 --- CHANGELOG.md | 3 +++ src/client.ts | 12 +++++++++++- test/client.test.ts | 22 ++++++++++++++++++++++ 3 files changed, 36 insertions(+), 1 deletion(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 918e6a9..b67b031 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -7,6 +7,9 @@ All notable changes to this project will be documented in this file. ### Changed - **`olcli comments list` (and `resolve`, `reopen`, `delete`, which look the thread up first) joins docs over four sockets instead of one doc at a time** - the command joined every doc in the project serially at about 0.25 s each, so a 43-doc project took around 14 s. One socket cannot overlap requests, since the long-poll gives an acknowledgment to whichever request polls first and the others time out, so the docs are spread across a small pool of sockets instead +### Fixed +- **Requests that Overleaf rejects with HTTP 429 are retried (up to 5 times, honoring `Retry-After`, else a 1 s to 16 s exponential backoff with jitter, about 30 s in total) instead of failing at once** - every CLI invocation fetches `/project` for a CSRF token, so a short burst of commands (for example scripted `comments add` calls) got `Failed to fetch projects page: 429`. A 429 means the request was not processed, so retrying is safe for every method + ## [0.13.1] - 2026-10-09 ### Fixed diff --git a/src/client.ts b/src/client.ts index 94f6427..c707455 100644 --- a/src/client.ts +++ b/src/client.ts @@ -576,7 +576,17 @@ export class OverleafClient { }); }; - return doRequest(url, maxRedirects); + // Overleaf answers bursts with 429 (rejected, not processed), so retrying is safe for any method. + // It sends no Retry-After in practice and its limit lasts ~25 s, so back off 1, 2, 4, 8, 16 s (about + // 30 s in total) with +/-25% jitter so parallel callers do not retry in lockstep. Retry-After wins if present. + for (let attempt = 0; ; attempt++) { + const res = await doRequest(url, maxRedirects); + if (res.status !== 429 || attempt >= 5) return res; + const retryAfter = Number(res.headers['retry-after']); + const waitMs = Number.isFinite(retryAfter) ? retryAfter * 1000 : 1000 * 2 ** attempt * (0.75 + Math.random() * 0.5); + this.logVerbose(`${method} ${url} -> 429, retrying in ${Math.round(waitMs)}ms (attempt ${attempt + 1}/5)`); + await new Promise(resolve => setTimeout(resolve, waitMs)); + } } /** diff --git a/test/client.test.ts b/test/client.test.ts index 27bcdf1..a7d6b29 100644 --- a/test/client.test.ts +++ b/test/client.test.ts @@ -153,3 +153,25 @@ test('listComments joins docs over several sockets, not one doc at a time on one assert.equal(open, 0, 'every socket is closed'); assert.deepEqual(comments.map((c: any) => [c.threadId, c.path]), [['thread7', 'f7.tex']]); }); + +test('httpRequest retries 429 (honoring Retry-After) and gives up after 5 retries', async () => { + for (const [failures, expectedStatus, expectedCalls] of [[2, 200, 3], [99, 429, 6]] as const) { + let calls = 0; + const server = createServer((_req, res) => { + calls++; + const limited = calls <= failures; + res.writeHead(limited ? 429 : 200, { 'Content-Type': 'application/json', ...(limited ? { 'Retry-After': '0' } : {}) }); + res.end('{}'); + }); + await new Promise(resolve => server.listen(0, '127.0.0.1', resolve)); + try { + const { port } = server.address() as { port: number }; + const client = new OverleafClient({ cookies: { session: 's' }, csrf: 'c', baseUrl: `http://127.0.0.1:${port}` }); + const res = await (client as any).httpRequest(`http://127.0.0.1:${port}/x`, { expect: 'json' }); + assert.equal(res.status, expectedStatus); + assert.equal(calls, expectedCalls); + } finally { + await new Promise(resolve => server.close(resolve)); + } + } +});