@isoloop/protocol 0.0.0-next.62ef495 → 0.0.0-next.800569e
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/dist/index.d.ts +54 -3
- package/dist/index.js +77 -2
- package/package.json +1 -1
package/dist/index.d.ts
CHANGED
|
@@ -160,6 +160,8 @@ export type InitPayload = {
|
|
|
160
160
|
* subscriptions start from `committed` instead, so it replays what it missed.
|
|
161
161
|
*/
|
|
162
162
|
wake?: boolean;
|
|
163
|
+
/** `KernelConfig.bus.commitIntervalMs` (ADR 0016, decision 1). Default 100. */
|
|
164
|
+
commitIntervalMs?: number;
|
|
163
165
|
};
|
|
164
166
|
/** What a beat says about whether the isolate could be suspended (ADR 0001). */
|
|
165
167
|
export type IdleReport = {
|
|
@@ -172,6 +174,15 @@ export type IdleReport = {
|
|
|
172
174
|
ageMs: number;
|
|
173
175
|
stack?: string;
|
|
174
176
|
}[];
|
|
177
|
+
/**
|
|
178
|
+
* Milliseconds the worker spent running this isolate's code since the last
|
|
179
|
+
* beat (ADR 0017): what the runtime dispatched to it and the callbacks it
|
|
180
|
+
* registered. A pool's shared framework render is split equally among the
|
|
181
|
+
* members whose roots it touched. Promise continuations, other schedulers
|
|
182
|
+
* and the worker's shared work (garbage collection) aren't counted; they
|
|
183
|
+
* show only in `lagMs`. Absent from runtimes before it.
|
|
184
|
+
*/
|
|
185
|
+
busyMs?: number;
|
|
175
186
|
};
|
|
176
187
|
/** Isolate port: host → isolate. */
|
|
177
188
|
export type HostToIsolate = Envelope<"init", InitPayload> | Envelope<"event", SerializedEvent> | Envelope<"result", CallResult> | Envelope<"shutdown", {
|
|
@@ -247,9 +258,11 @@ export type IsolateToBus = Envelope<"publish", {
|
|
|
247
258
|
group?: string;
|
|
248
259
|
}> | Envelope<"unsubscribe", {
|
|
249
260
|
topic: string;
|
|
250
|
-
}> | Envelope<"
|
|
251
|
-
|
|
252
|
-
|
|
261
|
+
}> | Envelope<"commits", {
|
|
262
|
+
offsets: {
|
|
263
|
+
topic: string;
|
|
264
|
+
offset: number;
|
|
265
|
+
}[];
|
|
253
266
|
}> | Envelope<"request", {
|
|
254
267
|
topic: string;
|
|
255
268
|
key?: string;
|
|
@@ -276,6 +289,44 @@ export declare const ISOLATE_TO_BUS: MessageTypes<IsolateToBus>;
|
|
|
276
289
|
export declare const BUS_TO_ISOLATE: MessageTypes<BusToIsolate>;
|
|
277
290
|
export declare const isIsolateToBus: (x: unknown) => x is IsolateToBus;
|
|
278
291
|
export declare const isBusToIsolate: (x: unknown) => x is BusToIsolate;
|
|
292
|
+
/** `KernelConfig.bus.commitIntervalMs` when unset (ADR 0016, decision 1, point 7). */
|
|
293
|
+
export declare const DEFAULT_COMMIT_INTERVAL_MS = 100;
|
|
294
|
+
/** The highest allowed interval: keeps the redelivery window after a crash under a second. */
|
|
295
|
+
export declare const MAX_COMMIT_INTERVAL_MS = 1e3;
|
|
296
|
+
/** A subscription's credit at the broker: records delivered and not yet committed. */
|
|
297
|
+
export declare const DEFAULT_CREDIT = 1e3;
|
|
298
|
+
/** Why an interval is invalid, or `undefined` when it's an integer from 0 to 1,000. */
|
|
299
|
+
export declare function commitIntervalProblem(ms: unknown): string | undefined;
|
|
300
|
+
export type CommitOffsets = {
|
|
301
|
+
topic: string;
|
|
302
|
+
offset: number;
|
|
303
|
+
}[];
|
|
304
|
+
export type CommitterOptions = {
|
|
305
|
+
/** Sends one `commits` message. Called with at least one entry. */
|
|
306
|
+
send: (offsets: CommitOffsets) => void;
|
|
307
|
+
/** 0: at the end of each task that settled a record. Default 100. */
|
|
308
|
+
intervalMs?: number;
|
|
309
|
+
/** The broker's credit per subscription; a subscription sends early at half of it. Default 1,000. */
|
|
310
|
+
credit?: number;
|
|
311
|
+
};
|
|
312
|
+
export type Committer = {
|
|
313
|
+
/** `records` records of `topic`, up to `offset`, are settled. Default one. */
|
|
314
|
+
settle(topic: string, offset: number, records?: number): void;
|
|
315
|
+
/** Sends what's pending now: an unsubscribe, and a new port after a bus restart. */
|
|
316
|
+
flush(): void;
|
|
317
|
+
/**
|
|
318
|
+
* Returns what's pending and clears it, without sending: for a sender that
|
|
319
|
+
* must wait for the broker's answer (a shutdown, before `bye`).
|
|
320
|
+
*/
|
|
321
|
+
take(): CommitOffsets | undefined;
|
|
322
|
+
/** Forgets a topic's pending offset (an unsubscribe or a lost memory topic). */
|
|
323
|
+
drop(topic: string): void;
|
|
324
|
+
/** Settled offsets not yet sent, by topic. For tests and reports. */
|
|
325
|
+
pending(): ReadonlyMap<string, number>;
|
|
326
|
+
/** Stops the timer without sending. */
|
|
327
|
+
dispose(): void;
|
|
328
|
+
};
|
|
329
|
+
export declare function createCommitter(o: CommitterOptions): Committer;
|
|
279
330
|
/**
|
|
280
331
|
* A rejected bus call (`kernel.publish`, `ctx.bus.publish`, `request`,
|
|
281
332
|
* `reply`): the broker's error code, and the schema issues for `invalid`.
|
package/dist/index.js
CHANGED
|
@@ -108,7 +108,7 @@ const ISOLATE_TO_BUS = {
|
|
|
108
108
|
publish: true,
|
|
109
109
|
subscribe: true,
|
|
110
110
|
unsubscribe: true,
|
|
111
|
-
|
|
111
|
+
commits: true,
|
|
112
112
|
request: true,
|
|
113
113
|
reply: true
|
|
114
114
|
};
|
|
@@ -119,6 +119,81 @@ const BUS_TO_ISOLATE = {
|
|
|
119
119
|
};
|
|
120
120
|
const isIsolateToBus = envelopeGuard(ISOLATE_TO_BUS);
|
|
121
121
|
const isBusToIsolate = envelopeGuard(BUS_TO_ISOLATE);
|
|
122
|
+
// src/commits.ts
|
|
123
|
+
/** `KernelConfig.bus.commitIntervalMs` when unset (ADR 0016, decision 1, point 7). */
|
|
124
|
+
const DEFAULT_COMMIT_INTERVAL_MS = 100;
|
|
125
|
+
/** The highest allowed interval: keeps the redelivery window after a crash under a second. */
|
|
126
|
+
const MAX_COMMIT_INTERVAL_MS = 1e3;
|
|
127
|
+
/** A subscription's credit at the broker: records delivered and not yet committed. */
|
|
128
|
+
const DEFAULT_CREDIT = 1e3;
|
|
129
|
+
/** Why an interval is invalid, or `undefined` when it's an integer from 0 to 1,000. */
|
|
130
|
+
function commitIntervalProblem(ms) {
|
|
131
|
+
if (typeof ms === "number" && Number.isInteger(ms) && ms >= 0 && ms <= 1e3) return void 0;
|
|
132
|
+
return `bus.commitIntervalMs must be an integer from 0 to ${MAX_COMMIT_INTERVAL_MS}, got ${String(ms)}`;
|
|
133
|
+
}
|
|
134
|
+
function createCommitter(o) {
|
|
135
|
+
const intervalMs = o.intervalMs ?? 100;
|
|
136
|
+
const half = Math.max(1, Math.floor((o.credit ?? 1e3) / 2));
|
|
137
|
+
/** Highest settled offset per topic, not yet sent. */
|
|
138
|
+
const offsets = /* @__PURE__ */ new Map();
|
|
139
|
+
/** Records settled per topic since its last send: the early send at half the credit. */
|
|
140
|
+
const counts = /* @__PURE__ */ new Map();
|
|
141
|
+
let timer;
|
|
142
|
+
let queued = false;
|
|
143
|
+
const take = () => {
|
|
144
|
+
if (timer !== void 0) {
|
|
145
|
+
clearTimeout(timer);
|
|
146
|
+
timer = void 0;
|
|
147
|
+
}
|
|
148
|
+
queued = false;
|
|
149
|
+
if (offsets.size === 0) return void 0;
|
|
150
|
+
const out = [];
|
|
151
|
+
for (const [topic, offset] of offsets) out.push({
|
|
152
|
+
topic,
|
|
153
|
+
offset
|
|
154
|
+
});
|
|
155
|
+
offsets.clear();
|
|
156
|
+
counts.clear();
|
|
157
|
+
return out;
|
|
158
|
+
};
|
|
159
|
+
const flush = () => {
|
|
160
|
+
const out = take();
|
|
161
|
+
if (out) o.send(out);
|
|
162
|
+
};
|
|
163
|
+
const schedule = () => {
|
|
164
|
+
if (intervalMs === 0) {
|
|
165
|
+
if (queued) return;
|
|
166
|
+
queued = true;
|
|
167
|
+
queueMicrotask(() => {
|
|
168
|
+
if (queued) flush();
|
|
169
|
+
});
|
|
170
|
+
return;
|
|
171
|
+
}
|
|
172
|
+
timer ??= setTimeout(flush, intervalMs);
|
|
173
|
+
};
|
|
174
|
+
return {
|
|
175
|
+
settle(topic, offset, records = 1) {
|
|
176
|
+
const prev = offsets.get(topic);
|
|
177
|
+
if (prev === void 0 || offset > prev) offsets.set(topic, offset);
|
|
178
|
+
const n = (counts.get(topic) ?? 0) + records;
|
|
179
|
+
counts.set(topic, n);
|
|
180
|
+
if (n >= half) flush();
|
|
181
|
+
else schedule();
|
|
182
|
+
},
|
|
183
|
+
flush,
|
|
184
|
+
take,
|
|
185
|
+
drop(topic) {
|
|
186
|
+
offsets.delete(topic);
|
|
187
|
+
counts.delete(topic);
|
|
188
|
+
},
|
|
189
|
+
pending: () => offsets,
|
|
190
|
+
dispose() {
|
|
191
|
+
if (timer !== void 0) clearTimeout(timer);
|
|
192
|
+
timer = void 0;
|
|
193
|
+
queued = false;
|
|
194
|
+
}
|
|
195
|
+
};
|
|
196
|
+
}
|
|
122
197
|
// src/bus-error.ts
|
|
123
198
|
/**
|
|
124
199
|
* A rejected bus call (`kernel.publish`, `ctx.bus.publish`, `request`,
|
|
@@ -253,4 +328,4 @@ function compareVersions(a, b) {
|
|
|
253
328
|
const [bm, bn] = parse(b);
|
|
254
329
|
return am - bm || an - bn;
|
|
255
330
|
}
|
|
256
|
-
export { BUS_TO_HOST, BUS_TO_ISOLATE, BusError, ERROR_CODES, HOST_TO_BUS, HOST_TO_ISOLATE, ISOLATE_TO_BUS, ISOLATE_TO_HOST, PROTOCOL, ROUTE_KEY, SYSTEM_TOPICS, SYSTEM_TOPIC_PREFIX, assertNever, checks, compareVersions, envelopeGuard, isBusToHost, isBusToIsolate, isEnvelope, isErrorCode, isHostToBus, isHostToIsolate, isIsolateToBus, isIsolateToHost, isSystemTopic, majorMinor, matchAny, matchTopic, routeTopic, systemSchema, systemTopic, topicConfigOf };
|
|
331
|
+
export { BUS_TO_HOST, BUS_TO_ISOLATE, BusError, DEFAULT_COMMIT_INTERVAL_MS, DEFAULT_CREDIT, ERROR_CODES, HOST_TO_BUS, HOST_TO_ISOLATE, ISOLATE_TO_BUS, ISOLATE_TO_HOST, MAX_COMMIT_INTERVAL_MS, PROTOCOL, ROUTE_KEY, SYSTEM_TOPICS, SYSTEM_TOPIC_PREFIX, assertNever, checks, commitIntervalProblem, compareVersions, createCommitter, envelopeGuard, isBusToHost, isBusToIsolate, isEnvelope, isErrorCode, isHostToBus, isHostToIsolate, isIsolateToBus, isIsolateToHost, isSystemTopic, majorMinor, matchAny, matchTopic, routeTopic, systemSchema, systemTopic, topicConfigOf };
|