fix(server): renew service keys on the next request and stop sending expired ones

An expired key is no longer sent; the request goes through the shared pool
while a due key, or an expired one within its grace, is renewed in the
background. Refusals in one burst count once, a refused renewal counts like
a refused search, and a Places request whose key is refused is sent once
more without it.
This commit is contained in:
Maurice
2026-10-29 09:13:26 +01:00
committed by Maurice
parent b53751344b
commit 476dbef7dc
6 changed files with 263 additions and 51 deletions
+15 -6
View File
@@ -255,15 +255,24 @@ function noteFailure(err: unknown, breaker: Breaker): void {
}
}
/** A request carrying who is asking: the instance token, and the installation key where it may go. */
/**
* A request carrying who is asking: the instance token, and the installation
* key where it may go. A refused key costs no answer: the request goes once
* more without it, through the shared pool, while the identity counts the refusal.
*/
async function identifiedFetch(url: URL, headers: Record<string, string>, signal: AbortSignal): Promise<Response> {
const identity = serviceIdentitySlot.get();
const res = await fetch(url, {
signal,
headers: { ...headers, 'X-TREK-Instance': instanceToken(), ...identity.headersFor(url) },
});
const own = identity.headersFor(url);
const base = { ...headers, 'X-TREK-Instance': instanceToken() };
const res = await fetch(url, { signal, headers: { ...base, ...own } });
identity.observe(url, res);
return res;
if (res.status !== 401 || !own.Authorization) return res;
await res.body?.cancel();
return fetch(url, { signal, headers: { ...base, ...withoutKey(own) } });
}
function withoutKey(headers: Record<string, string>): Record<string, string> {
return Object.fromEntries(Object.entries(headers).filter(([name]) => name !== 'Authorization'));
}
function statusError(status: number): Error & { status: number } {