serverless: Implement Connection.close()

This commit is contained in:
Pekka Enberg
2025-07-30 14:14:57 +03:00
parent 895f2acbfb
commit 1b8d95a79f
4 changed files with 51 additions and 3 deletions

View File

@@ -307,6 +307,11 @@ class LibSQLClient implements Client {
close(): void {
this._closed = true;
// Note: The libSQL client interface expects synchronous close,
// but our underlying session needs async close. We'll fire and forget.
this.session.close().catch(error => {
console.error('Error closing session:', error);
});
}
}

View File

@@ -90,6 +90,15 @@ export class Connection {
const sql = `PRAGMA ${pragma}`;
return this.session.execute(sql);
}
/**
* Close the connection.
*
* This sends a close request to the server to properly clean up the stream.
*/
async close(): Promise<void> {
await this.session.close();
}
}
/**

View File

@@ -52,9 +52,13 @@ export interface SequenceRequest {
sql: string;
}
export interface CloseRequest {
type: 'close';
}
export interface PipelineRequest {
baton: string | null;
requests: (ExecuteRequest | BatchRequest | SequenceRequest)[];
requests: (ExecuteRequest | BatchRequest | SequenceRequest | CloseRequest)[];
}
export interface PipelineResponse {
@@ -63,7 +67,7 @@ export interface PipelineResponse {
results: Array<{
type: 'ok' | 'error';
response?: {
type: 'execute' | 'batch' | 'sequence';
type: 'execute' | 'batch' | 'sequence' | 'close';
result?: ExecuteResult;
};
error?: {

View File

@@ -7,7 +7,8 @@ import {
type CursorResponse,
type CursorEntry,
type PipelineRequest,
type SequenceRequest
type SequenceRequest,
type CloseRequest
} from './protocol.js';
import { DatabaseError } from './error.js';
@@ -248,4 +249,33 @@ export class Session {
}
}
}
/**
* Close the session.
*
* This sends a close request to the server to properly clean up the stream
* before resetting the local state.
*/
async close(): Promise<void> {
// Only send close request if we have an active baton
if (this.baton) {
try {
const request: PipelineRequest = {
baton: this.baton,
requests: [{
type: "close"
} as CloseRequest]
};
await executePipeline(this.baseUrl, this.config.authToken, request);
} catch (error) {
// Ignore errors during close, as the connection might already be closed
console.error('Error closing session:', error);
}
}
// Reset local state
this.baton = null;
this.baseUrl = '';
}
}