-
Notifications
You must be signed in to change notification settings - Fork 0
/
pool-executions.ts
94 lines (82 loc) · 2.49 KB
/
pool-executions.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
import { noop } from '@proc7ts/primitives';
import os from 'node:os';
import { AbortedZExecutionError } from './aborted-execution-error.js';
import type { ZExecutionStarter } from './exec.js';
import { execZ } from './exec.js';
import type { ZExecution } from './execution.js';
import { failZ } from './fail.js';
/**
* Constructs execution pool.
*
* @typeParam TResult - Execution result type.
* @param maxRunning - The maximum number of simultaneously running executions. Zero or negative value means no limit.
* Equals to the number of CPUs by default.
*
* @returns A function accepting execution starter and returning started or pending execution.
*/
export function poolZExecutions<TResult>(
maxRunning = os.cpus().length,
): (this: void, starter: ZExecutionStarter<TResult>) => ZExecution<TResult> {
if (maxRunning <= 0) {
return execZ;
}
const queue: (() => void)[] = [];
const whenReady = (): Promise<void> => {
if (maxRunning > 0) {
// Reduce the number of simultaneous executions and execute immediately
--maxRunning;
return Promise.resolve();
}
// Enqueue execution
return new Promise(resolve => {
queue.push(resolve);
});
};
const execEnd = (): void => {
const next = queue.shift();
if (next) {
// Execute next queued
// The number of running jobs is not changed
next();
} else {
// No more executions in queue
// Allow one more simultaneous execution
++maxRunning;
}
};
return starter => execZ(() => {
let start: () => void = noop;
let dontStart: (error: unknown) => void = noop;
const whenStarted = (): Promise<void> => new Promise<void>((resolve, reject) => {
start = resolve;
dontStart = reject;
});
let abort = (): void => {
const aborted = new AbortedZExecutionError();
starter = () => failZ<TResult>(aborted);
dontStart(aborted);
};
const ready = whenReady();
return {
whenStarted() {
return whenStarted();
},
whenDone() {
return ready
.then(() => {
const execution = execZ(starter);
abort = () => execution.abort();
execution.whenStarted().then(
() => start(),
error => dontStart(error),
);
return execution.whenDone();
})
.finally(execEnd);
},
abort() {
abort();
},
};
});
}