@zukhruf/pool 0.8.19 → 0.8.21
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +275 -2
- package/dist/computer-store.d.ts +7 -0
- package/dist/computer-store.d.ts.map +1 -0
- package/dist/computer-store.js +29 -0
- package/dist/computer-store.js.map +1 -0
- package/dist/index.d.ts +3 -0
- package/dist/index.d.ts.map +1 -1
- package/dist/index.js +3 -0
- package/dist/index.js.map +1 -1
- package/dist/pool-workers.d.ts +33 -0
- package/dist/pool-workers.d.ts.map +1 -0
- package/dist/pool-workers.js +226 -0
- package/dist/pool-workers.js.map +1 -0
- package/dist/pool.d.ts +1 -1
- package/dist/pool.d.ts.map +1 -1
- package/dist/pool.js +20 -309
- package/dist/pool.js.map +1 -1
- package/dist/slots.d.ts +52 -0
- package/dist/slots.d.ts.map +1 -0
- package/dist/slots.js +339 -0
- package/dist/slots.js.map +1 -0
- package/dist/store.d.ts +24 -0
- package/dist/store.d.ts.map +1 -0
- package/dist/store.js +89 -0
- package/dist/store.js.map +1 -0
- package/dist/worker-kind.d.ts +19 -0
- package/dist/worker-kind.d.ts.map +1 -0
- package/dist/worker-kind.js +28 -0
- package/dist/worker-kind.js.map +1 -0
- package/dist/worker.d.ts +2 -1
- package/dist/worker.d.ts.map +1 -1
- package/package.json +8 -3
|
@@ -0,0 +1,226 @@
|
|
|
1
|
+
import { Latch, delay } from '@zukhruf/async';
|
|
2
|
+
import { SqliteElection } from '@zukhruf/election';
|
|
3
|
+
import { Slots } from "./slots.js";
|
|
4
|
+
import { Store, TOUCH_INTERVAL } from "./store.js";
|
|
5
|
+
// The read of the store that finds the jobs that no wake told of (ADR 0018).
|
|
6
|
+
const BACKSTOP = 1_000;
|
|
7
|
+
/**
|
|
8
|
+
* Offers this host's workers of one kind, and owns them while this host
|
|
9
|
+
* leads the election of the kind's store (ADR 0013). A standby starts no
|
|
10
|
+
* worker (ADR 0023). The owner takes as many jobs from the queue of its
|
|
11
|
+
* revision as it has free workers, runs each on a worker, and settles it with
|
|
12
|
+
* complete or fail (ADR 0017), so its sizing caps the jobs of its computer
|
|
13
|
+
* (ADR 0016). While a job runs, the owner touches it every 5 s, and kills its
|
|
14
|
+
* worker when the store took the job back or the kind's time limit ends. The
|
|
15
|
+
* disposal is a hand-over: it stops fetching, waits for the running jobs,
|
|
16
|
+
* resigns, and then closes the workers (ADR 0023).
|
|
17
|
+
*/
|
|
18
|
+
export class PoolWorkers {
|
|
19
|
+
#kind;
|
|
20
|
+
#clock;
|
|
21
|
+
#create;
|
|
22
|
+
#size;
|
|
23
|
+
#startup;
|
|
24
|
+
#eviction;
|
|
25
|
+
#store;
|
|
26
|
+
#election;
|
|
27
|
+
#disposed = new AbortController();
|
|
28
|
+
// Aborts with the first failure of the store: the host then stops owning.
|
|
29
|
+
#failed = new AbortController();
|
|
30
|
+
// Opens when the host stops offering its workers, with the failure that
|
|
31
|
+
// stopped it, if one did.
|
|
32
|
+
#ended = new Latch();
|
|
33
|
+
constructor(options) {
|
|
34
|
+
this.#kind = options.kind;
|
|
35
|
+
this.#clock = options.clock;
|
|
36
|
+
this.#create = options.create;
|
|
37
|
+
this.#size = options.size;
|
|
38
|
+
this.#startup = options.startup;
|
|
39
|
+
this.#eviction = options.eviction;
|
|
40
|
+
this.#store = new Store(options.store, options.clock);
|
|
41
|
+
const fail = (error) => this.#failed.abort(error);
|
|
42
|
+
this.#store.queue.on('error', fail);
|
|
43
|
+
this.#store.notifications.on('error', fail);
|
|
44
|
+
this.#election = new SqliteElection({
|
|
45
|
+
directory: this.#store.folder,
|
|
46
|
+
claimFile: `${options.kind.name}.claim`,
|
|
47
|
+
epochFile: `${options.kind.name}.epoch`,
|
|
48
|
+
clock: options.clock,
|
|
49
|
+
});
|
|
50
|
+
this.#live().then(() => this.#ended.open({ status: 'fulfilled', value: undefined }), (reason) => this.#ended.open({ status: 'rejected', reason }));
|
|
51
|
+
}
|
|
52
|
+
async #live() {
|
|
53
|
+
const stop = AbortSignal.any([this.#disposed.signal, this.#failed.signal]);
|
|
54
|
+
try {
|
|
55
|
+
await this.#store.start(this.#kind);
|
|
56
|
+
while (!stop.aborted) {
|
|
57
|
+
const term = await this.#election.campaign({
|
|
58
|
+
timeout: Infinity,
|
|
59
|
+
signal: stop,
|
|
60
|
+
});
|
|
61
|
+
// A campaign with no time limit resolves only with a term.
|
|
62
|
+
await this.#own(term, stop);
|
|
63
|
+
}
|
|
64
|
+
}
|
|
65
|
+
catch (error) {
|
|
66
|
+
if (error !== this.#disposed.signal.reason)
|
|
67
|
+
throw error;
|
|
68
|
+
}
|
|
69
|
+
this.#failed.signal.throwIfAborted();
|
|
70
|
+
}
|
|
71
|
+
// Owns until the stop, or until the term is lost: then it campaigns again.
|
|
72
|
+
async #own(term, stop) {
|
|
73
|
+
const owning = AbortSignal.any([stop, term.signal]);
|
|
74
|
+
const running = new Set();
|
|
75
|
+
let wake = Promise.withResolvers();
|
|
76
|
+
const slots = new Slots({
|
|
77
|
+
size: this.#size,
|
|
78
|
+
startup: this.#startup,
|
|
79
|
+
eviction: this.#eviction,
|
|
80
|
+
create: this.#create,
|
|
81
|
+
clock: this.#clock,
|
|
82
|
+
resized: () => wake.resolve(),
|
|
83
|
+
});
|
|
84
|
+
// Whether the last fetch took every job it asked for, so more may wait.
|
|
85
|
+
let backlog = false;
|
|
86
|
+
try {
|
|
87
|
+
while (!owning.aborted) {
|
|
88
|
+
const free = this.#size.current - running.size;
|
|
89
|
+
if (free > 0) {
|
|
90
|
+
const jobs = await this.#store.queue.fetch(this.#kind.queue, {
|
|
91
|
+
batchSize: free,
|
|
92
|
+
});
|
|
93
|
+
backlog = jobs.length === free;
|
|
94
|
+
for (const job of jobs) {
|
|
95
|
+
const run = this.#run(slots, job, () => backlog)
|
|
96
|
+
.catch((error) => this.#failed.abort(error))
|
|
97
|
+
.finally(() => {
|
|
98
|
+
running.delete(run);
|
|
99
|
+
wake.resolve();
|
|
100
|
+
});
|
|
101
|
+
running.add(run);
|
|
102
|
+
}
|
|
103
|
+
}
|
|
104
|
+
await this.#rest(wake.promise, owning);
|
|
105
|
+
wake = Promise.withResolvers();
|
|
106
|
+
}
|
|
107
|
+
}
|
|
108
|
+
finally {
|
|
109
|
+
await Promise.all(running);
|
|
110
|
+
// A standby takes over before the workers close: a close can be slow.
|
|
111
|
+
await term.resign();
|
|
112
|
+
await slots[Symbol.asyncDispose]();
|
|
113
|
+
}
|
|
114
|
+
}
|
|
115
|
+
// Waits for the backstop, a wake, or the end of the ownership.
|
|
116
|
+
async #rest(wake, owning) {
|
|
117
|
+
const woken = new AbortController();
|
|
118
|
+
void wake.then(() => woken.abort());
|
|
119
|
+
const signal = AbortSignal.any([owning, woken.signal]);
|
|
120
|
+
await delay(BACKSTOP, { clock: this.#clock, signal }).catch((error) => {
|
|
121
|
+
if (!signal.aborted)
|
|
122
|
+
throw error;
|
|
123
|
+
});
|
|
124
|
+
}
|
|
125
|
+
// Runs one fetched job on a worker, and settles it by its attempt. A
|
|
126
|
+
// worker that fails to start fails the job, as the worker's own failure.
|
|
127
|
+
// The kind's time limit and the touches start at the fetch, as the store
|
|
128
|
+
// counts both from it, so a warm start counts too. At the time limit the
|
|
129
|
+
// owner kills the worker and fails the job; when the store took the job
|
|
130
|
+
// back, it kills the worker and settles nothing.
|
|
131
|
+
async #run(slots, job, backlog) {
|
|
132
|
+
const attempt = { id: job.id, retryCount: job.retryCount };
|
|
133
|
+
const slot = slots.lend();
|
|
134
|
+
const ended = new AbortController();
|
|
135
|
+
try {
|
|
136
|
+
const outcome = await Promise.race([
|
|
137
|
+
this.#work(slots, slot, job.data, backlog, ended.signal),
|
|
138
|
+
this.#watch(attempt, ended.signal),
|
|
139
|
+
]);
|
|
140
|
+
ended.abort();
|
|
141
|
+
// A settle that affects no row finds the job under another claim: the
|
|
142
|
+
// store fenced it, and the output is dropped (ADR 0017).
|
|
143
|
+
if ('output' in outcome)
|
|
144
|
+
await this.#store.queue.complete(this.#kind.queue, attempt, {
|
|
145
|
+
output: outcome.output,
|
|
146
|
+
});
|
|
147
|
+
else if ('failure' in outcome)
|
|
148
|
+
await this.#store.queue.fail(this.#kind.queue, attempt, failure(outcome.failure));
|
|
149
|
+
else if ('expired' in outcome)
|
|
150
|
+
await this.#store.queue.fail(this.#kind.queue, attempt,
|
|
151
|
+
// The error of pg-boss's work at the time limit (manager.js:606).
|
|
152
|
+
failure(new Error(`handler execution exceeded ${this.#kind.expireInSeconds}s`)));
|
|
153
|
+
}
|
|
154
|
+
finally {
|
|
155
|
+
ended.abort();
|
|
156
|
+
slots.release(slot);
|
|
157
|
+
}
|
|
158
|
+
}
|
|
159
|
+
// Runs the input on the worker of the slot. The end of the run kills the
|
|
160
|
+
// worker of a run that has not ended, and leaves its outcome unread.
|
|
161
|
+
async #work(slots, slot, data, backlog, ended) {
|
|
162
|
+
let worker;
|
|
163
|
+
try {
|
|
164
|
+
worker = await slots.borrow(slot);
|
|
165
|
+
}
|
|
166
|
+
catch (error) {
|
|
167
|
+
return { failure: error };
|
|
168
|
+
}
|
|
169
|
+
if (ended.aborted)
|
|
170
|
+
return { gone: true };
|
|
171
|
+
const kill = () => slots.kill(slot, worker);
|
|
172
|
+
ended.addEventListener('abort', kill, { once: true });
|
|
173
|
+
const started = this.#clock.performance.now();
|
|
174
|
+
try {
|
|
175
|
+
if (!isEnvelope(data))
|
|
176
|
+
throw new TypeError('The job has no input: no Pool sent it.');
|
|
177
|
+
return { output: await worker.run(data.input) };
|
|
178
|
+
}
|
|
179
|
+
catch (error) {
|
|
180
|
+
return { failure: error };
|
|
181
|
+
}
|
|
182
|
+
finally {
|
|
183
|
+
ended.removeEventListener('abort', kill);
|
|
184
|
+
if (!ended.aborted)
|
|
185
|
+
slots.record(started, backlog());
|
|
186
|
+
}
|
|
187
|
+
}
|
|
188
|
+
// Touches the job every 5 s until the run ends, and resolves when the
|
|
189
|
+
// kind's time limit ends or a touch finds that the store took the job back.
|
|
190
|
+
async #watch(attempt, ended) {
|
|
191
|
+
const deadline = this.#clock.performance.now() + this.#kind.expireInSeconds * 1_000;
|
|
192
|
+
for (;;) {
|
|
193
|
+
const left = deadline - this.#clock.performance.now();
|
|
194
|
+
if (left <= 0)
|
|
195
|
+
return { expired: true };
|
|
196
|
+
await delay(Math.min(TOUCH_INTERVAL, left), {
|
|
197
|
+
clock: this.#clock,
|
|
198
|
+
signal: ended,
|
|
199
|
+
});
|
|
200
|
+
if (this.#clock.performance.now() >= deadline)
|
|
201
|
+
return { expired: true };
|
|
202
|
+
const { affected } = await this.#store.queue.touch(this.#kind.queue, attempt);
|
|
203
|
+
if (affected === 0)
|
|
204
|
+
return { gone: true };
|
|
205
|
+
}
|
|
206
|
+
}
|
|
207
|
+
async [Symbol.asyncDispose]() {
|
|
208
|
+
this.#disposed.abort();
|
|
209
|
+
const ended = await this.#ended.wait();
|
|
210
|
+
await this.#store[Symbol.asyncDispose]();
|
|
211
|
+
if (ended.status === 'rejected')
|
|
212
|
+
throw ended.reason;
|
|
213
|
+
}
|
|
214
|
+
}
|
|
215
|
+
// The pool does not check the input itself: a sender and an owner of one
|
|
216
|
+
// revision run the same code (ADR 0014).
|
|
217
|
+
function isEnvelope(data) {
|
|
218
|
+
return typeof data === 'object' && data !== null && 'input' in data;
|
|
219
|
+
}
|
|
220
|
+
/** A failure as data: its name and its message (ADR 0019). */
|
|
221
|
+
function failure(error) {
|
|
222
|
+
return Error.isError(error)
|
|
223
|
+
? { name: error.name, message: error.message }
|
|
224
|
+
: { name: 'Error', message: String(error) };
|
|
225
|
+
}
|
|
226
|
+
//# sourceMappingURL=pool-workers.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"pool-workers.js","sourceRoot":"","sources":["../src/pool-workers.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,KAAK,EAAkB,KAAK,EAAE,MAAM,gBAAgB,CAAC;AAC9D,OAAO,EAAE,cAAc,EAAa,MAAM,mBAAmB,CAAC;AAI9D,OAAO,EAAa,KAAK,EA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SAAS,CAAC,KAAK,EAAE,CAAC;QACvB,MAAM,KAAK,GAAG,MAAM,IAAI,CAAC,MAAM,CAAC,IAAI,EAAE,CAAC;QACvC,MAAM,IAAI,CAAC,MAAM,CAAC,MAAM,CAAC,YAAY,CAAC,EAAE,CAAC;QACzC,IAAI,KAAK,CAAC,MAAM,KAAK,UAAU;YAAE,MAAM,KAAK,CAAC,MAAM,CAAC;IACtD,CAAC;CACF;AAED,yEAAyE;AACzE,yCAAyC;AACzC,SAAS,UAAU,CAAQ,IAAa;IACtC,OAAO,OAAO,IAAI,KAAK,QAAQ,IAAI,IAAI,KAAK,IAAI,IAAI,OAAO,IAAI,IAAI,CAAC;AACtE,CAAC;AAED,8DAA8D;AAC9D,SAAS,OAAO,CAAC,KAAc;IAC7B,OAAO,KAAK,CAAC,OAAO,CAAC,KAAK,CAAC;QACzB,CAAC,CAAC,EAAE,IAAI,EAAE,KAAK,CAAC,IAAI,EAAE,OAAO,EAAE,KAAK,CAAC,OAAO,EAAE;QAC9C,CAAC,CAAC,EAAE,IAAI,EAAE,OAAO,EAAE,OAAO,EAAE,MAAM,CAAC,KAAK,CAAC,EAAE,CAAC;AAChD,CAAC"}
|
package/dist/pool.d.ts
CHANGED
|
@@ -4,7 +4,7 @@ import type { PoolStats } from './pool-stats.ts';
|
|
|
4
4
|
import type { Sizing } from './sizing.ts';
|
|
5
5
|
import type { Startup } from './startup.ts';
|
|
6
6
|
import type { Worker } from './worker.ts';
|
|
7
|
-
export declare class Pool<T extends Worker
|
|
7
|
+
export declare class Pool<T extends Worker<never, unknown>> implements AsyncDisposable {
|
|
8
8
|
#private;
|
|
9
9
|
constructor(options: {
|
|
10
10
|
size: Sizing;
|
package/dist/pool.d.ts.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"pool.d.ts","sourceRoot":"","sources":["../src/pool.ts"],"names":[],"mappings":"AAEA,OAAO,
|
|
1
|
+
{"version":3,"file":"pool.d.ts","sourceRoot":"","sources":["../src/pool.ts"],"names":[],"mappings":"AAEA,OAAO,EAAE,KAAK,KAAK,EAAgB,MAAM,gBAAgB,CAAC;AAE1D,OAAO,KAAK,EAAE,QAAQ,EAAE,MAAM,eAAe,CAAC;AAC9C,OAAO,KAAK,EAAE,SAAS,EAAE,MAAM,iBAAiB,CAAC;AACjD,OAAO,KAAK,EAAE,MAAM,EAAE,MAAM,aAAa,CAAC;AAE1C,OAAO,KAAK,EAAE,OAAO,EAAE,MAAM,cAAc,CAAC;AAC5C,OAAO,KAAK,EAAE,MAAM,EAAE,MAAM,aAAa,CAAC;AAS1C,qBAAa,IAAI,CAAC,CAAC,SAAS,MAAM,CAAC,KAAK,EAAE,OAAO,CAAC,CAAE,YAAW,eAAe;;gBAKhE,OAAO,EAAE;QACnB,IAAI,EAAE,MAAM,CAAC;QACb,OAAO,EAAE,OAAO,CAAC;QACjB,QAAQ,EAAE,QAAQ,CAAC;QACnB,MAAM,EAAE,MAAM,CAAC,CAAC;QAChB,oHAAoH;QACpH,KAAK,EAAE,KAAK,CAAC;KACd;IAaD,KAAK,IAAI,OAAO,CAAC,IAAI,CAAC;IAItB,IAAI,KAAK,IAAI,SAAS,CAErB;IAID,GAAG,CAAC,CAAC,EAAE,GAAG,EAAE,CAAC,MAAM,EAAE,CAAC,KAAK,OAAO,CAAC,CAAC,CAAC,EAAE,MAAM,CAAC,EAAE,WAAW,GAAG,OAAO,CAAC,CAAC,CAAC;IAiClE,CAAC,MAAM,CAAC,YAAY,CAAC,IAAI,OAAO,CAAC,IAAI,CAAC;CAI7C"}
|
package/dist/pool.js
CHANGED
|
@@ -1,17 +1,6 @@
|
|
|
1
1
|
import PQueue from 'p-queue';
|
|
2
|
-
import {
|
|
3
|
-
|
|
4
|
-
// exit, as Kubernetes restarts a container in CrashLoopBackOff (tuned by
|
|
5
|
-
// KEP-4603): 1 s, doubled for each early exit in a row, up to 60 s. A worker
|
|
6
|
-
// whose warm-up fails exits early too (ADR 0010).
|
|
7
|
-
const backoff = new Backoff({
|
|
8
|
-
initial: 1_000,
|
|
9
|
-
multiplier: 2,
|
|
10
|
-
max: 60_000,
|
|
11
|
-
jitter: { kind: 'none' },
|
|
12
|
-
});
|
|
13
|
-
// A worker that ran this long ends the back-off.
|
|
14
|
-
const backoffReset = 600_000;
|
|
2
|
+
import { untilAborted } from '@zukhruf/async';
|
|
3
|
+
import { Slots } from "./slots.js";
|
|
15
4
|
// Lends workers to jobs, one job per worker at a time. The sizing decides how
|
|
16
5
|
// many workers run, the startup when they start, and the eviction when an
|
|
17
6
|
// idle worker closes. A job takes an idle worker first. A worker that exits is
|
|
@@ -20,85 +9,43 @@ const backoffReset = 600_000;
|
|
|
20
9
|
// back-off when it exited early or its warm-up failed. The owner disposes the
|
|
21
10
|
// pool, which stops every worker.
|
|
22
11
|
export class Pool {
|
|
23
|
-
#create;
|
|
24
|
-
#startup;
|
|
25
|
-
#sizing;
|
|
26
|
-
#eviction;
|
|
27
12
|
#queue;
|
|
28
13
|
#slots;
|
|
29
|
-
#lent = new Set();
|
|
30
|
-
#workers = new Set();
|
|
31
|
-
#stopped = new AbortController();
|
|
32
|
-
#started = new Latch();
|
|
33
14
|
#clock;
|
|
34
|
-
// When the pool last changed how many workers it runs.
|
|
35
|
-
#resized;
|
|
36
15
|
constructor(options) {
|
|
37
16
|
this.#clock = options.clock;
|
|
38
|
-
this.#
|
|
39
|
-
this.#
|
|
40
|
-
|
|
41
|
-
|
|
42
|
-
|
|
43
|
-
|
|
44
|
-
|
|
45
|
-
index,
|
|
46
|
-
state: { kind: 'empty' },
|
|
47
|
-
early: 0,
|
|
48
|
-
}));
|
|
49
|
-
const outcomes = options.startup.eager
|
|
50
|
-
? this.#running().map((slot) => this.#spawn(slot))
|
|
51
|
-
: [];
|
|
52
|
-
Promise.all(outcomes.map((outcome) => settled(outcome))).then(() => this.#started.open({ status: 'fulfilled', value: undefined }), (reason) => this.#started.open({ status: 'rejected', reason }));
|
|
17
|
+
this.#queue = new PQueue({ concurrency: options.size.current });
|
|
18
|
+
this.#slots = new Slots({
|
|
19
|
+
...options,
|
|
20
|
+
resized: (current) => {
|
|
21
|
+
this.#queue.concurrency = current;
|
|
22
|
+
},
|
|
23
|
+
});
|
|
53
24
|
}
|
|
54
25
|
// Settles when the workers the startup starts with the pool are ready, and
|
|
55
26
|
// rejects when one of them fails to start.
|
|
56
|
-
|
|
57
|
-
|
|
58
|
-
if (outcome.status === 'rejected')
|
|
59
|
-
throw outcome.reason;
|
|
27
|
+
ready() {
|
|
28
|
+
return this.#slots.ready();
|
|
60
29
|
}
|
|
61
30
|
get stats() {
|
|
62
|
-
|
|
63
|
-
for (const slot of this.#slots) {
|
|
64
|
-
const lent = this.#lent.has(slot);
|
|
65
|
-
if (slot.state.kind === 'starting') {
|
|
66
|
-
stats.starting++;
|
|
67
|
-
if (lent)
|
|
68
|
-
stats.waiting++;
|
|
69
|
-
}
|
|
70
|
-
else if (slot.state.kind === 'ready') {
|
|
71
|
-
if (lent)
|
|
72
|
-
stats.lent++;
|
|
73
|
-
else
|
|
74
|
-
stats.idle++;
|
|
75
|
-
}
|
|
76
|
-
}
|
|
77
|
-
return stats;
|
|
31
|
+
return this.#slots.stats(this.#queue.size);
|
|
78
32
|
}
|
|
79
33
|
// A job aborted while it runs kills its worker. A job aborted while its
|
|
80
34
|
// worker warms up leaves the warm-up running for the jobs after it.
|
|
81
35
|
use(job, signal) {
|
|
82
36
|
const stop = signal === undefined
|
|
83
|
-
? this.#stopped
|
|
84
|
-
: AbortSignal.any([this.#stopped
|
|
37
|
+
? this.#slots.stopped
|
|
38
|
+
: AbortSignal.any([this.#slots.stopped, signal]);
|
|
85
39
|
if (stop.aborted)
|
|
86
40
|
return Promise.reject(stop.reason);
|
|
87
41
|
// The queue does not take the signal: on abort it would free the slot
|
|
88
42
|
// while the job still holds its worker.
|
|
89
43
|
return this.#queue.add(async () => {
|
|
90
44
|
stop.throwIfAborted();
|
|
91
|
-
const slot = this.#
|
|
92
|
-
this.#lent.add(slot);
|
|
93
|
-
// A job takes the worker before its eviction lets it go.
|
|
94
|
-
if (slot.state.kind === 'ready')
|
|
95
|
-
slot.state.idle.abort();
|
|
45
|
+
const slot = this.#slots.lend();
|
|
96
46
|
try {
|
|
97
|
-
const worker = await untilAborted(this.#borrow(slot), stop);
|
|
98
|
-
const kill = () =>
|
|
99
|
-
worker.kill();
|
|
100
|
-
this.#lose(slot, worker);
|
|
101
|
-
};
|
|
47
|
+
const worker = await untilAborted(this.#slots.borrow(slot), stop);
|
|
48
|
+
const kill = () => this.#slots.kill(slot, worker);
|
|
102
49
|
stop.addEventListener('abort', kill, { once: true });
|
|
103
50
|
const started = this.#clock.performance.now();
|
|
104
51
|
try {
|
|
@@ -113,253 +60,17 @@ export class Pool {
|
|
|
113
60
|
finally {
|
|
114
61
|
stop.removeEventListener('abort', kill);
|
|
115
62
|
if (!stop.aborted)
|
|
116
|
-
this.#record(started);
|
|
63
|
+
this.#slots.record(started, this.#queue.size > 0);
|
|
117
64
|
}
|
|
118
65
|
}
|
|
119
66
|
finally {
|
|
120
|
-
this.#
|
|
121
|
-
const state = slot.state;
|
|
122
|
-
if (slot.index >= this.#sizing.kept)
|
|
123
|
-
this.#retire(slot);
|
|
124
|
-
else if (state.kind === 'ready')
|
|
125
|
-
this.#rest(slot, state.worker, state.since);
|
|
126
|
-
else if (state.kind === 'lost')
|
|
127
|
-
this.#replace(slot, state.worker, state.since);
|
|
128
|
-
}
|
|
129
|
-
});
|
|
130
|
-
}
|
|
131
|
-
#running() {
|
|
132
|
-
return this.#slots.slice(0, this.#sizing.current);
|
|
133
|
-
}
|
|
134
|
-
// The slot a job takes, the lowest of its kind: an idle worker, else a
|
|
135
|
-
// worker that starts, else an empty slot or one whose worker ended, else a
|
|
136
|
-
// back-off. Jobs stay on the low slots, so the high ones stay idle until
|
|
137
|
-
// their eviction lets them go. This is the reason Commons Pool lends the
|
|
138
|
-
// object returned last (POOL-86) and Go's database/sql reuses the connection
|
|
139
|
-
// idle for the shortest time (sql.go:1336-1341).
|
|
140
|
-
#pick() {
|
|
141
|
-
const free = this.#running().filter((slot) => !this.#lent.has(slot));
|
|
142
|
-
return (free.find(({ state }) => state.kind === 'ready') ??
|
|
143
|
-
free.find(({ state }) => state.kind === 'starting') ??
|
|
144
|
-
free.find(({ state }) => state.kind === 'empty' || state.kind === 'lost') ??
|
|
145
|
-
free.find(({ state }) => state.kind === 'backoff'));
|
|
146
|
-
}
|
|
147
|
-
async #borrow(slot) {
|
|
148
|
-
const state = slot.state;
|
|
149
|
-
if (state.kind === 'ready')
|
|
150
|
-
return state.worker;
|
|
151
|
-
if (state.kind === 'lost') {
|
|
152
|
-
this.#enter(slot, { kind: 'empty' });
|
|
153
|
-
void this.#close(state.worker);
|
|
154
|
-
}
|
|
155
|
-
// A job does not wait out a back-off: its worker starts now, and the start
|
|
156
|
-
// ends the wait of the back-off.
|
|
157
|
-
const worker = await settled(state.kind === 'starting' ? state.outcome : this.#spawn(slot));
|
|
158
|
-
const now = slot.state;
|
|
159
|
-
if (now.kind === 'ready' && now.worker === worker)
|
|
160
|
-
return worker;
|
|
161
|
-
// The worker exited between its start and this borrow.
|
|
162
|
-
return this.#borrow(slot);
|
|
163
|
-
}
|
|
164
|
-
// A job measures how many workers pay only if, since it started, the pool
|
|
165
|
-
// ran the same number of workers and each was ready, and another job waited
|
|
166
|
-
// when it finished.
|
|
167
|
-
#record(started) {
|
|
168
|
-
const steady = this.#steady();
|
|
169
|
-
this.#sizing.record({
|
|
170
|
-
started,
|
|
171
|
-
finished: this.#clock.performance.now(),
|
|
172
|
-
measurable: steady !== undefined && started >= steady && this.#queue.size > 0,
|
|
173
|
-
});
|
|
174
|
-
for (const slot of this.#slots.slice(this.#sizing.kept))
|
|
175
|
-
this.#retire(slot);
|
|
176
|
-
if (this.#sizing.current === this.#queue.concurrency)
|
|
177
|
-
return;
|
|
178
|
-
this.#queue.concurrency = this.#sizing.current;
|
|
179
|
-
this.#resized = this.#clock.performance.now();
|
|
180
|
-
// An idle worker waits for its eviction only while the pool runs its slot.
|
|
181
|
-
for (const slot of this.#slots.slice(0, this.#sizing.kept)) {
|
|
182
|
-
const state = slot.state;
|
|
183
|
-
if (state.kind === 'ready' &&
|
|
184
|
-
!this.#lent.has(slot) &&
|
|
185
|
-
state.idle.signal.aborted === slot.index < this.#sizing.current)
|
|
186
|
-
this.#rest(slot, state.worker, state.since);
|
|
187
|
-
}
|
|
188
|
-
for (const slot of this.#running())
|
|
189
|
-
if (slot.state.kind === 'empty')
|
|
190
|
-
this.#spawn(slot);
|
|
191
|
-
}
|
|
192
|
-
// Since when every slot the pool runs has had a ready worker, if they have.
|
|
193
|
-
#steady() {
|
|
194
|
-
let since = this.#resized;
|
|
195
|
-
for (const { state } of this.#running()) {
|
|
196
|
-
if (state.kind !== 'ready')
|
|
197
|
-
return undefined;
|
|
198
|
-
since = Math.max(since, state.since);
|
|
199
|
-
}
|
|
200
|
-
return since;
|
|
201
|
-
}
|
|
202
|
-
#spawn(slot) {
|
|
203
|
-
const outcome = new Latch();
|
|
204
|
-
this.#enter(slot, { kind: 'starting', outcome });
|
|
205
|
-
const turn = Promise.withResolvers();
|
|
206
|
-
this.#startup
|
|
207
|
-
.turn(turn.promise)
|
|
208
|
-
.then(async () => {
|
|
209
|
-
this.#stopped.signal.throwIfAborted();
|
|
210
|
-
const worker = this.#create();
|
|
211
|
-
this.#workers.add(worker);
|
|
212
|
-
try {
|
|
213
|
-
await worker.ready?.();
|
|
214
|
-
return worker;
|
|
67
|
+
this.#slots.release(slot);
|
|
215
68
|
}
|
|
216
|
-
catch (error) {
|
|
217
|
-
worker.kill();
|
|
218
|
-
await this.#close(worker);
|
|
219
|
-
throw error;
|
|
220
|
-
}
|
|
221
|
-
})
|
|
222
|
-
.then((worker) => {
|
|
223
|
-
if (slot.state.kind === 'starting' && slot.state.outcome === outcome)
|
|
224
|
-
this.#rest(slot, worker, this.#clock.performance.now());
|
|
225
|
-
outcome.open({ status: 'fulfilled', value: worker });
|
|
226
|
-
turn.resolve();
|
|
227
|
-
void worker.exited().then(() => this.#ended(slot, worker));
|
|
228
|
-
}, (reason) => {
|
|
229
|
-
if (slot.state.kind === 'starting' &&
|
|
230
|
-
slot.state.outcome === outcome) {
|
|
231
|
-
this.#enter(slot, { kind: 'empty' });
|
|
232
|
-
// A failed warm-up is an early exit, whoever started the worker,
|
|
233
|
-
// as HikariCP tries a failed create again (ADR 0010).
|
|
234
|
-
if (this.#startup.eager && !this.#stopped.signal.aborted) {
|
|
235
|
-
slot.early++;
|
|
236
|
-
this.#restart(slot);
|
|
237
|
-
}
|
|
238
|
-
}
|
|
239
|
-
outcome.open({ status: 'rejected', reason });
|
|
240
|
-
turn.resolve();
|
|
241
69
|
});
|
|
242
|
-
return outcome;
|
|
243
|
-
}
|
|
244
|
-
// Every change of a slot's state comes here, so a ready worker that leaves
|
|
245
|
-
// its slot ends its idle wait, and a slot that leaves its back-off ends the
|
|
246
|
-
// wait of the back-off.
|
|
247
|
-
#enter(slot, next) {
|
|
248
|
-
const state = slot.state;
|
|
249
|
-
if (state.kind === 'ready')
|
|
250
|
-
state.idle.abort();
|
|
251
|
-
else if (state.kind === 'backoff')
|
|
252
|
-
state.wait.abort();
|
|
253
|
-
slot.state = next;
|
|
254
|
-
}
|
|
255
|
-
// A worker becomes idle in a slot. Its eviction may let it go only while
|
|
256
|
-
// no job has it and the pool runs its slot: a slot the sizing keeps but does
|
|
257
|
-
// not run has the worker the sizing tries (ADR 0002).
|
|
258
|
-
#rest(slot, worker, since) {
|
|
259
|
-
const state = {
|
|
260
|
-
kind: 'ready',
|
|
261
|
-
worker,
|
|
262
|
-
since,
|
|
263
|
-
idle: new AbortController(),
|
|
264
|
-
};
|
|
265
|
-
this.#enter(slot, state);
|
|
266
|
-
if (this.#lent.has(slot) || slot.index >= this.#sizing.current) {
|
|
267
|
-
state.idle.abort();
|
|
268
|
-
return;
|
|
269
|
-
}
|
|
270
|
-
this.#eviction.idle(AbortSignal.any([state.idle.signal, this.#stopped.signal]), () => this.#evict(slot, state), this.#clock);
|
|
271
|
-
}
|
|
272
|
-
// Closes an idle worker that its eviction let go. The slot is empty before
|
|
273
|
-
// the worker closes, so its exit starts no back-off and no replacement. A
|
|
274
|
-
// worker that ran 10 minutes ends the back-off, as at an exit.
|
|
275
|
-
#evict(slot, state) {
|
|
276
|
-
if (slot.state !== state ||
|
|
277
|
-
this.#lent.has(slot) ||
|
|
278
|
-
slot.index >= this.#sizing.current ||
|
|
279
|
-
this.#stopped.signal.aborted)
|
|
280
|
-
return;
|
|
281
|
-
if (this.#clock.performance.now() - state.since >= backoffReset)
|
|
282
|
-
slot.early = 0;
|
|
283
|
-
this.#retire(slot);
|
|
284
|
-
}
|
|
285
|
-
// The pool killed the worker of a slot: no job may borrow it any more.
|
|
286
|
-
#lose(slot, worker) {
|
|
287
|
-
const state = slot.state;
|
|
288
|
-
if (state.kind === 'ready' && state.worker === worker)
|
|
289
|
-
this.#enter(slot, { kind: 'lost', worker, since: state.since });
|
|
290
|
-
}
|
|
291
|
-
#ended(slot, worker) {
|
|
292
|
-
const state = slot.state;
|
|
293
|
-
if ((state.kind !== 'ready' && state.kind !== 'lost') ||
|
|
294
|
-
state.worker !== worker)
|
|
295
|
-
return;
|
|
296
|
-
this.#enter(slot, { kind: 'lost', worker, since: state.since });
|
|
297
|
-
if (!this.#lent.has(slot))
|
|
298
|
-
this.#replace(slot, worker, state.since);
|
|
299
|
-
}
|
|
300
|
-
// An eager pool closes the worker of a lost slot that no job has, and
|
|
301
|
-
// starts the next one.
|
|
302
|
-
#replace(slot, worker, since) {
|
|
303
|
-
if (this.#stopped.signal.aborted || !this.#startup.eager)
|
|
304
|
-
return;
|
|
305
|
-
this.#enter(slot, { kind: 'empty' });
|
|
306
|
-
void this.#close(worker);
|
|
307
|
-
slot.early =
|
|
308
|
-
this.#clock.performance.now() - since < backoffReset ? slot.early + 1 : 0;
|
|
309
|
-
this.#restart(slot);
|
|
310
|
-
}
|
|
311
|
-
// Starts the next worker of an empty slot that the pool runs: at once when
|
|
312
|
-
// no early exit came before, else after the back-off. A job that takes the
|
|
313
|
-
// slot, the sizing that stops keeping it, or the disposal ends the wait.
|
|
314
|
-
#restart(slot) {
|
|
315
|
-
if (slot.index >= this.#sizing.current)
|
|
316
|
-
return;
|
|
317
|
-
if (slot.early === 0) {
|
|
318
|
-
this.#spawn(slot);
|
|
319
|
-
return;
|
|
320
|
-
}
|
|
321
|
-
const state = { kind: 'backoff', wait: new AbortController() };
|
|
322
|
-
this.#enter(slot, state);
|
|
323
|
-
delay(backoff.duration(slot.early - 1), {
|
|
324
|
-
clock: this.#clock,
|
|
325
|
-
signal: AbortSignal.any([state.wait.signal, this.#stopped.signal]),
|
|
326
|
-
}).then(() => {
|
|
327
|
-
if (slot.state !== state)
|
|
328
|
-
return;
|
|
329
|
-
this.#enter(slot, { kind: 'empty' });
|
|
330
|
-
if (slot.index < this.#sizing.current)
|
|
331
|
-
this.#spawn(slot);
|
|
332
|
-
}, () => undefined);
|
|
333
|
-
}
|
|
334
|
-
// Closes the worker of a slot the pool no longer keeps, once its job ends.
|
|
335
|
-
#retire(slot) {
|
|
336
|
-
if (this.#lent.has(slot))
|
|
337
|
-
return;
|
|
338
|
-
const state = slot.state;
|
|
339
|
-
this.#enter(slot, { kind: 'empty' });
|
|
340
|
-
if (state.kind === 'ready' || state.kind === 'lost')
|
|
341
|
-
void this.#close(state.worker);
|
|
342
|
-
else if (state.kind === 'starting')
|
|
343
|
-
void settled(state.outcome).then((worker) => this.#close(worker), () => undefined);
|
|
344
|
-
}
|
|
345
|
-
async #close(worker) {
|
|
346
|
-
await worker.close();
|
|
347
|
-
this.#workers.delete(worker);
|
|
348
70
|
}
|
|
349
71
|
async [Symbol.asyncDispose]() {
|
|
350
|
-
|
|
351
|
-
this.#stopped.abort();
|
|
352
|
-
for (const worker of this.#workers)
|
|
353
|
-
worker.kill();
|
|
354
|
-
await Promise.all([...this.#workers].map((worker) => worker.close()));
|
|
72
|
+
await this.#slots[Symbol.asyncDispose]();
|
|
355
73
|
await this.#queue.onIdle();
|
|
356
74
|
}
|
|
357
75
|
}
|
|
358
|
-
// The worker a creation gave, or its failure.
|
|
359
|
-
async function settled(outcome) {
|
|
360
|
-
const result = await outcome.wait();
|
|
361
|
-
if (result.status === 'rejected')
|
|
362
|
-
throw result.reason;
|
|
363
|
-
return result.value;
|
|
364
|
-
}
|
|
365
76
|
//# sourceMappingURL=pool.js.map
|