Skip to content

Commit

Permalink
Fix race condition on getIsSchemaDatabase cache
Browse files Browse the repository at this point in the history
We a client instance is used concurrently before the
isSchemaDatabasePromise is resolved, this led to multiple executions
of the underlying function. It should only be executed once, and the
promise should be shared among callers, which is what this change
enables.
  • Loading branch information
athoscouto committed Jun 19, 2024
1 parent 8da38aa commit 2e803ad
Show file tree
Hide file tree
Showing 2 changed files with 6 additions and 6 deletions.
6 changes: 3 additions & 3 deletions packages/libsql-client/src/http.ts
Original file line number Diff line number Diff line change
Expand Up @@ -71,7 +71,7 @@ export class HttpClient implements Client {
protocol: "http";
#url: URL;
#authToken: string | undefined;
#isSchemaDatabase: boolean | undefined;
#isSchemaDatabase: Promise<boolean> | undefined;

/** @private */
constructor(
Expand All @@ -87,9 +87,9 @@ export class HttpClient implements Client {
this.#authToken = authToken;
}

async getIsSchemaDatabase(): Promise<boolean> {
getIsSchemaDatabase(): Promise<boolean> {
if (this.#isSchemaDatabase === undefined) {
this.#isSchemaDatabase = await getIsSchemaDatabase({
this.#isSchemaDatabase = getIsSchemaDatabase({
authToken: this.#authToken,
baseUrl: this.#url.origin,
});
Expand Down
6 changes: 3 additions & 3 deletions packages/libsql-client/src/ws.ts
Original file line number Diff line number Diff line change
Expand Up @@ -124,7 +124,7 @@ export class WsClient implements Client {
#futureConnState: ConnState | undefined;
closed: boolean;
protocol: "ws";
#isSchemaDatabase: boolean | undefined;
#isSchemaDatabase: Promise<boolean> | undefined;

/** @private */
constructor(
Expand All @@ -142,9 +142,9 @@ export class WsClient implements Client {
this.protocol = "ws";
}

async getIsSchemaDatabase(): Promise<boolean> {
getIsSchemaDatabase(): Promise<boolean> {
if (this.#isSchemaDatabase === undefined) {
this.#isSchemaDatabase = await getIsSchemaDatabase({
this.#isSchemaDatabase = getIsSchemaDatabase({
authToken: this.#authToken,
baseUrl: this.#url.origin,
});
Expand Down

0 comments on commit 2e803ad

Please sign in to comment.