0.0.4 release pool semaphore when terminating job

This commit is contained in:
Andrew Morris
2022-12-20 19:46:37 +11:00
parent f66406faf3
commit 1aeec54b71
2 changed files with 13 additions and 4 deletions

View File

@@ -21,7 +21,9 @@ export default class WorkerPool {
}
async use<T>(fn: (worker: Worker, terminate: () => void) => T): Promise<T> {
return await this.#semaphore.use(async () => {
const release = await this.#semaphore.acquire();
try {
let bestSlot = this.#slots[0];
for (let i = 1; i < this.size; i++) {
@@ -48,7 +50,14 @@ export default class WorkerPool {
);
}
return await bestSlot.use(fn);
});
return await bestSlot.use((worker, terminate) => {
return fn(worker, () => {
terminate();
release();
});
});
} finally {
release();
}
}
}