mirror of
https://github.com/mauriceboe/TREK.git
synced 2026-11-30 00:20:07 +00:00
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:
@@ -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 } {
|
||||
|
||||
Reference in New Issue
Block a user