@zackbart/connecta 0.24.4 → 0.26.0
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/AGENTS.md +43 -12
- package/CHANGELOG.md +524 -0
- package/README.md +32 -15
- package/SECURITY.md +6 -5
- package/bin/connecta.mjs +5 -1
- package/dist/activity.d.ts +10 -3
- package/dist/activity.js +10 -1
- package/dist/advertised-schema.js +53 -0
- package/dist/artifacts/connector.d.ts +55 -0
- package/dist/artifacts/connector.js +724 -0
- package/dist/artifacts/document.js +76 -0
- package/dist/artifacts/guide.js +140 -0
- package/dist/artifacts/html-scan.js +329 -0
- package/dist/artifacts/json.js +68 -0
- package/dist/artifacts/kv-store.d.ts +20 -0
- package/dist/artifacts/kv-store.js +140 -0
- package/dist/artifacts/markdown.js +449 -0
- package/dist/artifacts/module.d.ts +37 -0
- package/dist/artifacts/module.js +60 -0
- package/dist/artifacts/operations.d.ts +195 -0
- package/dist/artifacts/operations.js +944 -0
- package/dist/artifacts/refresh.d.ts +53 -0
- package/dist/artifacts/refresh.js +235 -0
- package/dist/artifacts/routes.js +194 -0
- package/dist/artifacts/types.d.ts +214 -0
- package/dist/artifacts/types.js +22 -0
- package/dist/artifacts/validate.d.ts +62 -0
- package/dist/artifacts/validate.js +652 -0
- package/dist/artifacts.d.ts +6 -0
- package/dist/artifacts.js +9 -0
- package/dist/auth/downstream-oauth.js +490 -106
- package/dist/call-admission.js +139 -113
- package/dist/catalog-service.js +152 -106
- package/dist/catalog.js +297 -38
- package/dist/concurrency.js +24 -23
- package/dist/connector-access.d.ts +18 -4
- package/dist/connector-access.js +65 -20
- package/dist/connector-caller.d.ts +8 -0
- package/dist/connector-caller.js +19 -0
- package/dist/connector-scope.js +6 -37
- package/dist/connectors/api.d.ts +2 -2
- package/dist/connectors/remote-mcp.d.ts +6 -5
- package/dist/connectors/remote-mcp.js +330 -240
- package/dist/credential-contract.d.ts +9 -0
- package/dist/credential-rules.js +2 -2
- package/dist/credentials.d.ts +6 -1
- package/dist/credentials.js +45 -3
- package/dist/errors.d.ts +16 -1
- package/dist/errors.js +28 -0
- package/dist/execute.js +745 -282
- package/dist/executor-admission.d.ts +11 -1
- package/dist/executor-admission.js +145 -63
- package/dist/executors/quickjs-runtime.js +4 -2
- package/dist/executors/quickjs.js +255 -212
- package/dist/fix-prompt.js +37 -0
- package/dist/index.d.ts +72 -7
- package/dist/index.js +281 -26
- package/dist/invocation.js +371 -286
- package/dist/meta-tools.js +213 -94
- package/dist/module-contracts.d.ts +21 -1
- package/dist/oauth-callback-outcome.js +94 -0
- package/dist/oauth-sealing.js +29 -0
- package/dist/operator-ui/fix-prompts.js +87 -0
- package/dist/operator-ui/generated.js +2 -2
- package/dist/operator-ui/model.d.ts +103 -1
- package/dist/operator-ui/setup-commands.js +53 -0
- package/dist/operator-ui/view.js +128 -3
- package/dist/program-source.js +321 -0
- package/dist/providers/notion.js +2 -2
- package/dist/providers/revenuecat.js +32 -5
- package/dist/providers/stripe.d.ts +3 -2
- package/dist/providers/vercel.js +1 -1
- package/dist/registry.d.ts +43 -7
- package/dist/registry.js +434 -288
- package/dist/resumable.js +1291 -0
- package/dist/routes/activity.js +74 -154
- package/dist/routes/actor-labels.js +114 -0
- package/dist/routes/credentials.js +196 -163
- package/dist/routes/mcp.js +228 -112
- package/dist/routes/oauth-management.js +73 -71
- package/dist/routes/oauth.js +44 -16
- package/dist/routes/operator.js +83 -0
- package/dist/routes/shared.d.ts +25 -1
- package/dist/routes/shared.js +20 -2
- package/dist/routes/ui-data.js +211 -0
- package/dist/routes/ui.js +76 -35
- package/dist/run-journal.js +347 -0
- package/dist/runtime/admission.js +43 -0
- package/dist/runtime/call-admission.js +67 -0
- package/dist/runtime/connector-scope.js +72 -0
- package/dist/runtime/run.js +174 -0
- package/dist/runtime/services.js +130 -0
- package/dist/runtime/shared-read.js +106 -0
- package/dist/runtime/storage.js +66 -0
- package/dist/server.js +137 -89
- package/dist/skills.js +28 -9
- package/dist/storage/file.js +39 -2
- package/dist/storage/memory.js +28 -16
- package/dist/timeout.js +39 -43
- package/dist/tool-safety.d.ts +27 -1
- package/dist/tool-safety.js +31 -0
- package/dist/types.d.ts +45 -7
- package/dist/ui.d.ts +25 -4
- package/dist/ui.js +77 -167
- package/dist/url-safety.js +157 -0
- package/dist/version.d.ts +1 -1
- package/dist/version.js +1 -1
- package/documentation/architecture.md +367 -16
- package/documentation/auth.md +163 -2
- package/documentation/code-mode.md +274 -56
- package/documentation/meta-tools.md +200 -44
- package/ethos.md +46 -47
- package/examples/worker/AGENTS.md +6 -0
- package/examples/worker/README.md +224 -14
- package/examples/worker/src/d1-activity-row.ts +14 -2
- package/examples/worker/src/d1-activity.ts +4 -2
- package/examples/worker/src/d1-storage.ts +116 -0
- package/examples/worker/src/index.ts +42 -7
- package/examples/worker/src/r2-artifact-blobs.ts +29 -0
- package/examples/worker/wrangler.jsonc +21 -5
- package/package.json +16 -4
- package/templates/node/AGENTS.md +9 -2
- package/templates/node/README.md +31 -3
- package/templates/node/package.json +1 -1
- package/templates/node/src/index.ts +27 -0
- package/dist/auth/downstream-oauth.d.ts +0 -200
- package/dist/catalog-fingerprint.d.ts +0 -19
- package/dist/catalog-limits.d.ts +0 -9
- package/dist/catalog-service.d.ts +0 -263
- package/dist/catalog.d.ts +0 -87
- package/dist/concurrency.d.ts +0 -3
- package/dist/connectors/guarded-fetch.d.ts +0 -96
- package/dist/credential-rules.d.ts +0 -85
- package/dist/execute.d.ts +0 -154
- package/dist/executor-result.d.ts +0 -10
- package/dist/executors/quickjs-child.d.ts +0 -1
- package/dist/executors/quickjs-protocol.d.ts +0 -67
- package/dist/favicon.d.ts +0 -1
- package/dist/identity.d.ts +0 -4
- package/dist/invocation.d.ts +0 -74
- package/dist/mcp-result.d.ts +0 -8
- package/dist/meta-tools.d.ts +0 -129
- package/dist/operator-ui/generated.d.ts +0 -2
- package/dist/operator-ui/view.d.ts +0 -188
- package/dist/result-shapes.d.ts +0 -13
- package/dist/retired-toolkits.d.ts +0 -2
- package/dist/routes/activity.d.ts +0 -2
- package/dist/routes/credentials.d.ts +0 -2
- package/dist/routes/mcp.d.ts +0 -10
- package/dist/routes/oauth-management.d.ts +0 -2
- package/dist/routes/oauth.d.ts +0 -2
- package/dist/routes/ui.d.ts +0 -2
- package/dist/server.d.ts +0 -9
- package/dist/skills.d.ts +0 -44
- package/dist/timeout.d.ts +0 -21
package/dist/call-admission.js
CHANGED
|
@@ -1,4 +1,7 @@
|
|
|
1
|
+
import { Deferred, Duration, Effect } from "effect";
|
|
1
2
|
import { ConnectorCallError } from "./errors.js";
|
|
3
|
+
import { provideCallAdmissionProgram, startCallAdmission, } from "./runtime/call-admission.js";
|
|
4
|
+
import { fromSignal, runEdge } from "./runtime/run.js";
|
|
2
5
|
const DEFAULT_MAX_QUEUE_SIZE = 32;
|
|
3
6
|
const DEFAULT_QUEUE_TIMEOUT_MS = 5_000;
|
|
4
7
|
const DEFAULT_RETRY_AFTER_MS = 1_000;
|
|
@@ -117,103 +120,10 @@ export class ConnectorCallAdmissionController {
|
|
|
117
120
|
this.partitionKey = rule.partitionKey;
|
|
118
121
|
}
|
|
119
122
|
acquire(input) {
|
|
120
|
-
//
|
|
121
|
-
// `input
|
|
122
|
-
// limiter's payload-free state contract.
|
|
123
|
-
|
|
124
|
-
if (this.closed) {
|
|
125
|
-
return Promise.reject(new CallAdmissionError("closed", "unavailable", `Connector "${this.connectorId}" call admission is closed.`));
|
|
126
|
-
}
|
|
127
|
-
if (signal?.aborted) {
|
|
128
|
-
this.cancelledTotal++;
|
|
129
|
-
return Promise.reject(this.cancelled(signal));
|
|
130
|
-
}
|
|
131
|
-
let key;
|
|
132
|
-
try {
|
|
133
|
-
key = this.partitionKey
|
|
134
|
-
? this.partitionKey({
|
|
135
|
-
toolName: input.toolName,
|
|
136
|
-
args: input.args,
|
|
137
|
-
})
|
|
138
|
-
: DEFAULT_PARTITION_KEY;
|
|
139
|
-
}
|
|
140
|
-
catch (cause) {
|
|
141
|
-
this.rejectedTotal++;
|
|
142
|
-
return Promise.reject(new CallAdmissionError("partition", "connector_call_failed", `Connector "${this.connectorId}" call-admission partitionKey threw.`, { cause }));
|
|
143
|
-
}
|
|
144
|
-
if (typeof key !== "string" ||
|
|
145
|
-
enc.encode(key).length > MAX_PARTITION_KEY_BYTES) {
|
|
146
|
-
this.rejectedTotal++;
|
|
147
|
-
return Promise.reject(new CallAdmissionError("partition", "connector_call_failed", `Connector "${this.connectorId}" call-admission partitionKey must return a string of at most ${MAX_PARTITION_KEY_BYTES} UTF-8 bytes.`));
|
|
148
|
-
}
|
|
149
|
-
// A partition callback is operator code and may synchronously abort the
|
|
150
|
-
// caller. Recheck after it returns so that cancellation cannot consume a
|
|
151
|
-
// budget entry or concurrency slot.
|
|
152
|
-
if (signal?.aborted) {
|
|
153
|
-
this.cancelledTotal++;
|
|
154
|
-
return Promise.reject(this.cancelled(signal));
|
|
155
|
-
}
|
|
156
|
-
const now = Date.now();
|
|
157
|
-
let state = this.partitions.get(key);
|
|
158
|
-
if (!state) {
|
|
159
|
-
this.evictIdlePartitions(now);
|
|
160
|
-
if (this.partitions.size >= this.maxPartitions) {
|
|
161
|
-
this.rejectedTotal++;
|
|
162
|
-
return Promise.reject(new CallAdmissionError("partition", "rate_limited", `Connector "${this.connectorId}" call-admission partition capacity is exhausted.`, { retryAfterMs: this.retryAfterMs }));
|
|
163
|
-
}
|
|
164
|
-
state = {
|
|
165
|
-
active: 0,
|
|
166
|
-
waiters: [],
|
|
167
|
-
admittedAt: [],
|
|
168
|
-
};
|
|
169
|
-
this.partitions.set(key, state);
|
|
170
|
-
}
|
|
171
|
-
this.pruneBudget(state, now);
|
|
172
|
-
const budgetRetryAfterMs = this.budgetRetryAfterMs(state, now);
|
|
173
|
-
if (budgetRetryAfterMs !== undefined) {
|
|
174
|
-
this.rateLimitedTotal++;
|
|
175
|
-
return Promise.reject(this.budgetLimited(budgetRetryAfterMs));
|
|
176
|
-
}
|
|
177
|
-
if (this.maxConcurrency === undefined ||
|
|
178
|
-
state.active < this.maxConcurrency) {
|
|
179
|
-
return Promise.resolve(this.admit(state, now, 0));
|
|
180
|
-
}
|
|
181
|
-
if (state.waiters.length >= this.maxQueueSize) {
|
|
182
|
-
this.rejectedTotal++;
|
|
183
|
-
return Promise.reject(this.concurrencyLimited("queue is full"));
|
|
184
|
-
}
|
|
185
|
-
return new Promise((resolve, reject) => {
|
|
186
|
-
const waiter = {
|
|
187
|
-
queuedAt: now,
|
|
188
|
-
resolve,
|
|
189
|
-
reject,
|
|
190
|
-
...(signal ? { signal } : {}),
|
|
191
|
-
};
|
|
192
|
-
waiter.onAbort = () => {
|
|
193
|
-
if (!this.removeWaiter(state, waiter))
|
|
194
|
-
return;
|
|
195
|
-
this.cleanupWaiter(waiter);
|
|
196
|
-
this.cancelledTotal++;
|
|
197
|
-
reject(this.cancelled(signal));
|
|
198
|
-
this.maybeDeletePartition(key, state, Date.now());
|
|
199
|
-
};
|
|
200
|
-
waiter.timer = setTimeout(() => {
|
|
201
|
-
if (!this.removeWaiter(state, waiter))
|
|
202
|
-
return;
|
|
203
|
-
this.cleanupWaiter(waiter);
|
|
204
|
-
this.rejectedTotal++;
|
|
205
|
-
reject(this.concurrencyLimited(`queue wait exceeded ${this.queueTimeoutMs}ms`));
|
|
206
|
-
this.maybeDeletePartition(key, state, Date.now());
|
|
207
|
-
}, this.queueTimeoutMs);
|
|
208
|
-
state.waiters.push(waiter);
|
|
209
|
-
this.queuedTotal++;
|
|
210
|
-
signal?.addEventListener("abort", waiter.onAbort, { once: true });
|
|
211
|
-
// Close the check-to-listener race: an abort before registration is not
|
|
212
|
-
// replayed by AbortSignal, so inspect it once after the waiter is fully
|
|
213
|
-
// removable and its timer is installed.
|
|
214
|
-
if (signal?.aborted)
|
|
215
|
-
waiter.onAbort();
|
|
216
|
-
});
|
|
123
|
+
// The checks run now and the returned effect only waits. It closes over
|
|
124
|
+
// the partition key, not `input`, so a queued call does not retain its
|
|
125
|
+
// `args` — the limiter's payload-free state contract.
|
|
126
|
+
return runEdge(startCallAdmission(this, input));
|
|
217
127
|
}
|
|
218
128
|
/** A principal registry may be evicted only after calls and budgets drain. */
|
|
219
129
|
isIdle() {
|
|
@@ -253,8 +163,7 @@ export class ConnectorCallAdmissionController {
|
|
|
253
163
|
this.closed = true;
|
|
254
164
|
for (const state of this.partitions.values()) {
|
|
255
165
|
for (const waiter of state.waiters.splice(0)) {
|
|
256
|
-
|
|
257
|
-
waiter.reject(new CallAdmissionError("closed", "unavailable", `Connector "${this.connectorId}" call admission is closed.`));
|
|
166
|
+
Deferred.doneUnsafe(waiter.outcome, Effect.fail(new CallAdmissionError("closed", "unavailable", `Connector "${this.connectorId}" call admission is closed.`)));
|
|
258
167
|
}
|
|
259
168
|
}
|
|
260
169
|
}
|
|
@@ -287,22 +196,24 @@ export class ConnectorCallAdmissionController {
|
|
|
287
196
|
while (state.active < this.maxConcurrency &&
|
|
288
197
|
state.waiters.length > 0) {
|
|
289
198
|
const waiter = state.waiters.shift();
|
|
290
|
-
|
|
291
|
-
|
|
292
|
-
|
|
293
|
-
|
|
294
|
-
|
|
295
|
-
|
|
296
|
-
|
|
199
|
+
// The waiter's signal is not read here. This runs in whichever request
|
|
200
|
+
// released a slot, and on Workers reading an AbortSignal that another
|
|
201
|
+
// request created throws, failing the releaser and stranding the
|
|
202
|
+
// waiter. Nor does it need reading: a waiter whose signal aborted took
|
|
203
|
+
// itself out of the queue when it did, from its own abort listener.
|
|
204
|
+
//
|
|
205
|
+
// The waiter's own clock: the fiber's Clock for an Effect caller, the
|
|
206
|
+
// live `Date.now` for a Promise one.
|
|
207
|
+
const now = waiter.now();
|
|
297
208
|
this.pruneBudget(state, now);
|
|
298
209
|
const retryAfterMs = this.budgetRetryAfterMs(state, now);
|
|
299
210
|
if (retryAfterMs !== undefined) {
|
|
300
211
|
this.rateLimitedTotal++;
|
|
301
|
-
waiter.
|
|
212
|
+
Deferred.doneUnsafe(waiter.outcome, Effect.fail(this.budgetLimited(retryAfterMs)));
|
|
302
213
|
continue;
|
|
303
214
|
}
|
|
304
215
|
const waitMs = Math.max(0, now - waiter.queuedAt);
|
|
305
|
-
waiter.
|
|
216
|
+
Deferred.doneUnsafe(waiter.outcome, Effect.succeed(this.admit(state, now, waitMs)));
|
|
306
217
|
}
|
|
307
218
|
}
|
|
308
219
|
pruneBudget(state, now) {
|
|
@@ -350,12 +261,20 @@ export class ConnectorCallAdmissionController {
|
|
|
350
261
|
state.waiters.splice(index, 1);
|
|
351
262
|
return true;
|
|
352
263
|
}
|
|
353
|
-
|
|
354
|
-
|
|
355
|
-
|
|
356
|
-
|
|
357
|
-
|
|
264
|
+
// The waiting fiber was interrupted — an Effect caller's scope closed, not a
|
|
265
|
+
// signal. Withdraw the waiter if it is still queued; if the pump handed it a
|
|
266
|
+
// permit in the same breath, give the slot back, because nobody is left to
|
|
267
|
+
// receive it. (A line comment, not JSDoc: TypeScript copies a private
|
|
268
|
+
// member's JSDoc into the published declaration.)
|
|
269
|
+
cleanupWaiter(key, state, waiter) {
|
|
270
|
+
if (this.removeWaiter(state, waiter)) {
|
|
271
|
+
this.cancelledTotal++;
|
|
272
|
+
this.maybeDeletePartition(key, state, waiter.now());
|
|
273
|
+
return Effect.void;
|
|
358
274
|
}
|
|
275
|
+
if (!Deferred.isDoneUnsafe(waiter.outcome))
|
|
276
|
+
return Effect.void;
|
|
277
|
+
return Deferred.await(waiter.outcome).pipe(Effect.tap((permit) => Effect.sync(() => permit.release())), Effect.ignore);
|
|
359
278
|
}
|
|
360
279
|
concurrencyLimited(reason) {
|
|
361
280
|
return new CallAdmissionError("concurrency", "rate_limited", `Connector "${this.connectorId}" call concurrency ${reason}.`, { retryAfterMs: this.retryAfterMs });
|
|
@@ -366,4 +285,111 @@ export class ConnectorCallAdmissionController {
|
|
|
366
285
|
cancelled(signal) {
|
|
367
286
|
return new CallAdmissionError("cancelled", "timeout", `Connector "${this.connectorId}" call was cancelled before admission.`, { cause: signal.reason });
|
|
368
287
|
}
|
|
288
|
+
// The admission program, handed to src/runtime/call-admission.ts. It lives
|
|
289
|
+
// in a static block because the class declaration ships, private member
|
|
290
|
+
// names and all: a new method or field would change the published `.d.ts`,
|
|
291
|
+
// and a static block is the one place outside an instance method that may
|
|
292
|
+
// read this bookkeeping while emitting nothing there.
|
|
293
|
+
//
|
|
294
|
+
// Calling the program decides: everything up to a grant, a refusal, or a
|
|
295
|
+
// place in the queue happens synchronously, and only the queued wait is left
|
|
296
|
+
// for a fiber. Semaphore does not fit that wait — permits here are
|
|
297
|
+
// per-partition, the queue is bounded and counted, and each waiter carries
|
|
298
|
+
// its own timeout and budget recheck — so the queue stays explicit, as in
|
|
299
|
+
// AdmissionController, with a Deferred per waiter.
|
|
300
|
+
static {
|
|
301
|
+
provideCallAdmissionProgram((controller, input, now) => {
|
|
302
|
+
// Copy the signal out before building the wait: the effect returned
|
|
303
|
+
// below must not reach `input`, or a queued call would retain `args`.
|
|
304
|
+
const signal = input.signal;
|
|
305
|
+
if (controller.closed) {
|
|
306
|
+
return Effect.fail(new CallAdmissionError("closed", "unavailable", `Connector "${controller.connectorId}" call admission is closed.`));
|
|
307
|
+
}
|
|
308
|
+
if (signal?.aborted) {
|
|
309
|
+
controller.cancelledTotal++;
|
|
310
|
+
return Effect.fail(controller.cancelled(signal));
|
|
311
|
+
}
|
|
312
|
+
let key;
|
|
313
|
+
try {
|
|
314
|
+
key = controller.partitionKey
|
|
315
|
+
? controller.partitionKey({
|
|
316
|
+
toolName: input.toolName,
|
|
317
|
+
args: input.args,
|
|
318
|
+
})
|
|
319
|
+
: DEFAULT_PARTITION_KEY;
|
|
320
|
+
}
|
|
321
|
+
catch (cause) {
|
|
322
|
+
controller.rejectedTotal++;
|
|
323
|
+
return Effect.fail(new CallAdmissionError("partition", "connector_call_failed", `Connector "${controller.connectorId}" call-admission partitionKey threw.`, { cause }));
|
|
324
|
+
}
|
|
325
|
+
if (typeof key !== "string" ||
|
|
326
|
+
enc.encode(key).length > MAX_PARTITION_KEY_BYTES) {
|
|
327
|
+
controller.rejectedTotal++;
|
|
328
|
+
return Effect.fail(new CallAdmissionError("partition", "connector_call_failed", `Connector "${controller.connectorId}" call-admission partitionKey must return a string of at most ${MAX_PARTITION_KEY_BYTES} UTF-8 bytes.`));
|
|
329
|
+
}
|
|
330
|
+
// A partition callback is operator code and may synchronously abort the
|
|
331
|
+
// caller. Recheck after it returns so that cancellation cannot consume a
|
|
332
|
+
// budget entry or concurrency slot.
|
|
333
|
+
if (signal?.aborted) {
|
|
334
|
+
controller.cancelledTotal++;
|
|
335
|
+
return Effect.fail(controller.cancelled(signal));
|
|
336
|
+
}
|
|
337
|
+
const queuedAt = now();
|
|
338
|
+
let found = controller.partitions.get(key);
|
|
339
|
+
if (!found) {
|
|
340
|
+
controller.evictIdlePartitions(queuedAt);
|
|
341
|
+
if (controller.partitions.size >= controller.maxPartitions) {
|
|
342
|
+
controller.rejectedTotal++;
|
|
343
|
+
return Effect.fail(new CallAdmissionError("partition", "rate_limited", `Connector "${controller.connectorId}" call-admission partition capacity is exhausted.`, { retryAfterMs: controller.retryAfterMs }));
|
|
344
|
+
}
|
|
345
|
+
found = { active: 0, waiters: [], admittedAt: [] };
|
|
346
|
+
controller.partitions.set(key, found);
|
|
347
|
+
}
|
|
348
|
+
const state = found;
|
|
349
|
+
controller.pruneBudget(state, queuedAt);
|
|
350
|
+
const budgetRetryAfterMs = controller.budgetRetryAfterMs(state, queuedAt);
|
|
351
|
+
if (budgetRetryAfterMs !== undefined) {
|
|
352
|
+
controller.rateLimitedTotal++;
|
|
353
|
+
return Effect.fail(controller.budgetLimited(budgetRetryAfterMs));
|
|
354
|
+
}
|
|
355
|
+
if (controller.maxConcurrency === undefined ||
|
|
356
|
+
state.active < controller.maxConcurrency) {
|
|
357
|
+
return Effect.succeed(controller.admit(state, queuedAt, 0));
|
|
358
|
+
}
|
|
359
|
+
if (state.waiters.length >= controller.maxQueueSize) {
|
|
360
|
+
controller.rejectedTotal++;
|
|
361
|
+
return Effect.fail(controller.concurrencyLimited("queue is full"));
|
|
362
|
+
}
|
|
363
|
+
const waiter = {
|
|
364
|
+
queuedAt,
|
|
365
|
+
now,
|
|
366
|
+
outcome: Deferred.makeUnsafe(),
|
|
367
|
+
};
|
|
368
|
+
state.waiters.push(waiter);
|
|
369
|
+
controller.queuedTotal++;
|
|
370
|
+
// A timeout or abort settles the waiter only if it takes it out of the
|
|
371
|
+
// queue first. Otherwise the pump or close() got there in the same tick,
|
|
372
|
+
// and the outcome they recorded is the answer.
|
|
373
|
+
const giveUp = (count, error) => Effect.suspend(() => {
|
|
374
|
+
if (!controller.removeWaiter(state, waiter)) {
|
|
375
|
+
return Deferred.await(waiter.outcome);
|
|
376
|
+
}
|
|
377
|
+
count();
|
|
378
|
+
const refusal = error();
|
|
379
|
+
controller.maybeDeletePartition(key, state, now());
|
|
380
|
+
return Effect.fail(refusal);
|
|
381
|
+
});
|
|
382
|
+
// One flat race; each contender is a forked fiber. The abort contender
|
|
383
|
+
// checks the signal as it starts, which closes the check-to-listener
|
|
384
|
+
// race an AbortSignal would not replay.
|
|
385
|
+
const contenders = [
|
|
386
|
+
Deferred.await(waiter.outcome),
|
|
387
|
+
Effect.sleep(Duration.millis(controller.queueTimeoutMs)).pipe(Effect.andThen(giveUp(() => controller.rejectedTotal++, () => controller.concurrencyLimited(`queue wait exceeded ${controller.queueTimeoutMs}ms`)))),
|
|
388
|
+
];
|
|
389
|
+
if (signal) {
|
|
390
|
+
contenders.push(fromSignal(signal).pipe(Effect.catch(() => giveUp(() => controller.cancelledTotal++, () => controller.cancelled(signal)))));
|
|
391
|
+
}
|
|
392
|
+
return Effect.raceAllFirst(contenders).pipe(Effect.onInterrupt(() => controller.cleanupWaiter(key, state, waiter)));
|
|
393
|
+
});
|
|
394
|
+
}
|
|
369
395
|
}
|