Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 12 additions & 0 deletions packages/libsql-client-wasm/src/wasm.ts
Original file line number Diff line number Diff line change
Expand Up @@ -219,10 +219,22 @@ export class Sqlite3Client implements Client {
);
}

async reconnect(): Promise<void> {
try {
if (!this.closed && this.#db !== null) {
this.#db.close();
}
} finally {
this.#db = new this.#sqlite3.oo1.DB(this.#path, "c");
this.closed = false;
}
}

close(): void {
this.closed = true;
if (this.#db !== null) {
this.#db.close();
this.#db = null;
}
}

Expand Down
38 changes: 34 additions & 4 deletions packages/libsql-client/src/http.ts
Original file line number Diff line number Diff line change
Expand Up @@ -74,6 +74,10 @@ const sqlCacheCapacity = 30;
export class HttpClient implements Client {
#client: hrana.HttpClient;
protocol: "http";
#url: URL;
#intMode: IntMode;
#customFetch: Function | undefined;
#concurrency: number;
#authToken: string | undefined;
#promiseLimitFunction: ReturnType<typeof promiseLimit<any>>;

Expand All @@ -85,11 +89,20 @@ export class HttpClient implements Client {
customFetch: Function | undefined,
concurrency: number,
) {
this.#client = hrana.openHttp(url, authToken, customFetch);
this.#client.intMode = intMode;
this.protocol = "http";
this.#url = url;
this.#authToken = authToken;
this.#promiseLimitFunction = promiseLimit<any>(concurrency);
this.#intMode = intMode;
this.#customFetch = customFetch;
this.#concurrency = concurrency;

this.#client = hrana.openHttp(
this.#url,
this.#authToken,
this.#customFetch,
);
this.#client.intMode = this.#intMode;
this.protocol = "http";
this.#promiseLimitFunction = promiseLimit<any>(this.#concurrency);
}

private async limit<T>(fn: () => Promise<T>): Promise<T> {
Expand Down Expand Up @@ -267,6 +280,23 @@ export class HttpClient implements Client {
this.#client.close();
}

async reconnect(): Promise<void> {
try {
if (!this.closed) {
// Abort in-flight ops and free resources
this.#client.close();
}
} finally {
// Recreate the underlying hrana client
this.#client = hrana.openHttp(
this.#url,
this.#authToken,
this.#customFetch,
);
this.#client.intMode = this.#intMode;
}
}

get closed(): boolean {
return this.#client.closed;
}
Expand Down
12 changes: 12 additions & 0 deletions packages/libsql-client/src/sqlite3.ts
Original file line number Diff line number Diff line change
Expand Up @@ -222,10 +222,22 @@ export class Sqlite3Client implements Client {
} as Replicated;
}

async reconnect(): Promise<void> {
try {
if (!this.closed && this.#db !== null) {
this.#db.close();
}
} finally {
this.#db = new Database(this.#path, this.#options);
this.closed = false;
}
}

close(): void {
this.closed = true;
if (this.#db !== null) {
this.#db.close();
this.#db = null;
}
}

Expand Down
36 changes: 36 additions & 0 deletions packages/libsql-client/src/ws.ts
Original file line number Diff line number Diff line change
Expand Up @@ -403,6 +403,35 @@ export class WsClient implements Client {
}
}

async reconnect(): Promise<void> {
try {
for (const st of Array.from(this.#connState.streamStates)) {
try {
st.stream.close();
} catch {}
}
this.#connState.client.close();
} catch {}

if (this.#futureConnState) {
try {
this.#futureConnState.client.close();
} catch {}
this.#futureConnState = undefined;
}

const next = this.#openConn();
const version = await next.client.getVersion();

next.useSqlCache = version >= 2;
if (next.useSqlCache) {
next.sqlCache.capacity = sqlCacheCapacity;
}

this.#connState = next;
this.closed = false;
}

_closeStream(streamState: StreamState): void {
streamState.stream.close();

Expand All @@ -421,6 +450,13 @@ export class WsClient implements Client {
close(): void {
this.#connState.client.close();
this.closed = true;
if (this.#futureConnState) {
try {
this.#futureConnState.client.close();
} catch {}
this.#futureConnState = undefined;
}
this.closed = true;
}
}

Expand Down
4 changes: 4 additions & 0 deletions packages/libsql-core/src/api.ts
Original file line number Diff line number Diff line change
Expand Up @@ -240,6 +240,10 @@ export interface Client {
*/
close(): void;

/** Reconnect after the client has been closed.
*/
reconnect(): void;

/** Is the client closed?
*
* This is set to `true` after a call to {@link close} or if the client encounters an unrecoverable
Expand Down