-
Notifications
You must be signed in to change notification settings - Fork 13.8k
Add arbitrary API request batching #63937
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
base: main
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -23,6 +23,9 @@ import { | |
| } from "../options.ts"; | ||
| import type { | ||
| APIMethodInfo, | ||
| APIRequest, | ||
| BatchRequestsParams, | ||
| BatchRequestsResponse, | ||
| SourceFileResponseMethod, | ||
| } from "../proto.ts"; | ||
| import { | ||
|
|
@@ -47,6 +50,8 @@ export class Client { | |
| private options: ClientOptions; | ||
| private connected = false; | ||
| private timing: TimingCollector | undefined; | ||
| private batchedRequests: { method: APIRequest["method"]; params: APIRequest["params"]; resolve: (value: unknown) => void; reject: (reason?: any) => void; }[] = []; | ||
| private nextBatch: NodeJS.Immediate | "manual" | undefined; | ||
|
|
||
| constructor(options: ClientOptions) { | ||
| this.options = options; | ||
|
|
@@ -158,15 +163,11 @@ export class Client { | |
| } | ||
| } | ||
|
|
||
| async apiRequest<K extends keyof APIMethodInfo>(method: K, params: APIMethodInfo[K]["params"]): Promise<APIMethodInfo[K]["result"]> { | ||
| if (!this.connected) { | ||
| await this.connect(); | ||
| } | ||
| private async sendRequestWithTiming<TResponse>(requestType: RequestType<unknown, TResponse, void>, params: unknown): Promise<TResponse> { | ||
| if (!this.connection) { | ||
| throw new Error("Connection not established"); | ||
| } | ||
|
|
||
| const requestType = new RequestType<unknown, APIMethodInfo[K]["result"], void>(method); | ||
| if (!this.timing) { | ||
| return this.connection.sendRequest(requestType, params); | ||
| } | ||
|
|
@@ -180,7 +181,7 @@ export class Client { | |
| const result = await this.connection.sendRequest(requestType, params); | ||
| const roundTripMs = performance.now() - start; | ||
| this.timing.record({ | ||
| method, | ||
| method: requestType.method, | ||
| roundTripMs, | ||
| bytesSent, | ||
| bytesReceived: result === undefined || result === null | ||
|
|
@@ -190,6 +191,83 @@ export class Client { | |
| return result; | ||
| } | ||
|
|
||
| private async doBatch(): Promise<void> { | ||
| this.nextBatch = undefined; | ||
| if (!this.batchedRequests.length) return; | ||
| const requests = this.batchedRequests; | ||
| this.batchedRequests = []; | ||
| try { | ||
| if (!this.connected) { | ||
| await this.connect(); | ||
| } | ||
| if (!this.connection) { | ||
| throw new Error("Connection not established"); | ||
| } | ||
|
|
||
| if (requests.length === 1) { | ||
| // send single queued requests directly instead of as a batched request | ||
| const requestType = new RequestType<unknown, unknown, void>(requests[0].method); | ||
| const response = await this.sendRequestWithTiming(requestType, requests[0].params); | ||
| requests[0].resolve(response); | ||
| return; | ||
| } | ||
|
|
||
| const requestType = new RequestType<unknown, BatchRequestsResponse, void>("batchRequests"); | ||
| const params: BatchRequestsParams = { requests: requests.map(request => ({ method: request.method, params: request.params })) }; | ||
| const response = await this.sendRequestWithTiming(requestType, params); | ||
| for (let i = 0; i < requests.length; i++) { | ||
| const { resolve, reject } = requests[i]; | ||
| const item = response.responses[i]; | ||
| if (item.error !== undefined) { | ||
| reject(new Error(item.error)); | ||
| } | ||
| else { | ||
| resolve(item.result); | ||
| } | ||
| } | ||
| } | ||
| catch (error) { | ||
| for (const { reject } of requests) reject(error); | ||
| } | ||
| } | ||
|
|
||
| private scheduleImmediateBatch(): void { | ||
| if (this.nextBatch) return; | ||
| this.nextBatch = setImmediate(this.doBatch.bind(this)); | ||
| } | ||
|
|
||
| batchContext(): { [Symbol.dispose](): void; } { | ||
| if (this.nextBatch === "manual") { | ||
| throw new Error("Already in a manual batch context"); | ||
| } | ||
| if (this.nextBatch) { | ||
| clearImmediate(this.nextBatch); | ||
| this.doBatch(); // empty the queue before entering a manual batch context | ||
| } | ||
| this.nextBatch = "manual"; | ||
| return { | ||
| [Symbol.dispose]: () => { | ||
| this.nextBatch = undefined; | ||
| this.scheduleImmediateBatch(); | ||
| }, | ||
| }; | ||
| } | ||
|
|
||
| async apiRequest<K extends keyof APIMethodInfo>(method: K, params: APIMethodInfo[K]["params"]): Promise<APIMethodInfo[K]["result"]> { | ||
| if (!this.connected) { | ||
| await this.connect(); | ||
| } | ||
| if (!this.connection) { | ||
| throw new Error("Connection not established"); | ||
| } | ||
|
|
||
| const resultPromise = new Promise<APIMethodInfo[K]["result"]>((resolve, reject) => { | ||
| this.batchedRequests.push({ method, params, resolve, reject }); | ||
| this.scheduleImmediateBatch(); | ||
|
weswigham marked this conversation as resolved.
Comment on lines
+264
to
+266
Member
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Preexisting issue, but... probably real. cc Andrew Branch (@andrewbranch) - I don't think there's a guard to ensure only one logical continuation is in the "connecting" state, since
Member
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. (And I'm actually sidestepping the problem in this design by deferring |
||
| }); | ||
| return resultPromise; | ||
| } | ||
|
|
||
| async apiRequestBinary<K extends SourceFileResponseMethod>(method: K, params: APIMethodInfo[K]["params"]): Promise<Uint8Array | undefined> { | ||
| const response = await this.apiRequest(method, params); | ||
| if (!response) return undefined; | ||
|
|
||
Uh oh!
There was an error while loading. Please reload this page.