@orkestrel/pool 0.0.4 → 0.0.6
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 +2 -2
- package/dist/src/core/index.cjs +544 -181
- package/dist/src/core/index.cjs.map +1 -1
- package/dist/src/core/index.d.cts +169 -109
- package/dist/src/core/index.d.ts +169 -109
- package/dist/src/core/index.js +541 -182
- package/dist/src/core/index.js.map +1 -1
- package/package.json +6 -6
package/dist/src/core/index.js
CHANGED
|
@@ -1,264 +1,623 @@
|
|
|
1
1
|
import { Emitter } from "@orkestrel/emitter";
|
|
2
|
+
//#region src/core/errors.ts
|
|
3
|
+
/**
|
|
4
|
+
* A stable, machine-readable pool failure with the original cause and structured context.
|
|
5
|
+
*
|
|
6
|
+
* @example
|
|
7
|
+
* ```ts
|
|
8
|
+
* import { PoolError, isPoolError } from '@orkestrel/pool'
|
|
9
|
+
*
|
|
10
|
+
* try {
|
|
11
|
+
* await pool.acquire()
|
|
12
|
+
* } catch (error: unknown) {
|
|
13
|
+
* if (isPoolError(error)) console.error(error.code, error.cause)
|
|
14
|
+
* }
|
|
15
|
+
* ```
|
|
16
|
+
*/
|
|
17
|
+
var PoolError = class extends Error {
|
|
18
|
+
/** Stable machine-readable failure category. */
|
|
19
|
+
code;
|
|
20
|
+
/** Optional structured input or aggregate-cleanup details. */
|
|
21
|
+
context;
|
|
22
|
+
/**
|
|
23
|
+
* Create a pool failure without coercing a hostile thrown value.
|
|
24
|
+
*
|
|
25
|
+
* @param options - Stable code plus optional cause and structured context
|
|
26
|
+
*/
|
|
27
|
+
constructor(options) {
|
|
28
|
+
let message = "pool input is invalid";
|
|
29
|
+
if (options.code === "destroyed") message = "pool is destroyed";
|
|
30
|
+
if (options.code === "create") message = "pool create failed";
|
|
31
|
+
if (options.code === "cleanup") message = "pool cleanup failed";
|
|
32
|
+
try {
|
|
33
|
+
if (options.cause instanceof Error && typeof options.cause.message === "string" && options.cause.message.length > 0) message = `${message}: ${options.cause.message}`;
|
|
34
|
+
} catch {}
|
|
35
|
+
super(message, options.cause === void 0 ? void 0 : { cause: options.cause });
|
|
36
|
+
this.name = "PoolError";
|
|
37
|
+
this.code = options.code;
|
|
38
|
+
this.context = options.context;
|
|
39
|
+
}
|
|
40
|
+
};
|
|
41
|
+
/**
|
|
42
|
+
* Test whether an unknown value is a {@link PoolError}, returning `false` for hostile proxies.
|
|
43
|
+
*
|
|
44
|
+
* @param value - The unknown boundary value
|
|
45
|
+
* @returns Whether the value is a real `PoolError` instance
|
|
46
|
+
*
|
|
47
|
+
* @example
|
|
48
|
+
* ```ts
|
|
49
|
+
* isPoolError(new PoolError({ code: 'destroyed' })) // true
|
|
50
|
+
* isPoolError(new Error('other')) // false
|
|
51
|
+
* ```
|
|
52
|
+
*/
|
|
53
|
+
function isPoolError(value) {
|
|
54
|
+
try {
|
|
55
|
+
return value instanceof PoolError;
|
|
56
|
+
} catch {
|
|
57
|
+
return false;
|
|
58
|
+
}
|
|
59
|
+
}
|
|
60
|
+
//#endregion
|
|
61
|
+
//#region src/core/validators.ts
|
|
62
|
+
/**
|
|
63
|
+
* Test whether a value is a valid finite pool maximum.
|
|
64
|
+
*
|
|
65
|
+
* @param value - The unknown maximum candidate
|
|
66
|
+
* @returns Whether the value is a positive safe integer
|
|
67
|
+
*
|
|
68
|
+
* @example
|
|
69
|
+
* ```ts
|
|
70
|
+
* isPoolMax(8) // true
|
|
71
|
+
* isPoolMax(Infinity) // false
|
|
72
|
+
* ```
|
|
73
|
+
*/
|
|
74
|
+
function isPoolMax(value) {
|
|
75
|
+
return typeof value === "number" && Number.isSafeInteger(value) && value > 0;
|
|
76
|
+
}
|
|
77
|
+
/**
|
|
78
|
+
* Test whether a value is a native `AbortSignal`, returning `false` for hostile proxies.
|
|
79
|
+
*
|
|
80
|
+
* @param value - The unknown signal candidate
|
|
81
|
+
* @returns Whether the value is a native `AbortSignal`
|
|
82
|
+
*
|
|
83
|
+
* @example
|
|
84
|
+
* ```ts
|
|
85
|
+
* isPoolSignal(new AbortController().signal) // true
|
|
86
|
+
* isPoolSignal({ aborted: false }) // false
|
|
87
|
+
* ```
|
|
88
|
+
*/
|
|
89
|
+
function isPoolSignal(value) {
|
|
90
|
+
try {
|
|
91
|
+
const getter = Object.getOwnPropertyDescriptor(AbortSignal.prototype, "aborted")?.get;
|
|
92
|
+
if (getter === void 0) return false;
|
|
93
|
+
Reflect.apply(getter, value, []);
|
|
94
|
+
return true;
|
|
95
|
+
} catch {
|
|
96
|
+
return false;
|
|
97
|
+
}
|
|
98
|
+
}
|
|
99
|
+
//#endregion
|
|
2
100
|
//#region src/core/Pool.ts
|
|
3
101
|
/**
|
|
4
|
-
* A
|
|
102
|
+
* A capacity-aware resource pool whose opaque ownership records preserve FIFO settlement,
|
|
103
|
+
* cancellation, exact lease release, and deterministic teardown under concurrent hooks.
|
|
5
104
|
*
|
|
6
|
-
* @
|
|
7
|
-
*
|
|
8
|
-
*
|
|
9
|
-
*
|
|
10
|
-
*
|
|
11
|
-
*
|
|
12
|
-
*
|
|
13
|
-
*
|
|
14
|
-
*
|
|
15
|
-
*
|
|
16
|
-
*
|
|
17
|
-
*
|
|
18
|
-
*
|
|
19
|
-
*
|
|
20
|
-
*
|
|
21
|
-
* returning `false` — the resource is "not usable", so it is destroyed and replaced —
|
|
22
|
-
* rather than escaping. This holds on both the idle reuse and the FIFO handoff paths, so
|
|
23
|
-
* a throwing validator can never strand a parked waiter on an unhandled rejection.
|
|
24
|
-
* - **Abort-cancellable waiting.** A parked `acquire` given an `AbortSignal` rejects
|
|
25
|
-
* when that signal fires and removes its waiter from the queue — no leaked waiter, so
|
|
26
|
-
* a later `release` still serves the next live waiter. The signal is supplied by the
|
|
27
|
-
* caller (a worker, for example, passes its per-attempt execution signal); the pool adds no abort
|
|
28
|
-
* of its own.
|
|
29
|
-
* - **Counts.** `size` = idle + leased; `idle` = available now; `active` = leased out.
|
|
30
|
-
* - **Teardown.** `clear` destroys every IDLE resource (leased ones keep running);
|
|
31
|
-
* `destroy` destroys ALL resources and rejects any parked waiters. Both await the
|
|
32
|
-
* `destroy` hook.
|
|
33
|
-
* - **Observable (§13).** The owned {@link emitter} ({@link PoolEventMap}) carries the
|
|
34
|
-
* resource lifecycle — `create` / `acquire` / `release` / `destroy` — for fire-and-forget
|
|
35
|
-
* observers. Every event is emitted directly, strictly AFTER the relevant transition —
|
|
36
|
-
* OUTSIDE the `#handoff` / `#serve` await-chain, never across a waiter's resolve; the
|
|
37
|
-
* emitter isolates a listener throw and routes it to its `error` handler (the `error`
|
|
38
|
-
* option), so a buggy observer can NEVER corrupt the validated FIFO handoff-eviction
|
|
39
|
-
* machinery (it cannot strand a parked waiter or unbalance the lease count). Observation is
|
|
40
|
-
* purely a side-channel.
|
|
41
|
-
* - **De-bloated.** No warm-floor / `min`, no eviction timers — lean.
|
|
105
|
+
* @typeParam T - The pooled resource value
|
|
106
|
+
*
|
|
107
|
+
* @example
|
|
108
|
+
* ```ts
|
|
109
|
+
* import { Pool } from '@orkestrel/pool'
|
|
110
|
+
*
|
|
111
|
+
* const pool = new Pool({ create: () => new Uint8Array(64), max: 2 })
|
|
112
|
+
* const token = await pool.acquire()
|
|
113
|
+
* try {
|
|
114
|
+
* consume(token.value)
|
|
115
|
+
* } finally {
|
|
116
|
+
* token.release()
|
|
117
|
+
* }
|
|
118
|
+
* await pool.destroy()
|
|
119
|
+
* ```
|
|
42
120
|
*/
|
|
43
121
|
var Pool = class {
|
|
44
122
|
#create;
|
|
45
|
-
#
|
|
123
|
+
#cleanup;
|
|
46
124
|
#validate;
|
|
47
125
|
#max;
|
|
48
126
|
#emitter;
|
|
49
|
-
#
|
|
127
|
+
#resources = /* @__PURE__ */ new Map();
|
|
128
|
+
#available = [];
|
|
129
|
+
#validating = /* @__PURE__ */ new Set();
|
|
130
|
+
#leased = /* @__PURE__ */ new Set();
|
|
131
|
+
#destroying = /* @__PURE__ */ new Map();
|
|
50
132
|
#waiters = [];
|
|
51
|
-
#
|
|
52
|
-
#
|
|
133
|
+
#assigned = /* @__PURE__ */ new Set();
|
|
134
|
+
#reservations = /* @__PURE__ */ new Set();
|
|
135
|
+
#signals = /* @__PURE__ */ new Map();
|
|
136
|
+
#ready = /* @__PURE__ */ new Map();
|
|
137
|
+
#operations = /* @__PURE__ */ new Set();
|
|
138
|
+
#owned = /* @__PURE__ */ new Set();
|
|
139
|
+
#failures = [];
|
|
140
|
+
#ending;
|
|
141
|
+
#pumping = false;
|
|
142
|
+
#repump = false;
|
|
143
|
+
/**
|
|
144
|
+
* Construct a pool and synchronously validate its capacity contract.
|
|
145
|
+
*
|
|
146
|
+
* @param options - Resource hooks, observation hooks, and optional positive safe `max`
|
|
147
|
+
*/
|
|
53
148
|
constructor(options) {
|
|
149
|
+
const max = options.max;
|
|
150
|
+
if (max !== void 0 && !isPoolMax(max)) throw new PoolError({
|
|
151
|
+
code: "invalid",
|
|
152
|
+
context: { value: max }
|
|
153
|
+
});
|
|
154
|
+
const on = options.on;
|
|
155
|
+
const error = options.error;
|
|
54
156
|
this.#create = options.create;
|
|
55
|
-
this.#
|
|
157
|
+
this.#cleanup = options.destroy;
|
|
56
158
|
this.#validate = options.validate;
|
|
57
|
-
this.#max =
|
|
159
|
+
this.#max = max;
|
|
58
160
|
this.#emitter = new Emitter({
|
|
59
|
-
...
|
|
60
|
-
...
|
|
161
|
+
...on === void 0 ? {} : { on },
|
|
162
|
+
...error === void 0 ? {} : { error }
|
|
61
163
|
});
|
|
62
164
|
}
|
|
165
|
+
/** The typed synchronous lifecycle observation surface. */
|
|
63
166
|
get emitter() {
|
|
64
167
|
return this.#emitter;
|
|
65
168
|
}
|
|
169
|
+
/** All owned records, including records validating or destroying. */
|
|
66
170
|
get size() {
|
|
67
|
-
return this.#
|
|
171
|
+
return this.#resources.size;
|
|
68
172
|
}
|
|
173
|
+
/** Records immediately available without validation work. */
|
|
69
174
|
get idle() {
|
|
70
|
-
return this.#
|
|
175
|
+
return this.#available.length;
|
|
71
176
|
}
|
|
177
|
+
/** Records represented by unsettled released-once lease tokens. */
|
|
72
178
|
get active() {
|
|
73
|
-
return this.#
|
|
179
|
+
return this.#leased.size;
|
|
74
180
|
}
|
|
75
|
-
|
|
76
|
-
|
|
77
|
-
|
|
78
|
-
|
|
79
|
-
|
|
80
|
-
|
|
81
|
-
|
|
181
|
+
/**
|
|
182
|
+
* Queue and lease one resource in FIFO settlement order.
|
|
183
|
+
*
|
|
184
|
+
* @param signal - Optional native cancellation signal
|
|
185
|
+
* @returns A promise for the unique resource lease
|
|
186
|
+
*/
|
|
187
|
+
acquire(signal) {
|
|
188
|
+
if (signal !== void 0 && !isPoolSignal(signal)) throw new PoolError({
|
|
189
|
+
code: "invalid",
|
|
190
|
+
context: { value: signal }
|
|
191
|
+
});
|
|
192
|
+
if (this.#ending !== void 0) return Promise.reject(new PoolError({ code: "destroyed" }));
|
|
193
|
+
if (signal !== void 0) {
|
|
194
|
+
const state = this.#state(signal);
|
|
195
|
+
if (state[0]) return Promise.reject(state[1]);
|
|
82
196
|
}
|
|
83
|
-
|
|
84
|
-
|
|
85
|
-
|
|
86
|
-
|
|
197
|
+
const waiter = Promise.withResolvers();
|
|
198
|
+
this.#waiters.push(waiter);
|
|
199
|
+
if (signal !== void 0) {
|
|
200
|
+
const listener = this.#createAbort(waiter, signal);
|
|
201
|
+
AbortSignal.prototype.addEventListener.call(signal, "abort", listener, { once: true });
|
|
202
|
+
this.#signals.set(waiter, {
|
|
203
|
+
signal,
|
|
204
|
+
listener
|
|
205
|
+
});
|
|
206
|
+
const state = this.#state(signal);
|
|
207
|
+
if (state[0]) this.#abort(waiter, state[1]);
|
|
87
208
|
}
|
|
88
|
-
|
|
209
|
+
this.#pump();
|
|
210
|
+
return waiter.promise;
|
|
89
211
|
}
|
|
90
|
-
|
|
91
|
-
|
|
92
|
-
|
|
212
|
+
/**
|
|
213
|
+
* Destroy the records that are idle at this call's synchronous snapshot.
|
|
214
|
+
*
|
|
215
|
+
* @returns A promise that settles after every snapshot cleanup attempt
|
|
216
|
+
*/
|
|
217
|
+
clear() {
|
|
218
|
+
if (this.#ending !== void 0) return Promise.reject(new PoolError({ code: "destroyed" }));
|
|
219
|
+
const records = this.#available.splice(0);
|
|
220
|
+
const cleanups = [];
|
|
221
|
+
for (const record of records) {
|
|
222
|
+
const cleanup = this.#dispose(record);
|
|
223
|
+
cleanups.push(cleanup);
|
|
224
|
+
cleanup.then(() => this.#pump(), () => this.#pump());
|
|
225
|
+
}
|
|
226
|
+
return this.#settleClear(cleanups);
|
|
93
227
|
}
|
|
94
|
-
|
|
95
|
-
|
|
96
|
-
|
|
228
|
+
/**
|
|
229
|
+
* Permanently tear down the pool and return its stable completion barrier.
|
|
230
|
+
*
|
|
231
|
+
* @returns The exact promise shared by every destroy call
|
|
232
|
+
*/
|
|
233
|
+
destroy() {
|
|
234
|
+
if (this.#ending !== void 0) return this.#ending.promise;
|
|
235
|
+
const ending = Promise.withResolvers();
|
|
236
|
+
this.#ending = ending;
|
|
97
237
|
const waiters = this.#waiters.splice(0);
|
|
98
|
-
const error = /* @__PURE__ */ new Error("pool is destroyed");
|
|
99
238
|
for (const waiter of waiters) {
|
|
100
|
-
waiter
|
|
101
|
-
waiter.reject(
|
|
239
|
+
this.#detach(waiter);
|
|
240
|
+
waiter.reject(new PoolError({ code: "destroyed" }));
|
|
102
241
|
}
|
|
103
|
-
|
|
104
|
-
|
|
105
|
-
|
|
106
|
-
|
|
107
|
-
|
|
108
|
-
|
|
109
|
-
|
|
110
|
-
|
|
111
|
-
|
|
112
|
-
|
|
242
|
+
this.#ready.clear();
|
|
243
|
+
this.#assigned.clear();
|
|
244
|
+
this.#available.splice(0);
|
|
245
|
+
for (const cleanup of this.#destroying.values()) this.#own(cleanup);
|
|
246
|
+
for (const record of this.#resources.keys()) if (!this.#validating.has(record)) this.#own(this.#dispose(record));
|
|
247
|
+
this.#finish();
|
|
248
|
+
return ending.promise;
|
|
249
|
+
}
|
|
250
|
+
#createAbort(waiter, signal) {
|
|
251
|
+
return () => {
|
|
252
|
+
let reason;
|
|
253
|
+
try {
|
|
254
|
+
reason = this.#state(signal)[1];
|
|
255
|
+
} catch (error) {
|
|
256
|
+
reason = error;
|
|
113
257
|
}
|
|
114
|
-
|
|
115
|
-
}
|
|
258
|
+
this.#abort(waiter, reason);
|
|
259
|
+
};
|
|
116
260
|
}
|
|
117
|
-
|
|
118
|
-
|
|
119
|
-
|
|
261
|
+
#state(signal) {
|
|
262
|
+
const aborted = Object.getOwnPropertyDescriptor(AbortSignal.prototype, "aborted")?.get;
|
|
263
|
+
const reason = Object.getOwnPropertyDescriptor(AbortSignal.prototype, "reason")?.get;
|
|
264
|
+
if (aborted === void 0 || reason === void 0) throw new PoolError({
|
|
265
|
+
code: "invalid",
|
|
266
|
+
context: { value: signal }
|
|
267
|
+
});
|
|
120
268
|
try {
|
|
121
|
-
|
|
269
|
+
const stopped = Reflect.apply(aborted, signal, []) === true;
|
|
270
|
+
return [stopped, stopped ? Reflect.apply(reason, signal, []) : void 0];
|
|
122
271
|
} catch (error) {
|
|
123
|
-
|
|
124
|
-
|
|
125
|
-
|
|
126
|
-
|
|
127
|
-
|
|
128
|
-
await this.#release(resource);
|
|
129
|
-
throw new Error("pool is destroyed");
|
|
272
|
+
throw new PoolError({
|
|
273
|
+
code: "invalid",
|
|
274
|
+
cause: error,
|
|
275
|
+
context: { value: signal }
|
|
276
|
+
});
|
|
130
277
|
}
|
|
131
|
-
this.#emitter.emit("create");
|
|
132
|
-
return this.#token(resource);
|
|
133
|
-
}
|
|
134
|
-
#wait(signal) {
|
|
135
|
-
return new Promise((resolve, reject) => {
|
|
136
|
-
const waiter = {
|
|
137
|
-
resolve,
|
|
138
|
-
reject,
|
|
139
|
-
clear: this.#ignore
|
|
140
|
-
};
|
|
141
|
-
this.#waiters.push(waiter);
|
|
142
|
-
if (signal !== void 0) {
|
|
143
|
-
const onAbort = this.#createAbort(signal, waiter);
|
|
144
|
-
signal.addEventListener("abort", onAbort, { once: true });
|
|
145
|
-
waiter.clear = this.#createClear(signal, onAbort);
|
|
146
|
-
}
|
|
147
|
-
});
|
|
148
278
|
}
|
|
149
|
-
#
|
|
150
|
-
return {
|
|
151
|
-
value: resource,
|
|
152
|
-
release: this.#createRelease(resource)
|
|
153
|
-
};
|
|
154
|
-
}
|
|
155
|
-
#ignore() {}
|
|
156
|
-
#createAbort(signal, waiter) {
|
|
157
|
-
return () => {
|
|
158
|
-
const index = this.#waiters.indexOf(waiter);
|
|
159
|
-
if (index >= 0) this.#waiters.splice(index, 1);
|
|
160
|
-
waiter.reject(signal.reason);
|
|
161
|
-
};
|
|
162
|
-
}
|
|
163
|
-
#createClear(signal, onAbort) {
|
|
164
|
-
return () => signal.removeEventListener("abort", onAbort);
|
|
165
|
-
}
|
|
166
|
-
#createRelease(resource) {
|
|
279
|
+
#createRelease(record) {
|
|
167
280
|
let released = false;
|
|
168
281
|
return () => {
|
|
169
282
|
if (released) return;
|
|
170
283
|
released = true;
|
|
171
|
-
this.#
|
|
284
|
+
this.#release(record);
|
|
172
285
|
};
|
|
173
286
|
}
|
|
174
|
-
#
|
|
175
|
-
|
|
176
|
-
|
|
177
|
-
|
|
178
|
-
|
|
179
|
-
|
|
180
|
-
|
|
181
|
-
|
|
182
|
-
|
|
287
|
+
#token(record, value) {
|
|
288
|
+
return {
|
|
289
|
+
value,
|
|
290
|
+
release: this.#createRelease(record)
|
|
291
|
+
};
|
|
292
|
+
}
|
|
293
|
+
#abort(waiter, reason) {
|
|
294
|
+
const index = this.#waiters.indexOf(waiter);
|
|
295
|
+
if (index < 0) return;
|
|
296
|
+
this.#waiters.splice(index, 1);
|
|
297
|
+
this.#detach(waiter);
|
|
298
|
+
const ready = this.#ready.get(waiter);
|
|
299
|
+
this.#ready.delete(waiter);
|
|
300
|
+
this.#assigned.delete(waiter);
|
|
301
|
+
if (ready?.success === true) this.#recycle(ready.record);
|
|
302
|
+
waiter.reject(reason);
|
|
303
|
+
this.#commit();
|
|
304
|
+
this.#pump();
|
|
305
|
+
}
|
|
306
|
+
#detach(waiter) {
|
|
307
|
+
const entry = this.#signals.get(waiter);
|
|
308
|
+
if (entry === void 0) return;
|
|
309
|
+
AbortSignal.prototype.removeEventListener.call(entry.signal, "abort", entry.listener);
|
|
310
|
+
this.#signals.delete(waiter);
|
|
311
|
+
}
|
|
312
|
+
#pump() {
|
|
313
|
+
if (this.#ending !== void 0) return;
|
|
314
|
+
if (this.#pumping) {
|
|
315
|
+
this.#repump = true;
|
|
183
316
|
return;
|
|
184
317
|
}
|
|
185
|
-
this.#
|
|
318
|
+
this.#pumping = true;
|
|
319
|
+
do {
|
|
320
|
+
this.#repump = false;
|
|
321
|
+
for (const waiter of this.#waiters) {
|
|
322
|
+
if (this.#assigned.has(waiter) || this.#ready.has(waiter)) continue;
|
|
323
|
+
const record = this.#available.shift();
|
|
324
|
+
if (record !== void 0) {
|
|
325
|
+
this.#assigned.add(waiter);
|
|
326
|
+
this.#validating.add(record);
|
|
327
|
+
this.#startValidation(waiter, record);
|
|
328
|
+
continue;
|
|
329
|
+
}
|
|
330
|
+
if (this.#max === void 0 || this.#resources.size + this.#reservations.size < this.#max) {
|
|
331
|
+
this.#assigned.add(waiter);
|
|
332
|
+
this.#reservations.add(waiter);
|
|
333
|
+
this.#startCreate(waiter);
|
|
334
|
+
continue;
|
|
335
|
+
}
|
|
336
|
+
break;
|
|
337
|
+
}
|
|
338
|
+
} while (this.#repump);
|
|
339
|
+
this.#pumping = false;
|
|
340
|
+
}
|
|
341
|
+
#startCreate(waiter) {
|
|
342
|
+
const operation = Promise.resolve().then(() => this.#createResource(waiter));
|
|
343
|
+
this.#operations.add(operation);
|
|
344
|
+
operation.then(() => this.#completeOperation(operation), (error) => this.#failOperation(operation, waiter, error, "create"));
|
|
186
345
|
}
|
|
187
|
-
|
|
188
|
-
|
|
189
|
-
|
|
346
|
+
#startValidation(waiter, record) {
|
|
347
|
+
for (const [owned, value] of this.#resources) {
|
|
348
|
+
if (owned !== record) continue;
|
|
349
|
+
if (this.#validate === void 0) {
|
|
350
|
+
this.#validating.delete(record);
|
|
351
|
+
this.#prepare(waiter, record, value);
|
|
352
|
+
return;
|
|
353
|
+
}
|
|
354
|
+
const operation = Promise.resolve().then(() => this.#validateResource(waiter, record, value));
|
|
355
|
+
this.#operations.add(operation);
|
|
356
|
+
operation.then(() => this.#completeOperation(operation), (error) => this.#failOperation(operation, waiter, error, "invalid"));
|
|
190
357
|
return;
|
|
191
358
|
}
|
|
192
|
-
this.#
|
|
193
|
-
|
|
359
|
+
this.#validating.delete(record);
|
|
360
|
+
this.#assigned.delete(waiter);
|
|
361
|
+
this.#repump = true;
|
|
362
|
+
}
|
|
363
|
+
async #createResource(waiter) {
|
|
364
|
+
let value;
|
|
194
365
|
try {
|
|
195
|
-
|
|
366
|
+
value = await this.#create();
|
|
196
367
|
} catch (error) {
|
|
197
|
-
this.#
|
|
198
|
-
|
|
199
|
-
|
|
200
|
-
|
|
368
|
+
this.#reservations.delete(waiter);
|
|
369
|
+
if (this.#waiters.includes(waiter)) this.#ready.set(waiter, {
|
|
370
|
+
success: false,
|
|
371
|
+
error: new PoolError({
|
|
372
|
+
code: "create",
|
|
373
|
+
cause: error
|
|
374
|
+
})
|
|
375
|
+
});
|
|
376
|
+
else this.#assigned.delete(waiter);
|
|
377
|
+
this.#commit();
|
|
201
378
|
return;
|
|
202
379
|
}
|
|
380
|
+
this.#reservations.delete(waiter);
|
|
381
|
+
const record = {};
|
|
382
|
+
this.#resources.set(record, value);
|
|
203
383
|
this.#emitter.emit("create");
|
|
204
|
-
this.#
|
|
205
|
-
|
|
206
|
-
|
|
207
|
-
|
|
208
|
-
|
|
209
|
-
waiter.clear();
|
|
210
|
-
waiter.resolve(this.#token(resource));
|
|
211
|
-
this.#emitter.emit("acquire");
|
|
384
|
+
if (this.#ending !== void 0) {
|
|
385
|
+
this.#assigned.delete(waiter);
|
|
386
|
+
try {
|
|
387
|
+
await this.#dispose(record);
|
|
388
|
+
} catch {}
|
|
212
389
|
return;
|
|
213
390
|
}
|
|
214
|
-
this.#
|
|
215
|
-
|
|
216
|
-
this.#
|
|
391
|
+
if (!this.#waiters.includes(waiter)) {
|
|
392
|
+
this.#assigned.delete(waiter);
|
|
393
|
+
this.#recycle(record);
|
|
217
394
|
return;
|
|
218
395
|
}
|
|
219
|
-
this.#
|
|
220
|
-
|
|
396
|
+
this.#ready.set(waiter, {
|
|
397
|
+
success: true,
|
|
398
|
+
record,
|
|
399
|
+
token: this.#token(record, value)
|
|
400
|
+
});
|
|
401
|
+
this.#commit();
|
|
221
402
|
}
|
|
222
|
-
async #
|
|
223
|
-
|
|
403
|
+
async #validateResource(waiter, record, value) {
|
|
404
|
+
let valid = false;
|
|
405
|
+
try {
|
|
406
|
+
valid = await this.#validate?.(value) === true;
|
|
407
|
+
} catch {}
|
|
408
|
+
this.#validating.delete(record);
|
|
409
|
+
if (this.#ending !== void 0) {
|
|
410
|
+
this.#assigned.delete(waiter);
|
|
411
|
+
try {
|
|
412
|
+
await this.#dispose(record);
|
|
413
|
+
} catch {}
|
|
414
|
+
return;
|
|
415
|
+
}
|
|
416
|
+
if (!this.#waiters.includes(waiter)) {
|
|
417
|
+
this.#assigned.delete(waiter);
|
|
418
|
+
if (valid) this.#recycle(record);
|
|
419
|
+
else try {
|
|
420
|
+
await this.#dispose(record);
|
|
421
|
+
} catch (error) {
|
|
422
|
+
this.#record(error);
|
|
423
|
+
}
|
|
424
|
+
return;
|
|
425
|
+
}
|
|
426
|
+
if (valid) {
|
|
427
|
+
this.#prepare(waiter, record, value);
|
|
428
|
+
return;
|
|
429
|
+
}
|
|
224
430
|
try {
|
|
225
|
-
|
|
226
|
-
} catch {
|
|
227
|
-
|
|
431
|
+
await this.#dispose(record);
|
|
432
|
+
} catch (error) {
|
|
433
|
+
this.#assigned.delete(waiter);
|
|
434
|
+
if (this.#waiters.includes(waiter)) {
|
|
435
|
+
this.#ready.set(waiter, {
|
|
436
|
+
success: false,
|
|
437
|
+
error: new PoolError({
|
|
438
|
+
code: "cleanup",
|
|
439
|
+
cause: error
|
|
440
|
+
})
|
|
441
|
+
});
|
|
442
|
+
this.#commit();
|
|
443
|
+
return;
|
|
444
|
+
}
|
|
445
|
+
this.#record(error);
|
|
446
|
+
this.#pump();
|
|
447
|
+
return;
|
|
228
448
|
}
|
|
449
|
+
this.#assigned.delete(waiter);
|
|
450
|
+
this.#pump();
|
|
229
451
|
}
|
|
230
|
-
|
|
231
|
-
if (this.#
|
|
232
|
-
this.#
|
|
452
|
+
#prepare(waiter, record, value) {
|
|
453
|
+
if (!this.#waiters.includes(waiter) || this.#ending !== void 0) {
|
|
454
|
+
this.#assigned.delete(waiter);
|
|
455
|
+
this.#recycle(record);
|
|
233
456
|
return;
|
|
234
457
|
}
|
|
458
|
+
this.#ready.set(waiter, {
|
|
459
|
+
success: true,
|
|
460
|
+
record,
|
|
461
|
+
token: this.#token(record, value)
|
|
462
|
+
});
|
|
463
|
+
this.#commit();
|
|
464
|
+
}
|
|
465
|
+
#commit() {
|
|
466
|
+
while (this.#ending === void 0) {
|
|
467
|
+
const waiter = this.#waiters[0];
|
|
468
|
+
if (waiter === void 0) return;
|
|
469
|
+
const result = this.#ready.get(waiter);
|
|
470
|
+
if (result === void 0) return;
|
|
471
|
+
this.#waiters.shift();
|
|
472
|
+
this.#repump = true;
|
|
473
|
+
this.#ready.delete(waiter);
|
|
474
|
+
this.#assigned.delete(waiter);
|
|
475
|
+
this.#detach(waiter);
|
|
476
|
+
if (!result.success) {
|
|
477
|
+
waiter.reject(result.error);
|
|
478
|
+
continue;
|
|
479
|
+
}
|
|
480
|
+
this.#leased.add(result.record);
|
|
481
|
+
waiter.resolve(result.token);
|
|
482
|
+
this.#emitter.emit("acquire");
|
|
483
|
+
}
|
|
484
|
+
}
|
|
485
|
+
#completeOperation(operation) {
|
|
486
|
+
this.#operations.delete(operation);
|
|
487
|
+
this.#commit();
|
|
488
|
+
this.#pump();
|
|
489
|
+
this.#finish();
|
|
490
|
+
}
|
|
491
|
+
#failOperation(operation, waiter, error, code) {
|
|
492
|
+
this.#operations.delete(operation);
|
|
493
|
+
this.#reservations.delete(waiter);
|
|
494
|
+
this.#assigned.delete(waiter);
|
|
495
|
+
if (this.#waiters.includes(waiter)) this.#ready.set(waiter, {
|
|
496
|
+
success: false,
|
|
497
|
+
error: new PoolError({
|
|
498
|
+
code,
|
|
499
|
+
cause: error
|
|
500
|
+
})
|
|
501
|
+
});
|
|
502
|
+
this.#commit();
|
|
503
|
+
this.#pump();
|
|
504
|
+
this.#finish();
|
|
505
|
+
}
|
|
506
|
+
#release(record) {
|
|
507
|
+
if (!this.#leased.delete(record)) return;
|
|
508
|
+
this.#recycle(record);
|
|
509
|
+
}
|
|
510
|
+
#recycle(record) {
|
|
511
|
+
if (this.#ending !== void 0) {
|
|
512
|
+
this.#own(this.#dispose(record));
|
|
513
|
+
return;
|
|
514
|
+
}
|
|
515
|
+
this.#available.push(record);
|
|
516
|
+
this.#pump();
|
|
517
|
+
if (this.#available.includes(record)) this.#emitter.emit("release");
|
|
518
|
+
}
|
|
519
|
+
#dispose(record) {
|
|
520
|
+
const existing = this.#destroying.get(record);
|
|
521
|
+
if (existing !== void 0) return existing;
|
|
522
|
+
for (const [owned, value] of this.#resources) {
|
|
523
|
+
if (owned !== record) continue;
|
|
524
|
+
const cleanup = Promise.withResolvers();
|
|
525
|
+
this.#destroying.set(record, cleanup.promise);
|
|
526
|
+
const index = this.#available.indexOf(record);
|
|
527
|
+
if (index >= 0) this.#available.splice(index, 1);
|
|
528
|
+
this.#validating.delete(record);
|
|
529
|
+
this.#leased.delete(record);
|
|
530
|
+
if (this.#ending !== void 0) this.#own(cleanup.promise);
|
|
531
|
+
this.#clean(record, value, cleanup);
|
|
532
|
+
return cleanup.promise;
|
|
533
|
+
}
|
|
534
|
+
return Promise.resolve();
|
|
535
|
+
}
|
|
536
|
+
async #clean(record, value, cleanup) {
|
|
537
|
+
let attempt;
|
|
235
538
|
try {
|
|
236
|
-
|
|
237
|
-
} catch {
|
|
539
|
+
attempt = Promise.resolve(this.#cleanup?.(value));
|
|
540
|
+
} catch (error) {
|
|
541
|
+
attempt = Promise.reject(error);
|
|
542
|
+
}
|
|
543
|
+
let failure;
|
|
544
|
+
let failed = false;
|
|
545
|
+
try {
|
|
546
|
+
await attempt;
|
|
547
|
+
} catch (error) {
|
|
548
|
+
failure = error;
|
|
549
|
+
failed = true;
|
|
550
|
+
}
|
|
551
|
+
this.#resources.delete(record);
|
|
552
|
+
if (failed) cleanup.reject(failure);
|
|
553
|
+
else cleanup.resolve();
|
|
554
|
+
await Promise.resolve();
|
|
238
555
|
this.#emitter.emit("destroy");
|
|
556
|
+
this.#destroying.delete(record);
|
|
557
|
+
this.#finish();
|
|
558
|
+
}
|
|
559
|
+
async #settleClear(cleanups) {
|
|
560
|
+
const settled = await Promise.allSettled(cleanups);
|
|
561
|
+
const failures = [];
|
|
562
|
+
for (const result of settled) if (result.status === "rejected" && !failures.some((failure) => Object.is(failure, result.reason))) failures.push(result.reason);
|
|
563
|
+
if (failures.length > 0) throw this.#cleanupError(failures);
|
|
564
|
+
}
|
|
565
|
+
#own(cleanup) {
|
|
566
|
+
if (this.#owned.has(cleanup)) return;
|
|
567
|
+
this.#owned.add(cleanup);
|
|
568
|
+
cleanup.then(() => this.#completeCleanup(cleanup), (error) => this.#failCleanup(cleanup, error));
|
|
569
|
+
}
|
|
570
|
+
#completeCleanup(cleanup) {
|
|
571
|
+
this.#owned.delete(cleanup);
|
|
572
|
+
this.#finish();
|
|
573
|
+
}
|
|
574
|
+
#failCleanup(cleanup, error) {
|
|
575
|
+
this.#owned.delete(cleanup);
|
|
576
|
+
this.#record(error);
|
|
577
|
+
this.#finish();
|
|
578
|
+
}
|
|
579
|
+
#record(error) {
|
|
580
|
+
if (!this.#failures.some((failure) => Object.is(failure, error))) this.#failures.push(error);
|
|
581
|
+
}
|
|
582
|
+
#cleanupError(failures) {
|
|
583
|
+
return new PoolError({
|
|
584
|
+
code: "cleanup",
|
|
585
|
+
...failures[0] === void 0 ? {} : { cause: failures[0] },
|
|
586
|
+
context: { failures: [...failures] }
|
|
587
|
+
});
|
|
588
|
+
}
|
|
589
|
+
#finish() {
|
|
590
|
+
const ending = this.#ending;
|
|
591
|
+
if (ending === void 0 || this.#operations.size > 0 || this.#owned.size > 0) return;
|
|
592
|
+
let claimed = false;
|
|
593
|
+
for (const record of this.#resources.keys()) {
|
|
594
|
+
if (this.#validating.has(record) || this.#destroying.has(record)) continue;
|
|
595
|
+
claimed = true;
|
|
596
|
+
this.#own(this.#dispose(record));
|
|
597
|
+
}
|
|
598
|
+
if (claimed || this.#resources.size > 0 || this.#destroying.size > 0) return;
|
|
599
|
+
this.#emitter.destroy();
|
|
600
|
+
if (this.#failures.length > 0) ending.reject(this.#cleanupError(this.#failures));
|
|
601
|
+
else ending.resolve();
|
|
239
602
|
}
|
|
240
603
|
};
|
|
241
604
|
//#endregion
|
|
242
605
|
//#region src/core/factories.ts
|
|
243
606
|
/**
|
|
244
|
-
* Create a
|
|
245
|
-
* resource (reusing a validated idle one, growing up to `max`, or parking until a
|
|
246
|
-
* `release` frees one) and the returned token's `release` returns it for reuse.
|
|
607
|
+
* Create a resource pool with optional bounded capacity, unique ownership, and FIFO settlement.
|
|
247
608
|
*
|
|
248
609
|
* @remarks
|
|
249
|
-
*
|
|
250
|
-
*
|
|
251
|
-
*
|
|
252
|
-
* no eviction timers — and observable (§13): a typed `emitter` surfaces
|
|
253
|
-
* `create` / `acquire` / `release` / `destroy`.
|
|
610
|
+
* Concurrent create and validation hooks may overlap, while acquire promises settle in
|
|
611
|
+
* request order. `clear` owns its idle snapshot; `destroy` returns one stable barrier and
|
|
612
|
+
* waits for every in-flight hook and cleanup before destroying the emitter last.
|
|
254
613
|
*
|
|
255
614
|
* @typeParam T - The pooled resource type
|
|
256
|
-
* @param options -
|
|
615
|
+
* @param options - Lifecycle hooks, optional positive safe `max`, and observation hooks
|
|
257
616
|
* @returns A working {@link PoolInterface}
|
|
258
617
|
*
|
|
259
618
|
* @example
|
|
260
619
|
* ```ts
|
|
261
|
-
* import { createPool } from '@
|
|
620
|
+
* import { createPool } from '@orkestrel/pool'
|
|
262
621
|
*
|
|
263
622
|
* const pool = createPool<Connection>({
|
|
264
623
|
* create: () => connect(),
|
|
@@ -279,6 +638,6 @@ function createPool(options) {
|
|
|
279
638
|
return new Pool(options);
|
|
280
639
|
}
|
|
281
640
|
//#endregion
|
|
282
|
-
export { Pool, createPool };
|
|
641
|
+
export { Pool, PoolError, createPool, isPoolError, isPoolMax, isPoolSignal };
|
|
283
642
|
|
|
284
643
|
//# sourceMappingURL=index.js.map
|