Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add concurrency limits #226

Merged
merged 8 commits into from
Jun 25, 2024
Merged
Show file tree
Hide file tree
Changes from 2 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
8 changes: 7 additions & 1 deletion package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

3 changes: 2 additions & 1 deletion packages/libsql-client/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -105,7 +105,8 @@
"@libsql/core": "^0.6.2",
"@libsql/hrana-client": "^0.6.0",
"js-base64": "^3.7.5",
"libsql": "^0.3.10"
"libsql": "^0.3.10",
"promise-limit": "^2.7.0"
},
"devDependencies": {
"@types/jest": "^29.2.5",
Expand Down
55 changes: 30 additions & 25 deletions packages/libsql-client/src/http.ts
Original file line number Diff line number Diff line change
Expand Up @@ -24,9 +24,12 @@ import {
getIsSchemaDatabase,
waitForLastMigrationJobToFinish,
} from "./migrations.js";
import promiseLimit from "promise-limit";

export * from "@libsql/core/api";

const limit = promiseLimit<ResultSet>(1);
giovannibenussi marked this conversation as resolved.
Show resolved Hide resolved

export function createClient(config: Config): Client {
return _createClient(expandConfig(config, true));
}
Expand Down Expand Up @@ -99,33 +102,35 @@ export class HttpClient implements Client {
}

async execute(stmt: InStatement): Promise<ResultSet> {
try {
const isSchemaDatabasePromise = this.getIsSchemaDatabase();
const hranaStmt = stmtToHrana(stmt);

// Pipeline all operations, so `hrana.HttpClient` can open the stream, execute the statement and
// close the stream in a single HTTP request.
let rowsPromise: Promise<hrana.RowsResult>;
const stream = this.#client.openStream();
return limit(async () => {
athoscouto marked this conversation as resolved.
Show resolved Hide resolved
try {
rowsPromise = stream.query(hranaStmt);
} finally {
stream.closeGracefully();
}

const rowsResult = await rowsPromise;
const isSchemaDatabase = await isSchemaDatabasePromise;
if (isSchemaDatabase) {
await waitForLastMigrationJobToFinish({
authToken: this.#authToken,
baseUrl: this.#url.origin,
});
const isSchemaDatabasePromise = this.getIsSchemaDatabase();
const hranaStmt = stmtToHrana(stmt);

// Pipeline all operations, so `hrana.HttpClient` can open the stream, execute the statement and
// close the stream in a single HTTP request.
let rowsPromise: Promise<hrana.RowsResult>;
const stream = this.#client.openStream();
try {
rowsPromise = stream.query(hranaStmt);
} finally {
stream.closeGracefully();
}

const rowsResult = await rowsPromise;
const isSchemaDatabase = await isSchemaDatabasePromise;
if (isSchemaDatabase) {
await waitForLastMigrationJobToFinish({
authToken: this.#authToken,
baseUrl: this.#url.origin,
});
}

return resultSetFromHrana(rowsResult);
} catch (e) {
throw mapHranaError(e);
}

return resultSetFromHrana(rowsResult);
} catch (e) {
throw mapHranaError(e);
}
});
}

async batch(
Expand Down
Loading