Drain the requests already on the wire before close() and unbind() tear down

This commit is contained in:
2026-08-30 20:08:36 +02:00
parent c06eda4582
commit bebd74b42b
13 changed files with 248 additions and 85 deletions
+31
View File
@@ -2,6 +2,7 @@
export class SendWindow {
private readonly limit: number;
private readonly waiting: (() => void)[] = [];
private readonly waitingForIdle: (() => void)[] = [];
private inFlight = 0;
constructor(limit: number) {
@@ -28,5 +29,35 @@ export class SendWindow {
}
this.inFlight--;
if (this.inFlight > 0) return;
for (const resolve of this.waitingForIdle.splice(0)) {
resolve();
}
}
/** Resolves once nothing is in flight, or on the timeout with how many still are. 0 never times out. */
idle(timeout: number): Promise<number> {
if (this.inFlight === 0) return Promise.resolve(0);
return new Promise<number>(resolve => {
let timer: NodeJS.Timeout | undefined = undefined;
const done = (): void => {
const index = this.waitingForIdle.indexOf(done);
if (timer) clearTimeout(timer);
if (index !== -1) this.waitingForIdle.splice(index, 1);
resolve(this.inFlight);
};
if (timeout > 0) {
timer = setTimeout(done, timeout);
timer.unref();
}
this.waitingForIdle.push(done);
});
}
}