Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
12 changes: 11 additions & 1 deletion src/client.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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));
}
}

/**
Expand Down
22 changes: 22 additions & 0 deletions test/client.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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<void>(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));
}
}
});