Skip to content

Commit

Permalink
Add caching for pending token renewals
Browse files Browse the repository at this point in the history
  • Loading branch information
AlexTugarev authored and roboquat committed Jun 24, 2022
1 parent f0a38ff commit 1bb3ff9
Showing 1 changed file with 16 additions and 0 deletions.
16 changes: 16 additions & 0 deletions components/server/src/user/token-service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,23 @@ export class TokenService implements TokenProvider {
});
}

protected getTokenForHostCache = new Map<string, Promise<Token>>();

async getTokenForHost(user: User, host: string): Promise<Token> {
// (AT) when it comes to token renewal, the awaited http requests may
// cause "parallel" calls to repeat the renewal, which will fail.
// Caching for pending operations should solve this issue.
const key = `${host}-${user.id}`;
let promise = this.getTokenForHostCache.get(key);
if (!promise) {
promise = this.doGetTokenForHost(user, host);
this.getTokenForHostCache.set(key, promise);
promise.finally(() => this.getTokenForHostCache.delete(key));
}
return promise;
}

async doGetTokenForHost(user: User, host: string): Promise<Token> {
const identity = this.getIdentityForHost(user, host);
let token = await this.userDB.findTokenForIdentity(identity);
if (!token) {
Expand Down

0 comments on commit 1bb3ff9

Please sign in to comment.