Refresh: one attempt per identity per process; waiters share its outcome

Callers that arrive while a refresh of one linked identity is in flight in
this process join it and receive its success or failure, instead of queuing
on the row lock and presenting the same refresh token again behind a failed
holder. The entry lives as long as the attempt, so a later request retries.
The store lock stays the cross-process guard; the residual risk (one
presentation per process per burst) is written into the service contract.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
Meta Vens 2026-10-19 12:34:39 +02:00
commit b15bf33b21
3 changed files with 71 additions and 4 deletions

View file

@ -60,18 +60,31 @@ function withoutTokens(identity: StoredLinkedIdentity): StoredLinkedIdentity {
return rest;
}
/**
* Refreshes in flight in this process, per store and linked identity. The row
* lock serialises processes; it cannot tell a waiter that queued behind a
* failed holder from a caller that arrived after the failure, so each waiter
* would present the same refresh token again. Callers that arrive while an
* attempt is in flight join it and receive its outcome, success or failure.
* The entry lives exactly as long as the attempt: a later request retries.
*
* Accepted residual risk (docs/service-contract.md): one presentation per
* process per burst, so N processes may present one token N times.
*/
const refreshesInFlight = new WeakMap<HeimdallStore, Map<string, Promise<ManagedCredentialOutcome>>>();
/**
* The one refresh policy. `snapshot` is a hint: it may skip the lock when no
* refresh is due and never decides one. The row's lock owns when a refresh
* token is presented; the decision is made on the row as read under it.
* token is presented across processes; this process makes one attempt per
* identity at a time; the decision is made on the row as read under the lock.
*/
export async function refreshManagedCredential(
ctx: ManagedCredentialContext,
snapshot: StoredLinkedIdentity
): Promise<ManagedCredentialOutcome> {
const clock = ctx.clock ?? Date.now;
const provider = snapshot.provider;
const runtime = ctx.oauthRuntimes[provider];
const runtime = ctx.oauthRuntimes[snapshot.provider];
if (!snapshot.accessTokenEncrypted) {
return { kind: "revoked", identity: snapshot };
@ -80,6 +93,30 @@ export async function refreshManagedCredential(
return { kind: "current", identity: snapshot };
}
let inFlight = refreshesInFlight.get(ctx.store);
if (!inFlight) {
inFlight = new Map();
refreshesInFlight.set(ctx.store, inFlight);
}
const joined = inFlight.get(snapshot.id);
if (joined) {
return joined;
}
const attempt = refreshUnderLock(ctx, snapshot).finally(() => {
inFlight.delete(snapshot.id);
});
inFlight.set(snapshot.id, attempt);
return attempt;
}
async function refreshUnderLock(
ctx: ManagedCredentialContext,
snapshot: StoredLinkedIdentity
): Promise<ManagedCredentialOutcome> {
const clock = ctx.clock ?? Date.now;
const provider = snapshot.provider;
const runtime = ctx.oauthRuntimes[provider];
let revocation: ManagedCredentialRevocationReason | undefined;
const decide = async (locked: StoredLinkedIdentity): Promise<CredentialDecision> => {
if (!locked.accessTokenEncrypted || !dueForRefresh(locked, runtime, clock())) {