@jfrz38/adaptive-admission-node 0.0.0-stage → 0.2.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/COMPONENT.md +60 -0
- package/README.md +200 -2
- package/component/COMPONENT.json +44 -0
- package/component/SHA256SUMS +5 -0
- package/component/adaptive-admission-build-v0.1.0.json +36 -0
- package/component/adaptive-admission-component-v0.1.0.wasm +0 -0
- package/component/adaptive-admission-conformance-v0.1.0.tar.gz +0 -0
- package/component/adaptive-admission-release-v0.1.0.json +34 -0
- package/component/adaptive-admission-world-v0.1.0.wit +193 -0
- package/dist/component-runtime.d.ts +13 -0
- package/dist/component-runtime.js +26 -0
- package/dist/configuration.d.ts +4 -0
- package/dist/configuration.js +81 -0
- package/dist/controller.d.ts +48 -0
- package/dist/controller.js +330 -0
- package/dist/errors.d.ts +38 -0
- package/dist/errors.js +68 -0
- package/dist/generated/adaptive-admission.core.wasm +0 -0
- package/dist/generated/adaptive-admission.core2.wasm +0 -0
- package/dist/generated/adaptive-admission.core3.wasm +0 -0
- package/dist/generated/adaptive-admission.d.ts +57 -0
- package/dist/generated/adaptive-admission.js +5869 -0
- package/dist/generated/interfaces/jfrz38-adaptive-admission-controller.d.ts +258 -0
- package/dist/index.d.ts +4 -0
- package/dist/index.js +3 -0
- package/dist/priority.d.ts +12 -0
- package/dist/priority.js +30 -0
- package/dist/types.d.ts +76 -0
- package/dist/types.js +1 -0
- package/package.json +56 -4
|
@@ -0,0 +1,81 @@
|
|
|
1
|
+
import { componentErrorPayload, createComponentController, defaultComponentConfig, } from './component-runtime.js';
|
|
2
|
+
import { ConfigurationError } from './errors.js';
|
|
3
|
+
const MAX_U64 = (1n << 64n) - 1n;
|
|
4
|
+
function toU64(field, value) {
|
|
5
|
+
let converted;
|
|
6
|
+
if (typeof value === 'number') {
|
|
7
|
+
if (!Number.isSafeInteger(value)) {
|
|
8
|
+
throw new RangeError(`${field} must be a safe integer or bigint`);
|
|
9
|
+
}
|
|
10
|
+
converted = BigInt(value);
|
|
11
|
+
}
|
|
12
|
+
else {
|
|
13
|
+
converted = value;
|
|
14
|
+
}
|
|
15
|
+
if (converted < 0n || converted > MAX_U64) {
|
|
16
|
+
throw new RangeError(`${field} must be between 0 and 2^64 - 1`);
|
|
17
|
+
}
|
|
18
|
+
return converted;
|
|
19
|
+
}
|
|
20
|
+
export function resolveConfiguration(options = {}) {
|
|
21
|
+
const defaults = defaultComponentConfig();
|
|
22
|
+
return {
|
|
23
|
+
initialThreshold: options.initialThreshold ?? defaults.initialThreshold,
|
|
24
|
+
maxConcurrentRequests: options.maxConcurrentRequests === undefined
|
|
25
|
+
? defaults.maxConcurrentRequests
|
|
26
|
+
: toU64('maxConcurrentRequests', options.maxConcurrentRequests),
|
|
27
|
+
maxQueuedRequests: options.maxQueuedRequests === undefined
|
|
28
|
+
? defaults.maxQueuedRequests
|
|
29
|
+
: toU64('maxQueuedRequests', options.maxQueuedRequests),
|
|
30
|
+
queueTimeoutMs: options.queueTimeoutMs === undefined
|
|
31
|
+
? defaults.queueTimeoutMs
|
|
32
|
+
: toU64('queueTimeoutMs', options.queueTimeoutMs),
|
|
33
|
+
queueTimeoutRatio: options.queueTimeoutRatio ?? defaults.queueTimeoutRatio,
|
|
34
|
+
statistics: {
|
|
35
|
+
minWindowMs: options.statistics?.minWindowMs === undefined
|
|
36
|
+
? defaults.statistics.minWindowMs
|
|
37
|
+
: toU64('statistics.minWindowMs', options.statistics.minWindowMs),
|
|
38
|
+
maxWindowMs: options.statistics?.maxWindowMs === undefined
|
|
39
|
+
? defaults.statistics.maxWindowMs
|
|
40
|
+
: toU64('statistics.maxWindowMs', options.statistics.maxWindowMs),
|
|
41
|
+
minSamples: options.statistics?.minSamples === undefined
|
|
42
|
+
? defaults.statistics.minSamples
|
|
43
|
+
: toU64('statistics.minSamples', options.statistics.minSamples),
|
|
44
|
+
minPercentileSamples: options.statistics?.minPercentileSamples === undefined
|
|
45
|
+
? defaults.statistics.minPercentileSamples
|
|
46
|
+
: toU64('statistics.minPercentileSamples', options.statistics.minPercentileSamples),
|
|
47
|
+
maxSamples: options.statistics?.maxSamples === undefined
|
|
48
|
+
? defaults.statistics.maxSamples
|
|
49
|
+
: toU64('statistics.maxSamples', options.statistics.maxSamples),
|
|
50
|
+
latencyPercentile: options.statistics?.latencyPercentile ?? defaults.statistics.latencyPercentile,
|
|
51
|
+
},
|
|
52
|
+
pid: {
|
|
53
|
+
proportionalGain: options.pid?.proportionalGain ?? defaults.pid.proportionalGain,
|
|
54
|
+
integralGain: options.pid?.integralGain ?? defaults.pid.integralGain,
|
|
55
|
+
derivativeGain: options.pid?.derivativeGain ?? defaults.pid.derivativeGain,
|
|
56
|
+
intervalMs: options.pid?.intervalMs === undefined
|
|
57
|
+
? defaults.pid.intervalMs
|
|
58
|
+
: toU64('pid.intervalMs', options.pid.intervalMs),
|
|
59
|
+
maxDeltaPercentage: options.pid?.maxDeltaPercentage ?? defaults.pid.maxDeltaPercentage,
|
|
60
|
+
integralDecay: options.pid?.integralDecay ?? defaults.pid.integralDecay,
|
|
61
|
+
},
|
|
62
|
+
};
|
|
63
|
+
}
|
|
64
|
+
export function createValidatedComponentController(options = {}) {
|
|
65
|
+
try {
|
|
66
|
+
return createComponentController(resolveConfiguration(options));
|
|
67
|
+
}
|
|
68
|
+
catch (error) {
|
|
69
|
+
const payload = componentErrorPayload(error);
|
|
70
|
+
if (isErrorPayload(payload)) {
|
|
71
|
+
throw new ConfigurationError(payload);
|
|
72
|
+
}
|
|
73
|
+
throw error;
|
|
74
|
+
}
|
|
75
|
+
}
|
|
76
|
+
function isErrorPayload(payload) {
|
|
77
|
+
return typeof payload === 'object'
|
|
78
|
+
&& payload !== null
|
|
79
|
+
&& 'tag' in payload
|
|
80
|
+
&& typeof payload.tag === 'string';
|
|
81
|
+
}
|
|
@@ -0,0 +1,48 @@
|
|
|
1
|
+
import { Priority } from './priority.js';
|
|
2
|
+
import type { AcquireOptions, AdmissionPermit, ControllerDiagnosticListener, ControllerOptions, RunOptions, StatisticsSnapshot } from './types.js';
|
|
3
|
+
export interface MonotonicClock {
|
|
4
|
+
nowMilliseconds(): bigint;
|
|
5
|
+
}
|
|
6
|
+
export interface DeadlineScheduler {
|
|
7
|
+
schedule(callback: () => void, delayMilliseconds: number): unknown;
|
|
8
|
+
cancel(handle: unknown): void;
|
|
9
|
+
}
|
|
10
|
+
export interface ControllerDependencies {
|
|
11
|
+
clock: MonotonicClock;
|
|
12
|
+
scheduler: DeadlineScheduler;
|
|
13
|
+
defer(callback: () => void): void;
|
|
14
|
+
}
|
|
15
|
+
export declare class AdaptiveAdmissionController {
|
|
16
|
+
private readonly dependencies;
|
|
17
|
+
private readonly component;
|
|
18
|
+
private readonly pending;
|
|
19
|
+
private timer;
|
|
20
|
+
private timerGeneration;
|
|
21
|
+
private shutdownRequested;
|
|
22
|
+
private readonly diagnosticListeners;
|
|
23
|
+
private latestPublishedStatisticsId;
|
|
24
|
+
private latestControllerDiagnostics;
|
|
25
|
+
constructor(options?: ControllerOptions, dependencies?: ControllerDependencies);
|
|
26
|
+
run<T>(task: () => T | Promise<T>, priority?: Priority, options?: RunOptions): Promise<T>;
|
|
27
|
+
acquire(priority?: Priority, options?: AcquireOptions): Promise<AdmissionPermit>;
|
|
28
|
+
private runWithPermit;
|
|
29
|
+
shutdown(): void;
|
|
30
|
+
latestStatistics(): StatisticsSnapshot | undefined;
|
|
31
|
+
priorityPercentile(percentile: number): number | undefined;
|
|
32
|
+
onDiagnostic(listener: ControllerDiagnosticListener): () => void;
|
|
33
|
+
private processActions;
|
|
34
|
+
private publishControllerDiagnostics;
|
|
35
|
+
private publishDiagnostic;
|
|
36
|
+
private start;
|
|
37
|
+
private createPermit;
|
|
38
|
+
private complete;
|
|
39
|
+
private reject;
|
|
40
|
+
private settleRejected;
|
|
41
|
+
private observeAbort;
|
|
42
|
+
private cancelQueued;
|
|
43
|
+
private removeAbortListener;
|
|
44
|
+
private rescheduleDeadline;
|
|
45
|
+
private clearTimer;
|
|
46
|
+
private now;
|
|
47
|
+
private translateProtocolError;
|
|
48
|
+
}
|
|
@@ -0,0 +1,330 @@
|
|
|
1
|
+
import { componentErrorPayload, } from './component-runtime.js';
|
|
2
|
+
import { createValidatedComponentController } from './configuration.js';
|
|
3
|
+
import { CancelledRequestError, ControllerProtocolError, ControllerShutdownError, EvictedRequestError, QueueFullError, ThresholdRejectedError, } from './errors.js';
|
|
4
|
+
import { Priority } from './priority.js';
|
|
5
|
+
const systemDependencies = {
|
|
6
|
+
clock: {
|
|
7
|
+
nowMilliseconds: () => process.hrtime.bigint() / 1000000n,
|
|
8
|
+
},
|
|
9
|
+
scheduler: {
|
|
10
|
+
schedule: (callback, delayMilliseconds) => setTimeout(callback, delayMilliseconds),
|
|
11
|
+
cancel: (handle) => clearTimeout(handle),
|
|
12
|
+
},
|
|
13
|
+
defer: queueMicrotask,
|
|
14
|
+
};
|
|
15
|
+
const MAX_TIMER_DELAY_MS = 2147483647n;
|
|
16
|
+
export class AdaptiveAdmissionController {
|
|
17
|
+
dependencies;
|
|
18
|
+
component;
|
|
19
|
+
pending = new Map();
|
|
20
|
+
timer;
|
|
21
|
+
timerGeneration = 0;
|
|
22
|
+
shutdownRequested = false;
|
|
23
|
+
diagnosticListeners = new Set();
|
|
24
|
+
latestPublishedStatisticsId;
|
|
25
|
+
latestControllerDiagnostics;
|
|
26
|
+
constructor(options = {}, dependencies = systemDependencies) {
|
|
27
|
+
this.dependencies = dependencies;
|
|
28
|
+
this.component = createValidatedComponentController(options);
|
|
29
|
+
}
|
|
30
|
+
run(task, priority = Priority.default(), options = {}) {
|
|
31
|
+
return this.runWithPermit(task, priority, options);
|
|
32
|
+
}
|
|
33
|
+
acquire(priority = Priority.default(), options = {}) {
|
|
34
|
+
if (this.shutdownRequested) {
|
|
35
|
+
return Promise.reject(new ControllerShutdownError());
|
|
36
|
+
}
|
|
37
|
+
if (options.signal?.aborted) {
|
|
38
|
+
return Promise.reject(new CancelledRequestError(undefined, 'requested'));
|
|
39
|
+
}
|
|
40
|
+
let resolve;
|
|
41
|
+
let reject;
|
|
42
|
+
const promise = new Promise((resolvePromise, rejectPromise) => {
|
|
43
|
+
resolve = resolvePromise;
|
|
44
|
+
reject = rejectPromise;
|
|
45
|
+
});
|
|
46
|
+
try {
|
|
47
|
+
const submission = this.component.submit(priority.value, this.now());
|
|
48
|
+
this.pending.set(submission.requestId, { resolve, reject, state: 'queued' });
|
|
49
|
+
this.processActions(submission.actions);
|
|
50
|
+
this.observeAbort(submission.requestId, options.signal);
|
|
51
|
+
this.rescheduleDeadline();
|
|
52
|
+
}
|
|
53
|
+
catch (error) {
|
|
54
|
+
reject(this.translateProtocolError(error));
|
|
55
|
+
}
|
|
56
|
+
return promise;
|
|
57
|
+
}
|
|
58
|
+
async runWithPermit(task, priority, options) {
|
|
59
|
+
const permit = await this.acquire(priority, options);
|
|
60
|
+
let value;
|
|
61
|
+
try {
|
|
62
|
+
value = await task();
|
|
63
|
+
}
|
|
64
|
+
catch (taskError) {
|
|
65
|
+
try {
|
|
66
|
+
permit.complete('failure');
|
|
67
|
+
}
|
|
68
|
+
catch {
|
|
69
|
+
// Preserve the task's original failure after the work has already run.
|
|
70
|
+
}
|
|
71
|
+
throw taskError;
|
|
72
|
+
}
|
|
73
|
+
permit.complete('success');
|
|
74
|
+
return value;
|
|
75
|
+
}
|
|
76
|
+
shutdown() {
|
|
77
|
+
if (this.shutdownRequested) {
|
|
78
|
+
return;
|
|
79
|
+
}
|
|
80
|
+
this.shutdownRequested = true;
|
|
81
|
+
try {
|
|
82
|
+
this.processActions(this.component.shutdown(this.now()));
|
|
83
|
+
this.rescheduleDeadline();
|
|
84
|
+
}
|
|
85
|
+
catch (error) {
|
|
86
|
+
this.clearTimer();
|
|
87
|
+
throw this.translateProtocolError(error);
|
|
88
|
+
}
|
|
89
|
+
}
|
|
90
|
+
latestStatistics() {
|
|
91
|
+
return this.component.latestStatistics();
|
|
92
|
+
}
|
|
93
|
+
priorityPercentile(percentile) {
|
|
94
|
+
if (!Number.isInteger(percentile) || percentile < 0 || percentile > 100) {
|
|
95
|
+
throw new RangeError('percentile must be an integer between 0 and 100');
|
|
96
|
+
}
|
|
97
|
+
return this.component.priorityPercentile(percentile);
|
|
98
|
+
}
|
|
99
|
+
onDiagnostic(listener) {
|
|
100
|
+
this.diagnosticListeners.add(listener);
|
|
101
|
+
return () => this.diagnosticListeners.delete(listener);
|
|
102
|
+
}
|
|
103
|
+
processActions(actions) {
|
|
104
|
+
const diagnostics = [];
|
|
105
|
+
for (const action of actions) {
|
|
106
|
+
switch (action.tag) {
|
|
107
|
+
case 'start':
|
|
108
|
+
this.start(action.val);
|
|
109
|
+
break;
|
|
110
|
+
case 'reject':
|
|
111
|
+
this.reject(action.val.requestId, action.val.reason);
|
|
112
|
+
break;
|
|
113
|
+
case 'evict':
|
|
114
|
+
this.settleRejected(action.val.requestId, new EvictedRequestError(action.val.requestId));
|
|
115
|
+
break;
|
|
116
|
+
case 'cancel':
|
|
117
|
+
this.settleRejected(action.val.requestId, action.val.reason === 'shutdown'
|
|
118
|
+
? new ControllerShutdownError(action.val.requestId)
|
|
119
|
+
: new CancelledRequestError(action.val.requestId, action.val.reason));
|
|
120
|
+
break;
|
|
121
|
+
case 'threshold-changed':
|
|
122
|
+
diagnostics.push({
|
|
123
|
+
type: 'threshold-changed',
|
|
124
|
+
previous: action.val.previous,
|
|
125
|
+
current: action.val.current,
|
|
126
|
+
});
|
|
127
|
+
break;
|
|
128
|
+
case 'concurrency-changed':
|
|
129
|
+
diagnostics.push({
|
|
130
|
+
type: 'concurrency-changed',
|
|
131
|
+
previous: action.val.previous,
|
|
132
|
+
current: action.val.current,
|
|
133
|
+
});
|
|
134
|
+
break;
|
|
135
|
+
}
|
|
136
|
+
}
|
|
137
|
+
const statistics = this.component.latestStatistics();
|
|
138
|
+
if (statistics !== undefined && statistics.id !== this.latestPublishedStatisticsId) {
|
|
139
|
+
this.latestPublishedStatisticsId = statistics.id;
|
|
140
|
+
diagnostics.push({ type: 'statistics-published', snapshot: statistics });
|
|
141
|
+
}
|
|
142
|
+
for (const diagnostic of diagnostics) {
|
|
143
|
+
this.publishDiagnostic(diagnostic);
|
|
144
|
+
}
|
|
145
|
+
this.publishControllerDiagnostics();
|
|
146
|
+
}
|
|
147
|
+
publishControllerDiagnostics() {
|
|
148
|
+
const componentDiagnostics = this.component.latestDiagnostics();
|
|
149
|
+
const diagnostics = {
|
|
150
|
+
threshold: componentDiagnostics.threshold,
|
|
151
|
+
concurrencyLimit: componentDiagnostics.concurrencyLimit,
|
|
152
|
+
queueTimeoutMs: componentDiagnostics.queueTimeoutMs,
|
|
153
|
+
targetLatencyMs: componentDiagnostics.targetLatencyMs,
|
|
154
|
+
aggregatedLatencyMs: componentDiagnostics.aggregatedLatencyMs,
|
|
155
|
+
};
|
|
156
|
+
if (sameDiagnostics(this.latestControllerDiagnostics, diagnostics)) {
|
|
157
|
+
return;
|
|
158
|
+
}
|
|
159
|
+
this.latestControllerDiagnostics = diagnostics;
|
|
160
|
+
this.publishDiagnostic({ type: 'controller-diagnostics', diagnostics });
|
|
161
|
+
}
|
|
162
|
+
publishDiagnostic(diagnostic) {
|
|
163
|
+
for (const listener of this.diagnosticListeners) {
|
|
164
|
+
try {
|
|
165
|
+
listener(diagnostic);
|
|
166
|
+
}
|
|
167
|
+
catch {
|
|
168
|
+
// Diagnostic consumers must not change admission or task outcomes.
|
|
169
|
+
}
|
|
170
|
+
}
|
|
171
|
+
}
|
|
172
|
+
start(requestId) {
|
|
173
|
+
const pending = this.pending.get(requestId);
|
|
174
|
+
if (!pending || pending.state !== 'queued') {
|
|
175
|
+
throw new Error(`component started unknown or non-queued request ${requestId}`);
|
|
176
|
+
}
|
|
177
|
+
pending.state = 'started';
|
|
178
|
+
this.removeAbortListener(pending);
|
|
179
|
+
const permit = this.createPermit(requestId);
|
|
180
|
+
this.dependencies.defer(() => pending.resolve(permit));
|
|
181
|
+
}
|
|
182
|
+
createPermit(requestId) {
|
|
183
|
+
let completed = false;
|
|
184
|
+
return {
|
|
185
|
+
complete: (outcome) => {
|
|
186
|
+
if (completed) {
|
|
187
|
+
throw new Error('Admission permit has already been completed');
|
|
188
|
+
}
|
|
189
|
+
completed = true;
|
|
190
|
+
this.complete(requestId, outcome);
|
|
191
|
+
},
|
|
192
|
+
};
|
|
193
|
+
}
|
|
194
|
+
complete(requestId, outcome) {
|
|
195
|
+
const pending = this.pending.get(requestId);
|
|
196
|
+
if (!pending || pending.state !== 'started') {
|
|
197
|
+
throw new Error(`cannot complete unknown or non-started request ${requestId}`);
|
|
198
|
+
}
|
|
199
|
+
this.pending.delete(requestId);
|
|
200
|
+
try {
|
|
201
|
+
this.processActions(this.component.complete(requestId, outcome, this.now()));
|
|
202
|
+
}
|
|
203
|
+
catch (error) {
|
|
204
|
+
throw this.translateProtocolError(error);
|
|
205
|
+
}
|
|
206
|
+
finally {
|
|
207
|
+
this.rescheduleDeadline();
|
|
208
|
+
}
|
|
209
|
+
}
|
|
210
|
+
reject(requestId, reason) {
|
|
211
|
+
switch (reason.tag) {
|
|
212
|
+
case 'threshold':
|
|
213
|
+
this.settleRejected(requestId, new ThresholdRejectedError(requestId, reason.val.priority, reason.val.threshold));
|
|
214
|
+
break;
|
|
215
|
+
case 'queue-full':
|
|
216
|
+
this.settleRejected(requestId, new QueueFullError(requestId));
|
|
217
|
+
break;
|
|
218
|
+
case 'shutdown':
|
|
219
|
+
this.settleRejected(requestId, new ControllerShutdownError(requestId));
|
|
220
|
+
break;
|
|
221
|
+
}
|
|
222
|
+
}
|
|
223
|
+
settleRejected(requestId, error) {
|
|
224
|
+
const pending = this.pending.get(requestId);
|
|
225
|
+
if (!pending) {
|
|
226
|
+
throw new Error(`component settled unknown request ${requestId}`);
|
|
227
|
+
}
|
|
228
|
+
this.pending.delete(requestId);
|
|
229
|
+
this.removeAbortListener(pending);
|
|
230
|
+
pending.reject(error);
|
|
231
|
+
}
|
|
232
|
+
observeAbort(requestId, signal) {
|
|
233
|
+
const pending = this.pending.get(requestId);
|
|
234
|
+
if (!signal || !pending || pending.state !== 'queued') {
|
|
235
|
+
return;
|
|
236
|
+
}
|
|
237
|
+
const listener = () => this.cancelQueued(requestId);
|
|
238
|
+
pending.abort = { signal, listener };
|
|
239
|
+
signal.addEventListener('abort', listener, { once: true });
|
|
240
|
+
if (signal.aborted) {
|
|
241
|
+
listener();
|
|
242
|
+
}
|
|
243
|
+
}
|
|
244
|
+
cancelQueued(requestId) {
|
|
245
|
+
const pending = this.pending.get(requestId);
|
|
246
|
+
if (!pending || pending.state !== 'queued') {
|
|
247
|
+
return;
|
|
248
|
+
}
|
|
249
|
+
try {
|
|
250
|
+
this.processActions(this.component.cancel(requestId, this.now()));
|
|
251
|
+
}
|
|
252
|
+
catch (error) {
|
|
253
|
+
this.pending.delete(requestId);
|
|
254
|
+
this.removeAbortListener(pending);
|
|
255
|
+
pending.reject(this.translateProtocolError(error));
|
|
256
|
+
}
|
|
257
|
+
finally {
|
|
258
|
+
this.rescheduleDeadline();
|
|
259
|
+
}
|
|
260
|
+
}
|
|
261
|
+
removeAbortListener(pending) {
|
|
262
|
+
if (pending.abort) {
|
|
263
|
+
pending.abort.signal.removeEventListener('abort', pending.abort.listener);
|
|
264
|
+
pending.abort = undefined;
|
|
265
|
+
}
|
|
266
|
+
}
|
|
267
|
+
rescheduleDeadline() {
|
|
268
|
+
this.clearTimer();
|
|
269
|
+
const deadline = this.component.nextDeadline();
|
|
270
|
+
if (deadline === undefined) {
|
|
271
|
+
return;
|
|
272
|
+
}
|
|
273
|
+
const generation = ++this.timerGeneration;
|
|
274
|
+
const remaining = deadline > this.now() ? deadline - this.now() : 0n;
|
|
275
|
+
const boundedDelay = remaining > MAX_TIMER_DELAY_MS ? MAX_TIMER_DELAY_MS : remaining;
|
|
276
|
+
this.timer = this.dependencies.scheduler.schedule(() => {
|
|
277
|
+
if (generation !== this.timerGeneration) {
|
|
278
|
+
return;
|
|
279
|
+
}
|
|
280
|
+
this.timer = undefined;
|
|
281
|
+
try {
|
|
282
|
+
this.processActions(this.component.advanceTime(this.now()));
|
|
283
|
+
}
|
|
284
|
+
finally {
|
|
285
|
+
this.rescheduleDeadline();
|
|
286
|
+
}
|
|
287
|
+
}, Number(boundedDelay));
|
|
288
|
+
}
|
|
289
|
+
clearTimer() {
|
|
290
|
+
this.timerGeneration += 1;
|
|
291
|
+
if (this.timer !== undefined) {
|
|
292
|
+
this.dependencies.scheduler.cancel(this.timer);
|
|
293
|
+
this.timer = undefined;
|
|
294
|
+
}
|
|
295
|
+
}
|
|
296
|
+
now() {
|
|
297
|
+
return this.dependencies.clock.nowMilliseconds();
|
|
298
|
+
}
|
|
299
|
+
translateProtocolError(error) {
|
|
300
|
+
const payload = componentErrorPayload(error);
|
|
301
|
+
if (isSubmitError(payload) && payload.tag === 'controller') {
|
|
302
|
+
return new ControllerProtocolError(payload.val);
|
|
303
|
+
}
|
|
304
|
+
if (isControllerError(payload)) {
|
|
305
|
+
return new ControllerProtocolError(payload);
|
|
306
|
+
}
|
|
307
|
+
return error;
|
|
308
|
+
}
|
|
309
|
+
}
|
|
310
|
+
function isControllerError(payload) {
|
|
311
|
+
return typeof payload === 'object'
|
|
312
|
+
&& payload !== null
|
|
313
|
+
&& 'tag' in payload
|
|
314
|
+
&& typeof payload.tag === 'string';
|
|
315
|
+
}
|
|
316
|
+
function isSubmitError(payload) {
|
|
317
|
+
return typeof payload === 'object'
|
|
318
|
+
&& payload !== null
|
|
319
|
+
&& 'tag' in payload
|
|
320
|
+
&& payload.tag === 'controller'
|
|
321
|
+
&& 'val' in payload;
|
|
322
|
+
}
|
|
323
|
+
function sameDiagnostics(previous, current) {
|
|
324
|
+
return previous !== undefined
|
|
325
|
+
&& previous.threshold === current.threshold
|
|
326
|
+
&& previous.concurrencyLimit === current.concurrencyLimit
|
|
327
|
+
&& previous.queueTimeoutMs === current.queueTimeoutMs
|
|
328
|
+
&& previous.targetLatencyMs === current.targetLatencyMs
|
|
329
|
+
&& previous.aggregatedLatencyMs === current.aggregatedLatencyMs;
|
|
330
|
+
}
|
package/dist/errors.d.ts
ADDED
|
@@ -0,0 +1,38 @@
|
|
|
1
|
+
import type { ConfigurationErrorPayload, ControllerErrorPayload } from './types.js';
|
|
2
|
+
export declare class AdaptiveAdmissionError extends Error {
|
|
3
|
+
constructor(message: string);
|
|
4
|
+
}
|
|
5
|
+
export declare class AdmissionRequestError extends AdaptiveAdmissionError {
|
|
6
|
+
}
|
|
7
|
+
export declare class ConfigurationError extends AdaptiveAdmissionError {
|
|
8
|
+
readonly payload: ConfigurationErrorPayload;
|
|
9
|
+
constructor(payload: ConfigurationErrorPayload);
|
|
10
|
+
}
|
|
11
|
+
export declare class ControllerProtocolError extends AdaptiveAdmissionError {
|
|
12
|
+
readonly payload: ControllerErrorPayload;
|
|
13
|
+
constructor(payload: ControllerErrorPayload);
|
|
14
|
+
}
|
|
15
|
+
export declare class RejectedRequestError extends AdmissionRequestError {
|
|
16
|
+
readonly requestId: bigint | undefined;
|
|
17
|
+
constructor(requestId: bigint | undefined, message: string);
|
|
18
|
+
}
|
|
19
|
+
export declare class ThresholdRejectedError extends RejectedRequestError {
|
|
20
|
+
readonly priority: number;
|
|
21
|
+
readonly threshold: number;
|
|
22
|
+
constructor(requestId: bigint, priority: number, threshold: number);
|
|
23
|
+
}
|
|
24
|
+
export declare class QueueFullError extends RejectedRequestError {
|
|
25
|
+
constructor(requestId: bigint);
|
|
26
|
+
}
|
|
27
|
+
export declare class ControllerShutdownError extends RejectedRequestError {
|
|
28
|
+
constructor(requestId?: bigint);
|
|
29
|
+
}
|
|
30
|
+
export declare class EvictedRequestError extends AdmissionRequestError {
|
|
31
|
+
readonly requestId: bigint;
|
|
32
|
+
constructor(requestId: bigint);
|
|
33
|
+
}
|
|
34
|
+
export declare class CancelledRequestError extends AdmissionRequestError {
|
|
35
|
+
readonly requestId: bigint | undefined;
|
|
36
|
+
readonly reason: 'requested' | 'shutdown';
|
|
37
|
+
constructor(requestId: bigint | undefined, reason: 'requested' | 'shutdown');
|
|
38
|
+
}
|
package/dist/errors.js
ADDED
|
@@ -0,0 +1,68 @@
|
|
|
1
|
+
export class AdaptiveAdmissionError extends Error {
|
|
2
|
+
constructor(message) {
|
|
3
|
+
super(message);
|
|
4
|
+
this.name = new.target.name;
|
|
5
|
+
}
|
|
6
|
+
}
|
|
7
|
+
export class AdmissionRequestError extends AdaptiveAdmissionError {
|
|
8
|
+
}
|
|
9
|
+
export class ConfigurationError extends AdaptiveAdmissionError {
|
|
10
|
+
payload;
|
|
11
|
+
constructor(payload) {
|
|
12
|
+
super(`Invalid adaptive admission configuration: ${payload.tag}`);
|
|
13
|
+
this.payload = payload;
|
|
14
|
+
}
|
|
15
|
+
}
|
|
16
|
+
export class ControllerProtocolError extends AdaptiveAdmissionError {
|
|
17
|
+
payload;
|
|
18
|
+
constructor(payload) {
|
|
19
|
+
super(`Adaptive admission controller error: ${payload.tag}`);
|
|
20
|
+
this.payload = payload;
|
|
21
|
+
}
|
|
22
|
+
}
|
|
23
|
+
export class RejectedRequestError extends AdmissionRequestError {
|
|
24
|
+
requestId;
|
|
25
|
+
constructor(requestId, message) {
|
|
26
|
+
super(message);
|
|
27
|
+
this.requestId = requestId;
|
|
28
|
+
}
|
|
29
|
+
}
|
|
30
|
+
export class ThresholdRejectedError extends RejectedRequestError {
|
|
31
|
+
priority;
|
|
32
|
+
threshold;
|
|
33
|
+
constructor(requestId, priority, threshold) {
|
|
34
|
+
super(requestId, `Request ${requestId} was rejected because priority ${priority} is outside threshold ${threshold}`);
|
|
35
|
+
this.priority = priority;
|
|
36
|
+
this.threshold = threshold;
|
|
37
|
+
}
|
|
38
|
+
}
|
|
39
|
+
export class QueueFullError extends RejectedRequestError {
|
|
40
|
+
constructor(requestId) {
|
|
41
|
+
super(requestId, `Request ${requestId} was rejected because the admission queue is full`);
|
|
42
|
+
}
|
|
43
|
+
}
|
|
44
|
+
export class ControllerShutdownError extends RejectedRequestError {
|
|
45
|
+
constructor(requestId) {
|
|
46
|
+
super(requestId, requestId === undefined
|
|
47
|
+
? 'The controller is shut down'
|
|
48
|
+
: `Request ${requestId} was rejected because the controller is shut down`);
|
|
49
|
+
}
|
|
50
|
+
}
|
|
51
|
+
export class EvictedRequestError extends AdmissionRequestError {
|
|
52
|
+
requestId;
|
|
53
|
+
constructor(requestId) {
|
|
54
|
+
super(`Request ${requestId} was evicted after exceeding its queue deadline`);
|
|
55
|
+
this.requestId = requestId;
|
|
56
|
+
}
|
|
57
|
+
}
|
|
58
|
+
export class CancelledRequestError extends AdmissionRequestError {
|
|
59
|
+
requestId;
|
|
60
|
+
reason;
|
|
61
|
+
constructor(requestId, reason) {
|
|
62
|
+
super(requestId === undefined
|
|
63
|
+
? `Request was cancelled before submission: ${reason}`
|
|
64
|
+
: `Request ${requestId} was cancelled: ${reason}`);
|
|
65
|
+
this.requestId = requestId;
|
|
66
|
+
this.reason = reason;
|
|
67
|
+
}
|
|
68
|
+
}
|
|
Binary file
|
|
Binary file
|
|
Binary file
|
|
@@ -0,0 +1,57 @@
|
|
|
1
|
+
// world root:component/root
|
|
2
|
+
import type * as Jfrz38AdaptiveAdmissionController from './interfaces/jfrz38-adaptive-admission-controller.js'; // jfrz38:adaptive-admission/controller@0.1.0
|
|
3
|
+
export interface ImportObject {
|
|
4
|
+
}
|
|
5
|
+
export interface Root {
|
|
6
|
+
"jfrz38:adaptive-admission/controller@0.1.0": typeof Jfrz38AdaptiveAdmissionController,
|
|
7
|
+
controller: typeof Jfrz38AdaptiveAdmissionController,
|
|
8
|
+
}
|
|
9
|
+
|
|
10
|
+
/**
|
|
11
|
+
* Options accepted by `instantiate`.
|
|
12
|
+
*/
|
|
13
|
+
export interface InstantiateOptions {
|
|
14
|
+
/** Runtime overrides for this instance. */
|
|
15
|
+
shim?: {
|
|
16
|
+
/** Platform globals used instead of those on `globalThis`. */
|
|
17
|
+
globals?: {
|
|
18
|
+
/**
|
|
19
|
+
* The WebAssembly implementation used by this instance,
|
|
20
|
+
* including by the default `instantiateCore`. Defaults to
|
|
21
|
+
* `globalThis.WebAssembly`.
|
|
22
|
+
*/
|
|
23
|
+
WebAssembly?: typeof WebAssembly;
|
|
24
|
+
};
|
|
25
|
+
};
|
|
26
|
+
}
|
|
27
|
+
|
|
28
|
+
|
|
29
|
+
/**
|
|
30
|
+
* Instantiates this component with the provided imports and
|
|
31
|
+
* returns a map of all the exports of the component.
|
|
32
|
+
*
|
|
33
|
+
* This function is intended to be similar to the
|
|
34
|
+
* `WebAssembly.Instantiate` constructor. The second `imports`
|
|
35
|
+
* argument is the "import object" for wasm, except here it
|
|
36
|
+
* uses component-model-layer types instead of core wasm
|
|
37
|
+
* integers/numbers/etc.
|
|
38
|
+
*
|
|
39
|
+
* The first argument to this function, `getCoreModule`, is
|
|
40
|
+
* used to compile core wasm modules within the component.
|
|
41
|
+
* Components are composed of core wasm modules and this callback
|
|
42
|
+
* will be invoked per core wasm module. The caller of this
|
|
43
|
+
* function is responsible for reading the core wasm module
|
|
44
|
+
* identified by `path` and returning its compiled
|
|
45
|
+
* `WebAssembly.Module` object. This would use the
|
|
46
|
+
* `WebAssembly.Module` constructor on the web, for example.
|
|
47
|
+
*
|
|
48
|
+
* The optional `options` argument configures instantiation; see
|
|
49
|
+
* `InstantiateOptions`.
|
|
50
|
+
*/
|
|
51
|
+
export function instantiate(
|
|
52
|
+
getCoreModule: (path: string) => WebAssembly.Module,
|
|
53
|
+
imports: ImportObject,
|
|
54
|
+
instantiateCore?: (module: WebAssembly.Module, imports: Record<string, any>) => WebAssembly.Instance,
|
|
55
|
+
options?: InstantiateOptions
|
|
56
|
+
): Root;
|
|
57
|
+
|