Skip to content

Commit

Permalink
Prevent concurrent renewToken calls
Browse files Browse the repository at this point in the history
  • Loading branch information
fredli74 committed Oct 1, 2024
1 parent f38530a commit 8edd781
Showing 1 changed file with 15 additions and 0 deletions.
15 changes: 15 additions & 0 deletions providers/tesla/tesla-api.ts
Original file line number Diff line number Diff line change
Expand Up @@ -80,7 +80,20 @@ export class TeslaAPI {
}
}

/**
* WARNING: This function is not safe for multiple concurrent calls with the same refresh_token.
* Ensure that only one call is made at a time for each refresh_token.
* @throws Will throw an error if the function is called concurrently with the same refresh_token.
*/
private renewTokenLock: Set<string> = new Set();
public async renewToken(refresh_token: string): Promise<TeslaToken> {
if (this.renewTokenLock.has(refresh_token)) {
throw new RestClientError(
`Concurrent renewToken(${refresh_token}) calls are not allowed`,
500
);
}
this.renewTokenLock.add(refresh_token);
try {
log(LogLevel.Trace, `TeslaAPI.renewToken(${refresh_token})`);

Expand All @@ -96,6 +109,8 @@ export class TeslaAPI {
} catch (e) {
console.debug(`TeslaAPI.renewToken(${refresh_token}) error: ${e}`);
throw e;
} finally {
this.renewTokenLock.delete(refresh_token);
}
}

Expand Down

0 comments on commit 8edd781

Please sign in to comment.