-
Notifications
You must be signed in to change notification settings - Fork 3
/
pool.ts
177 lines (150 loc) · 4.83 KB
/
pool.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
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
import os from 'node:os';
import { Options, Pool, createPool } from 'generic-pool';
import { Env, Util } from '@travetto/base';
import { WorkQueue } from './queue';
type ItrSource<I> = Iterable<I> | AsyncIterable<I>;
/**
* Worker definition
*/
export interface Worker<I, O = unknown> {
active?: boolean;
id?: unknown;
init?(): Promise<unknown>;
execute(input: I, idx: number): Promise<O>;
destroy?(): Promise<void>;
release?(): unknown;
}
type WorkerInput<I, O> = (() => Worker<I, O>) | ((input: I, inputIdx: number) => Promise<O>);
type WorkPoolConfig<I, O> = Options & {
onComplete?: (output: O, input: I, finishIdx: number) => void;
onError?(ev: Error, input: I, finishIdx: number): (unknown | Promise<unknown>);
shutdown?: AbortSignal;
};
const isWorkerFactory = <I, O>(o: WorkerInput<I, O>): o is (() => Worker<I, O>) => o.length === 0;
/**
* Work pool support
*/
export class WorkPool {
static MAX_SIZE = os.availableParallelism();
static DEFAULT_SIZE = Math.min(WorkPool.MAX_SIZE, 4);
/** Build worker pool */
static #buildPool<I, O>(worker: WorkerInput<I, O>, opts?: WorkPoolConfig<I, O>): Pool<Worker<I, O>> {
let pendingAcquires = 0;
const trace = /@travetto\/worker/.test(Env.DEBUG.val ?? '');
// Create the pool
const pool = createPool({
async create() {
try {
pendingAcquires += 1;
const res = isWorkerFactory(worker) ? await worker() : { execute: worker };
res.id ??= Util.uuid();
if (res.init) {
await res.init();
}
return res;
} finally {
pendingAcquires -= 1;
}
},
async destroy(x) {
if (trace) {
console.debug('Destroying', { pid: process.pid, worker: x.id });
}
return x.destroy?.();
},
validate: async (x: Worker<I, O>) => x.active ?? true
}, {
max: WorkPool.DEFAULT_SIZE,
min: 1,
evictionRunIntervalMillis: 5000,
...(opts ?? {}),
});
// Listen for shutdown
opts?.shutdown?.addEventListener('abort', async () => {
while (pendingAcquires) {
await Util.nonBlockingTimeout(10);
}
await pool.drain();
await pool.clear();
});
return pool;
}
/**
* Process a given input source and worker, and fire on completion
*/
static async run<I, O>(workerFactory: WorkerInput<I, O>, src: ItrSource<I>, opts: WorkPoolConfig<I, O> = {}): Promise<void> {
const trace = /@travetto\/worker/.test(Env.DEBUG.val ?? '');
const pending = new Set<Promise<unknown>>();
const errors: Error[] = [];
let inputIdx = 0;
let finishIdx = 0;
const pool = this.#buildPool(workerFactory, opts);
for await (const nextInput of src) {
const worker = await pool.acquire()!;
if (trace) {
console.debug('Acquired', { pid: process.pid, worker: worker.id });
}
const completion = worker.execute(nextInput, inputIdx += 1)
.then(v => opts.onComplete?.(v, nextInput, finishIdx += 1))
.catch(err => {
errors.push(err);
opts?.onError?.(err, nextInput, finishIdx += 1);
}) // Catch error
.finally(async () => {
if (trace) {
console.debug('Releasing', { pid: process.pid, worker: worker.id });
}
try {
if (worker.active ?? true) {
try {
await worker.release?.();
} catch { }
await pool.release(worker);
} else {
await pool.destroy(worker);
}
} catch { }
});
completion.finally(() => pending.delete(completion));
pending.add(completion);
}
await Promise.all(Array.from(pending));
if (errors.length) {
throw errors[0];
}
}
/**
* Process a given input source as an async iterable
*/
static runStream<I, O>(worker: WorkerInput<I, O>, input: ItrSource<I>, opts?: WorkPoolConfig<I, O>): AsyncIterable<O> {
const itr = new WorkQueue<O>();
const res = this.run(worker, input, {
...opts,
onComplete: (ev, inp, finishIdx) => {
itr.add(ev);
opts?.onComplete?.(ev, inp, finishIdx);
}
});
res.finally(() => itr.close());
return itr;
}
/**
* Process a given input source as an async iterable with progress information
*/
static runStreamProgress<I, O>(worker: WorkerInput<I, O>, input: ItrSource<I>, total: number, opts?: WorkPoolConfig<I, O>): AsyncIterable<{
idx: number;
value: O;
total: number;
}> {
const itr = new WorkQueue<{ idx: number, value: O, total: number }>();
const res = this.run(worker, input, {
...opts,
onComplete: (ev, inp, finishIdx) => {
itr.add({ value: ev, idx: finishIdx, total });
opts?.onComplete?.(ev, inp, finishIdx);
}
});
res.finally(() => itr.close());
return itr;
}
}