@mark1russell7/service 0.6.2 → 0.6.4
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 +90 -4
- package/build/{builder-0OlfomVU.d.ts → builder-bXJqMe2p.d.ts} +3 -2
- package/build/{core-Iy9rIXOT.d.ts → core-B1zbS4WY.d.ts} +4 -3
- package/build/{dial-DLT7bTlA.d.ts → dial-CSGKVlZe.d.ts} +2 -2
- package/build/grpc.d.ts +3 -3
- package/build/grpc.js +1 -1
- package/build/host.d.ts +556 -0
- package/build/host.js +1738 -0
- package/build/host.js.map +1 -0
- package/build/http-client.d.ts +3 -3
- package/build/http-client.js +1 -1
- package/build/http.d.ts +1 -1
- package/build/index.d.ts +4 -3
- package/build/index.js +5 -749
- package/build/index.js.map +1 -1
- package/build/lifecycle-Ks9Yd5CM.d.ts +387 -0
- package/build/{lifecycle-DXK880vG.d.ts → logger-DhFgh48y.d.ts} +2 -385
- package/build/{machine-Dq2OlzCG.js → machine-DKWLZOqu.js} +3 -2
- package/build/{machine-Dq2OlzCG.js.map → machine-DKWLZOqu.js.map} +1 -1
- package/build/mongodb.d.ts +3 -3
- package/build/mongodb.js +1 -1
- package/build/operation-DL5RxFds.js +25 -0
- package/build/operation-DL5RxFds.js.map +1 -0
- package/build/pg.d.ts +2 -2
- package/build/pg.js +1 -1
- package/build/ports-CZLzYK5d.d.ts +125 -0
- package/build/process-DG_im2zc.js +171 -0
- package/build/process-DG_im2zc.js.map +1 -0
- package/build/{queue-CvBNNZZJ.d.ts → queue-BjiGodqh.d.ts} +3 -2
- package/build/rabbitmq.d.ts +4 -4
- package/build/rabbitmq.js +1 -1
- package/build/redis.d.ts +2 -2
- package/build/redis.js +1 -1
- package/build/requests-BIq9Tr4P.js +586 -0
- package/build/requests-BIq9Tr4P.js.map +1 -0
- package/build/{retrier-CRda6z9G.js → retrier-Bzkme_yM.js} +2 -24
- package/build/retrier-Bzkme_yM.js.map +1 -0
- package/build/testing.d.ts +136 -4
- package/build/testing.js +485 -13
- package/build/testing.js.map +1 -1
- package/build/websocket.d.ts +3 -3
- package/build/websocket.js +1 -1
- package/package.json +7 -2
- package/build/retrier-CRda6z9G.js.map +0 -1
package/build/index.js
CHANGED
|
@@ -1,13 +1,16 @@
|
|
|
1
1
|
import { platformClock, realClock } from "./clock.js";
|
|
2
2
|
import { detectionTime, heartbeat } from "./heartbeat.js";
|
|
3
3
|
import { a as recoverSpan, i as heartbeatDetection, n as asWindow, r as failSpan, t as asDebounce } from "./timing-DGRi3XCI.js";
|
|
4
|
-
import {
|
|
4
|
+
import { n as operationProblems, t as mayReissue } from "./operation-DL5RxFds.js";
|
|
5
5
|
import { a as limiterProblems, i as limitRule, n as freeAt, o as longestRetryAfter, r as idleSpan, s as retryAfterSeconds, t as decide } from "./policy-CgS4y94U.js";
|
|
6
6
|
import { a as lateHolds, l as takeOutside, n as countOf, r as millisecondsOf, s as ownStream, t as boundedMillisecondsOf } from "./knobs-D2ClI68I.js";
|
|
7
|
+
import { a as isExitCode, c as matchesMessage, i as embeddedProcess, l as nodeProcess, n as PROCESS_TRIGGERS, o as isExitSignal, r as UNCAUGHT_ORIGINS, s as isMessageShape, t as EXIT_SIGNALS, u as processPortOf } from "./process-DG_im2zc.js";
|
|
8
|
+
import { a as runUnit, c as untilAbort, i as retryOf, l as routePatternProblems, n as boundProblems, o as settleWithin, r as flights, s as sleeper, t as tracker } from "./requests-BIq9Tr4P.js";
|
|
7
9
|
import { backoffBand, backoffDelay, backoffProblems } from "./backoff.js";
|
|
8
10
|
import { DRAIN_RULES, O04, O09, O10, O16 } from "./rules.js";
|
|
11
|
+
import { t as retrier } from "./retrier-Bzkme_yM.js";
|
|
9
12
|
import { a as keyedLimiter, i as limiterStateOf, n as TOKEN_BUCKET_MACHINE, o as memoryStore, r as limiterMachine, t as SLIDING_WINDOW_MACHINE } from "./limiter-CmC4vCjk.js";
|
|
10
|
-
import { convertExact, convertible,
|
|
13
|
+
import { convertExact, convertible, rationals, valueIn } from "@mark1russell7/quantity";
|
|
11
14
|
import { replay } from "@mark1russell7/algebra/replay";
|
|
12
15
|
//#region src/description.ts
|
|
13
16
|
/**
|
|
@@ -778,753 +781,6 @@ const logEvents = (logger, description) => {
|
|
|
778
781
|
};
|
|
779
782
|
};
|
|
780
783
|
//#endregion
|
|
781
|
-
//#region src/process.ts
|
|
782
|
-
/** These are the signals that the runtime handles. */
|
|
783
|
-
const EXIT_SIGNALS = [
|
|
784
|
-
"SIGINT",
|
|
785
|
-
"SIGTERM",
|
|
786
|
-
"SIGQUIT"
|
|
787
|
-
];
|
|
788
|
-
/** These are the process triggers that a declaration can opt in to. */
|
|
789
|
-
const PROCESS_TRIGGERS = [
|
|
790
|
-
"stdin-close",
|
|
791
|
-
"ipc-close",
|
|
792
|
-
"ipc-message"
|
|
793
|
-
];
|
|
794
|
-
/** These are the origins of the errors that the option `uncaught` of `run()` receives. */
|
|
795
|
-
const UNCAUGHT_ORIGINS = ["uncaughtException", "unhandledRejection"];
|
|
796
|
-
/** This function tells if a number is an exit code of a process: a whole number from 0 to 255. */
|
|
797
|
-
const isExitCode = (code) => Number.isInteger(code) && code >= 0 && code <= 255;
|
|
798
|
-
/** This function tells if a trigger is an exit signal. A repeat signal obeys `onRepeatSignal`. */
|
|
799
|
-
const isExitSignal = (trigger) => EXIT_SIGNALS.includes(trigger);
|
|
800
|
-
/** This function tells if a value is a message shape: a value of JSON, with no function and no undefined. */
|
|
801
|
-
const isMessageShape = (value) => {
|
|
802
|
-
if (value === null || typeof value === "string" || typeof value === "boolean") return true;
|
|
803
|
-
if (typeof value === "number") return Number.isFinite(value);
|
|
804
|
-
if (Array.isArray(value)) return value.every(isMessageShape);
|
|
805
|
-
if (typeof value !== "object") return false;
|
|
806
|
-
const prototype = Object.getPrototypeOf(value);
|
|
807
|
-
return (prototype === Object.prototype || prototype === null) && Object.values(value).every(isMessageShape);
|
|
808
|
-
};
|
|
809
|
-
/** This function tells if a message of the IPC channel matches a shape. */
|
|
810
|
-
const matchesMessage = (shape, message) => {
|
|
811
|
-
if (shape === null || typeof shape !== "object") return Object.is(shape, message);
|
|
812
|
-
if (Array.isArray(shape)) {
|
|
813
|
-
const items = shape;
|
|
814
|
-
return Array.isArray(message) && message.length === items.length && items.every((item, at) => matchesMessage(item, message[at]));
|
|
815
|
-
}
|
|
816
|
-
if (message === null || typeof message !== "object" || Array.isArray(message)) return false;
|
|
817
|
-
const fields = message;
|
|
818
|
-
return Object.entries(shape).every(([field, value]) => Object.hasOwn(fields, field) && matchesMessage(value, fields[field]));
|
|
819
|
-
};
|
|
820
|
-
/** This time is the interval of the timer of a hold, the most that a timer of Node accepts. */
|
|
821
|
-
const HOLD_INTERVAL = 2147483647;
|
|
822
|
-
/** This function adds a handler to some events of an emitter. The handler operates one time at most. */
|
|
823
|
-
const once = (emitter, events, handler) => {
|
|
824
|
-
let called = false;
|
|
825
|
-
const fire = () => {
|
|
826
|
-
if (called) return;
|
|
827
|
-
called = true;
|
|
828
|
-
handler();
|
|
829
|
-
};
|
|
830
|
-
for (const event of events) emitter.on(event, fire);
|
|
831
|
-
return () => {
|
|
832
|
-
for (const event of events) emitter.off(event, fire);
|
|
833
|
-
};
|
|
834
|
-
};
|
|
835
|
-
/** This function gives a promise that ends when the data before it in a stream is written. */
|
|
836
|
-
const written = (stream) => new Promise((resolve) => {
|
|
837
|
-
if (stream === void 0 || stream.destroyed === true || stream.writable === false) {
|
|
838
|
-
resolve();
|
|
839
|
-
return;
|
|
840
|
-
}
|
|
841
|
-
try {
|
|
842
|
-
stream.write("", () => resolve());
|
|
843
|
-
} catch {
|
|
844
|
-
resolve();
|
|
845
|
-
}
|
|
846
|
-
});
|
|
847
|
-
/**
|
|
848
|
-
* This function makes a process port on a host. `find` gives the host at each use, or undefined
|
|
849
|
-
* when the platform has no process. Then the use throws.
|
|
850
|
-
*/
|
|
851
|
-
const processPortOf = (find) => {
|
|
852
|
-
const hostProcess = () => {
|
|
853
|
-
const host = find();
|
|
854
|
-
if (host === void 0) throw new Error("no process: give the runtime a process port");
|
|
855
|
-
return host;
|
|
856
|
-
};
|
|
857
|
-
return {
|
|
858
|
-
on(trigger, handler) {
|
|
859
|
-
const host = hostProcess();
|
|
860
|
-
if (trigger === "stdin-close") {
|
|
861
|
-
const input = host.stdin;
|
|
862
|
-
if (input === void 0) return () => {};
|
|
863
|
-
if (input.readableEnded === true) {
|
|
864
|
-
let live = true;
|
|
865
|
-
queueMicrotask(() => {
|
|
866
|
-
if (live) handler();
|
|
867
|
-
});
|
|
868
|
-
return () => {
|
|
869
|
-
live = false;
|
|
870
|
-
};
|
|
871
|
-
}
|
|
872
|
-
return once(input, ["end", "close"], handler);
|
|
873
|
-
}
|
|
874
|
-
if (trigger === "ipc-close") return once(host, ["disconnect"], handler);
|
|
875
|
-
if (trigger === "ipc-message") {
|
|
876
|
-
const receive = (message) => handler(message);
|
|
877
|
-
host.on("message", receive);
|
|
878
|
-
return () => {
|
|
879
|
-
host.off("message", receive);
|
|
880
|
-
};
|
|
881
|
-
}
|
|
882
|
-
host.on(trigger, handler);
|
|
883
|
-
return () => {
|
|
884
|
-
host.off(trigger, handler);
|
|
885
|
-
};
|
|
886
|
-
},
|
|
887
|
-
onUncaught(handler) {
|
|
888
|
-
const host = hostProcess();
|
|
889
|
-
const offs = UNCAUGHT_ORIGINS.map((origin) => {
|
|
890
|
-
const receive = (error) => handler(error, origin);
|
|
891
|
-
host.on(origin, receive);
|
|
892
|
-
return () => {
|
|
893
|
-
host.off(origin, receive);
|
|
894
|
-
};
|
|
895
|
-
});
|
|
896
|
-
return () => {
|
|
897
|
-
for (const off of offs) off();
|
|
898
|
-
};
|
|
899
|
-
},
|
|
900
|
-
async flush() {
|
|
901
|
-
const host = hostProcess();
|
|
902
|
-
await Promise.all([written(host.stdout), written(host.stderr)]);
|
|
903
|
-
},
|
|
904
|
-
hold() {
|
|
905
|
-
hostProcess();
|
|
906
|
-
const timer = setInterval(() => {}, HOLD_INTERVAL);
|
|
907
|
-
let held = true;
|
|
908
|
-
return () => {
|
|
909
|
-
if (!held) return;
|
|
910
|
-
held = false;
|
|
911
|
-
clearInterval(timer);
|
|
912
|
-
};
|
|
913
|
-
},
|
|
914
|
-
exit(code) {
|
|
915
|
-
hostProcess().exit(code);
|
|
916
|
-
}
|
|
917
|
-
};
|
|
918
|
-
};
|
|
919
|
-
/** This port is the process of Node. */
|
|
920
|
-
const nodeProcess = processPortOf(() => globalThis.process);
|
|
921
|
-
/**
|
|
922
|
-
* This function gives a process port for a lifecycle in a process that another part owns. That
|
|
923
|
-
* part is a script, a test of an application, or a library entry. The port adds no handler of a signal or of
|
|
924
|
-
* a trigger, it does not hold the process, and its `exit` only records the code. The caller stops
|
|
925
|
-
* the lifecycle with `life.shutdown()`. A process with a service of its own gives its parts to
|
|
926
|
-
* that service, and does not use this port.
|
|
927
|
-
*/
|
|
928
|
-
const embeddedProcess = () => {
|
|
929
|
-
let code = null;
|
|
930
|
-
let resolve = () => {};
|
|
931
|
-
return {
|
|
932
|
-
on() {
|
|
933
|
-
return () => {};
|
|
934
|
-
},
|
|
935
|
-
exit(requested) {
|
|
936
|
-
if (code !== null) return;
|
|
937
|
-
code = requested;
|
|
938
|
-
resolve(requested);
|
|
939
|
-
},
|
|
940
|
-
get exitCode() {
|
|
941
|
-
return code;
|
|
942
|
-
},
|
|
943
|
-
exited: new Promise((done) => {
|
|
944
|
-
resolve = done;
|
|
945
|
-
})
|
|
946
|
-
};
|
|
947
|
-
};
|
|
948
|
-
//#endregion
|
|
949
|
-
//#region src/routes.ts
|
|
950
|
-
const NAME = /^[A-Za-z_][A-Za-z0-9_]*/;
|
|
951
|
-
/** This function gives the problems of the syntax of a pattern: an empty pattern, a part without a name, or a name two times. */
|
|
952
|
-
const patternProblems = (owner, pattern) => {
|
|
953
|
-
const problems = [];
|
|
954
|
-
if (pattern.length === 0) problems.push(`${owner} has an empty route pattern`);
|
|
955
|
-
const names = /* @__PURE__ */ new Set();
|
|
956
|
-
for (let at = 0; at < pattern.length; at += 1) {
|
|
957
|
-
if (pattern[at] !== ":") continue;
|
|
958
|
-
const name = NAME.exec(pattern.slice(at + 1))?.[0];
|
|
959
|
-
if (name === void 0) {
|
|
960
|
-
problems.push(`${owner}'s pattern ${pattern} has a ":" without a name at ${at}`);
|
|
961
|
-
continue;
|
|
962
|
-
}
|
|
963
|
-
if (names.has(name)) problems.push(`${owner}'s pattern ${pattern} names :${name} two times`);
|
|
964
|
-
names.add(name);
|
|
965
|
-
at += name.length;
|
|
966
|
-
}
|
|
967
|
-
return problems;
|
|
968
|
-
};
|
|
969
|
-
/** This function gives the problems of a pattern and of each of its operations, which `describe()` refuses. */
|
|
970
|
-
const routePatternProblems = (owner, declared) => {
|
|
971
|
-
const problems = patternProblems(owner, declared.pattern);
|
|
972
|
-
const names = Object.keys(declared.operations);
|
|
973
|
-
if (names.length === 0) problems.push(`${owner}'s pattern ${declared.pattern} declares no operation`);
|
|
974
|
-
for (const name of names) problems.push(...operationProblems(owner, `${declared.pattern} (${name})`, declared.operations[name]));
|
|
975
|
-
return problems;
|
|
976
|
-
};
|
|
977
|
-
/** This function compiles a pattern to a matcher. The match must cover the whole route. */
|
|
978
|
-
const compilePattern = (pattern) => {
|
|
979
|
-
const kinds = [];
|
|
980
|
-
let source = "^";
|
|
981
|
-
for (let at = 0; at < pattern.length; at += 1) {
|
|
982
|
-
const char = pattern[at];
|
|
983
|
-
if (char === "*") {
|
|
984
|
-
source += "([\\s\\S]*)";
|
|
985
|
-
kinds.push({ kind: "wildcard" });
|
|
986
|
-
continue;
|
|
987
|
-
}
|
|
988
|
-
if (char === ":") {
|
|
989
|
-
const name = NAME.exec(pattern.slice(at + 1))?.[0];
|
|
990
|
-
if (name !== void 0) {
|
|
991
|
-
source += "([^/]+)";
|
|
992
|
-
kinds.push({
|
|
993
|
-
kind: "param",
|
|
994
|
-
name
|
|
995
|
-
});
|
|
996
|
-
at += name.length;
|
|
997
|
-
continue;
|
|
998
|
-
}
|
|
999
|
-
}
|
|
1000
|
-
source += char.replace(/[.*+?^${}()|[\]\\/]/g, "\\$&");
|
|
1001
|
-
}
|
|
1002
|
-
const matcher = new RegExp(`${source}$`);
|
|
1003
|
-
return {
|
|
1004
|
-
pattern,
|
|
1005
|
-
match(route) {
|
|
1006
|
-
const found = matcher.exec(route);
|
|
1007
|
-
if (found === null) return null;
|
|
1008
|
-
const params = {};
|
|
1009
|
-
const wildcards = [];
|
|
1010
|
-
kinds.forEach((part, index) => {
|
|
1011
|
-
const text = found[index + 1] ?? "";
|
|
1012
|
-
if (part.kind === "param") params[part.name] = text;
|
|
1013
|
-
else wildcards.push(text);
|
|
1014
|
-
});
|
|
1015
|
-
return {
|
|
1016
|
-
params,
|
|
1017
|
-
wildcards
|
|
1018
|
-
};
|
|
1019
|
-
}
|
|
1020
|
-
};
|
|
1021
|
-
};
|
|
1022
|
-
//#endregion
|
|
1023
|
-
//#region src/units.ts
|
|
1024
|
-
/** This function gives the cause of an abort as text. */
|
|
1025
|
-
const causeOf = (signal) => signal.reason instanceof Error ? signal.reason.message : String(signal.reason ?? "the unit got an abort");
|
|
1026
|
-
/**
|
|
1027
|
-
* This function does one unit of work, and gives its end. With a request, the unit goes through
|
|
1028
|
-
* `life.request`, thus the service counts it, drains it and can cut it. A throw after the abort of
|
|
1029
|
-
* the transport is a cut, not a failure (S6 A10). A throw after the bound is a failure.
|
|
1030
|
-
*/
|
|
1031
|
-
const runUnit = async (run) => {
|
|
1032
|
-
const { clock, bound, abort, life, request, unit, work } = run;
|
|
1033
|
-
const timeout = new AbortController();
|
|
1034
|
-
const cancel = Number.isFinite(bound) ? clock.after(bound, () => timeout.abort(/* @__PURE__ */ new Error(`the unit did not end in its unit_processing_time of ${bound} ms`))) : () => {};
|
|
1035
|
-
const call = async (tracker) => work(AbortSignal.any(tracker === void 0 ? [abort.signal, timeout.signal] : [
|
|
1036
|
-
abort.signal,
|
|
1037
|
-
timeout.signal,
|
|
1038
|
-
tracker
|
|
1039
|
-
]));
|
|
1040
|
-
const thrown = (error) => {
|
|
1041
|
-
if (abort.signal.aborted) return {
|
|
1042
|
-
kind: "cut",
|
|
1043
|
-
cause: causeOf(abort.signal),
|
|
1044
|
-
retryable: false
|
|
1045
|
-
};
|
|
1046
|
-
if (timeout.signal.aborted) return {
|
|
1047
|
-
kind: "failed",
|
|
1048
|
-
error: timeout.signal.reason
|
|
1049
|
-
};
|
|
1050
|
-
return {
|
|
1051
|
-
kind: "failed",
|
|
1052
|
-
error
|
|
1053
|
-
};
|
|
1054
|
-
};
|
|
1055
|
-
try {
|
|
1056
|
-
if (request === void 0) try {
|
|
1057
|
-
return {
|
|
1058
|
-
kind: "done",
|
|
1059
|
-
value: await call()
|
|
1060
|
-
};
|
|
1061
|
-
} catch (error) {
|
|
1062
|
-
return thrown(error);
|
|
1063
|
-
}
|
|
1064
|
-
let collected;
|
|
1065
|
-
try {
|
|
1066
|
-
const route = typeof request.route === "string" ? request.route : request.route(unit);
|
|
1067
|
-
const detail = request.detail?.(unit);
|
|
1068
|
-
collected = life.request(request.controller, route, (signal) => call(signal).then((value) => ({ value })), detail === void 0 ? {} : { detail }).collect();
|
|
1069
|
-
} catch (error) {
|
|
1070
|
-
return {
|
|
1071
|
-
kind: "failed",
|
|
1072
|
-
error
|
|
1073
|
-
};
|
|
1074
|
-
}
|
|
1075
|
-
const { items, end } = await collected;
|
|
1076
|
-
if (end.kind === "done") return {
|
|
1077
|
-
kind: "done",
|
|
1078
|
-
value: items[0]?.value
|
|
1079
|
-
};
|
|
1080
|
-
if (end.kind === "failed") return thrown(end.error);
|
|
1081
|
-
if (end.kind === "refused") return {
|
|
1082
|
-
kind: "refused",
|
|
1083
|
-
cause: end.cause
|
|
1084
|
-
};
|
|
1085
|
-
return {
|
|
1086
|
-
kind: "cut",
|
|
1087
|
-
cause: end.cause,
|
|
1088
|
-
retryable: end.retryable
|
|
1089
|
-
};
|
|
1090
|
-
} finally {
|
|
1091
|
-
cancel();
|
|
1092
|
-
}
|
|
1093
|
-
};
|
|
1094
|
-
/** This function makes a flight set on a clock. */
|
|
1095
|
-
const flights = (clock) => {
|
|
1096
|
-
const units = /* @__PURE__ */ new Set();
|
|
1097
|
-
const idle = /* @__PURE__ */ new Set();
|
|
1098
|
-
const lower = /* @__PURE__ */ new Set();
|
|
1099
|
-
let aborted = null;
|
|
1100
|
-
const settle = () => {
|
|
1101
|
-
for (const done of [...lower]) done();
|
|
1102
|
-
lower.clear();
|
|
1103
|
-
if (units.size === 0) for (const done of [...idle]) done();
|
|
1104
|
-
};
|
|
1105
|
-
const abort = (reason) => {
|
|
1106
|
-
aborted ??= { reason };
|
|
1107
|
-
for (const unit of [...units]) if (!unit.signal.aborted) unit.abort(aborted.reason);
|
|
1108
|
-
};
|
|
1109
|
-
return {
|
|
1110
|
-
get size() {
|
|
1111
|
-
return units.size;
|
|
1112
|
-
},
|
|
1113
|
-
add(unit) {
|
|
1114
|
-
units.add(unit);
|
|
1115
|
-
if (aborted !== null && !unit.signal.aborted) unit.abort(aborted.reason);
|
|
1116
|
-
return () => {
|
|
1117
|
-
if (units.delete(unit)) settle();
|
|
1118
|
-
};
|
|
1119
|
-
},
|
|
1120
|
-
abort,
|
|
1121
|
-
clear() {
|
|
1122
|
-
units.clear();
|
|
1123
|
-
settle();
|
|
1124
|
-
},
|
|
1125
|
-
drain(deadline, signal) {
|
|
1126
|
-
const cooperative = () => abort(signal.reason);
|
|
1127
|
-
if (signal.aborted) cooperative();
|
|
1128
|
-
else signal.addEventListener("abort", cooperative, { once: true });
|
|
1129
|
-
if (units.size === 0 || deadline <= clock.now()) {
|
|
1130
|
-
signal.removeEventListener("abort", cooperative);
|
|
1131
|
-
return Promise.resolve();
|
|
1132
|
-
}
|
|
1133
|
-
return new Promise((resolve) => {
|
|
1134
|
-
let cancel = () => {};
|
|
1135
|
-
const done = () => {
|
|
1136
|
-
idle.delete(done);
|
|
1137
|
-
cancel();
|
|
1138
|
-
signal.removeEventListener("abort", cooperative);
|
|
1139
|
-
resolve();
|
|
1140
|
-
};
|
|
1141
|
-
idle.add(done);
|
|
1142
|
-
cancel = clock.after(Math.max(0, deadline - clock.now()), done);
|
|
1143
|
-
});
|
|
1144
|
-
},
|
|
1145
|
-
less() {
|
|
1146
|
-
return new Promise((resolve) => lower.add(resolve));
|
|
1147
|
-
},
|
|
1148
|
-
release() {
|
|
1149
|
-
for (const done of [...lower]) done();
|
|
1150
|
-
lower.clear();
|
|
1151
|
-
}
|
|
1152
|
-
};
|
|
1153
|
-
};
|
|
1154
|
-
/** This function makes a sleeper on a clock. */
|
|
1155
|
-
const sleeper = (clock) => {
|
|
1156
|
-
let wakes = 0;
|
|
1157
|
-
let stopped = false;
|
|
1158
|
-
const sleeps = /* @__PURE__ */ new Set();
|
|
1159
|
-
const backoffs = /* @__PURE__ */ new Set();
|
|
1160
|
-
const wait = (ms, into) => stopped ? Promise.resolve() : new Promise((resolve) => {
|
|
1161
|
-
let cancel = () => {};
|
|
1162
|
-
const done = () => {
|
|
1163
|
-
if (!into.delete(done)) return;
|
|
1164
|
-
cancel();
|
|
1165
|
-
resolve();
|
|
1166
|
-
};
|
|
1167
|
-
into.add(done);
|
|
1168
|
-
if (Number.isFinite(ms)) cancel = clock.after(ms, done);
|
|
1169
|
-
});
|
|
1170
|
-
return {
|
|
1171
|
-
get wakes() {
|
|
1172
|
-
return wakes;
|
|
1173
|
-
},
|
|
1174
|
-
sleep(ms, since) {
|
|
1175
|
-
if (since !== wakes) return Promise.resolve();
|
|
1176
|
-
return wait(ms, sleeps);
|
|
1177
|
-
},
|
|
1178
|
-
backoff(ms) {
|
|
1179
|
-
return wait(ms, backoffs);
|
|
1180
|
-
},
|
|
1181
|
-
wake() {
|
|
1182
|
-
wakes += 1;
|
|
1183
|
-
const ended = sleeps.size;
|
|
1184
|
-
for (const done of [...sleeps]) done();
|
|
1185
|
-
return ended;
|
|
1186
|
-
},
|
|
1187
|
-
stop() {
|
|
1188
|
-
stopped = true;
|
|
1189
|
-
for (const done of [...sleeps, ...backoffs]) done();
|
|
1190
|
-
}
|
|
1191
|
-
};
|
|
1192
|
-
};
|
|
1193
|
-
/** This function waits for a work until a bound on the clock, in milliseconds. */
|
|
1194
|
-
const settleWithin = (clock, ms, work) => new Promise((resolve) => {
|
|
1195
|
-
const cancel = Number.isFinite(ms) ? clock.after(ms, () => resolve({ kind: "bound" })) : () => {};
|
|
1196
|
-
work.then(() => {
|
|
1197
|
-
cancel();
|
|
1198
|
-
resolve({ kind: "done" });
|
|
1199
|
-
}, (error) => {
|
|
1200
|
-
cancel();
|
|
1201
|
-
resolve({
|
|
1202
|
-
kind: "failed",
|
|
1203
|
-
error
|
|
1204
|
-
});
|
|
1205
|
-
});
|
|
1206
|
-
});
|
|
1207
|
-
/**
|
|
1208
|
-
* This function gives the value of a promise, or `stop` when the signal aborts first. Each wait adds
|
|
1209
|
-
* its own listener, and removes it when the promise settles. A race against one pending promise
|
|
1210
|
-
* keeps each settled value until that promise ends.
|
|
1211
|
-
*/
|
|
1212
|
-
const untilAbort = (promise, signal, stop) => {
|
|
1213
|
-
if (signal.aborted) return Promise.race([promise, Promise.resolve(stop)]);
|
|
1214
|
-
return new Promise((resolve, reject) => {
|
|
1215
|
-
const onAbort = () => resolve(stop);
|
|
1216
|
-
signal.addEventListener("abort", onAbort, { once: true });
|
|
1217
|
-
promise.then((value) => {
|
|
1218
|
-
signal.removeEventListener("abort", onAbort);
|
|
1219
|
-
resolve(value);
|
|
1220
|
-
}, (error) => {
|
|
1221
|
-
signal.removeEventListener("abort", onAbort);
|
|
1222
|
-
reject(error);
|
|
1223
|
-
});
|
|
1224
|
-
});
|
|
1225
|
-
};
|
|
1226
|
-
/**
|
|
1227
|
-
* This function gives the backoff of the knobs of a loop. The first delay is `retry_base`, and each
|
|
1228
|
-
* delay is two times longer, up to `retry_cap`. Each delay has a jitter up to the base, as in the Redis client.
|
|
1229
|
-
*/
|
|
1230
|
-
const retryOf = (knobs, owner) => {
|
|
1231
|
-
const base = boundedMillisecondsOf(knobs, "retry_base", owner);
|
|
1232
|
-
const backoff = {
|
|
1233
|
-
base,
|
|
1234
|
-
factor: 2,
|
|
1235
|
-
cap: boundedMillisecondsOf(knobs, "retry_cap", owner),
|
|
1236
|
-
jitter: {
|
|
1237
|
-
kind: "add",
|
|
1238
|
-
amount: base
|
|
1239
|
-
}
|
|
1240
|
-
};
|
|
1241
|
-
const problems = backoffProblems(backoff);
|
|
1242
|
-
if (problems.length > 0) throw new Error(`${owner}: the retry_base and retry_cap give no backoff: ${problems.join(", ")}`);
|
|
1243
|
-
return backoff;
|
|
1244
|
-
};
|
|
1245
|
-
/**
|
|
1246
|
-
* This function gives the problems of a loop in a declaration: each phase that the loop needs and
|
|
1247
|
-
* that the declaration does not give. A loop closes in `close transports`, and it ends its units in
|
|
1248
|
-
* `drain`. `lint()` compares the bounds with the caps of the phases (`BOUND_RULES`, S6 A10).
|
|
1249
|
-
*/
|
|
1250
|
-
const boundProblems = (owner, description) => {
|
|
1251
|
-
const has = (name) => description.shutdown.phases.some((each) => each.name === name);
|
|
1252
|
-
return [...has("close transports") ? [] : [`${owner} needs the phase "close transports" to close`], ...has("drain") ? [] : [`${owner} needs the phase "drain" to end its units`]];
|
|
1253
|
-
};
|
|
1254
|
-
//#endregion
|
|
1255
|
-
//#region src/requests.ts
|
|
1256
|
-
const isAsyncIterable$1 = (value) => value !== null && (typeof value === "object" || typeof value === "function") && typeof value[Symbol.asyncIterator] === "function";
|
|
1257
|
-
const isPromiseLike = (value) => value !== null && (typeof value === "object" || typeof value === "function") && typeof value.then === "function";
|
|
1258
|
-
/** This function gives a stream of events from a function that gives the next event. */
|
|
1259
|
-
const streamOf = (next, stop) => {
|
|
1260
|
-
let chain = Promise.resolve();
|
|
1261
|
-
const iterator = {
|
|
1262
|
-
next() {
|
|
1263
|
-
const step = chain.then(next).then((event) => event === null ? {
|
|
1264
|
-
value: void 0,
|
|
1265
|
-
done: true
|
|
1266
|
-
} : {
|
|
1267
|
-
value: event,
|
|
1268
|
-
done: false
|
|
1269
|
-
});
|
|
1270
|
-
chain = step.catch(() => {});
|
|
1271
|
-
return step;
|
|
1272
|
-
},
|
|
1273
|
-
return() {
|
|
1274
|
-
stop();
|
|
1275
|
-
return Promise.resolve({
|
|
1276
|
-
value: void 0,
|
|
1277
|
-
done: true
|
|
1278
|
-
});
|
|
1279
|
-
}
|
|
1280
|
-
};
|
|
1281
|
-
return {
|
|
1282
|
-
[Symbol.asyncIterator]: () => iterator,
|
|
1283
|
-
async collect() {
|
|
1284
|
-
const items = [];
|
|
1285
|
-
for (;;) {
|
|
1286
|
-
const step = await iterator.next();
|
|
1287
|
-
if (step.done === true) throw new Error("the stream ended without its end");
|
|
1288
|
-
if (step.value.kind === "item") items.push(step.value.value);
|
|
1289
|
-
else return {
|
|
1290
|
-
items,
|
|
1291
|
-
end: step.value
|
|
1292
|
-
};
|
|
1293
|
-
}
|
|
1294
|
-
}
|
|
1295
|
-
};
|
|
1296
|
-
};
|
|
1297
|
-
/**
|
|
1298
|
-
* This function makes a tracker. `refuses` gives the reason that the service refuses a new request
|
|
1299
|
-
* for a controller at this time, or null when it admits it. `choices` gives the function of each
|
|
1300
|
-
* route pattern, by controller and by pattern.
|
|
1301
|
-
*/
|
|
1302
|
-
const tracker = (controllers, clock, refuses, choices = /* @__PURE__ */ new Map()) => {
|
|
1303
|
-
const byName = new Map(controllers.map((controller) => [controller.name, {
|
|
1304
|
-
controller,
|
|
1305
|
-
patterns: (controller.patterns ?? []).map((declared) => ({
|
|
1306
|
-
declared,
|
|
1307
|
-
compiled: compilePattern(declared.pattern)
|
|
1308
|
-
}))
|
|
1309
|
-
}]));
|
|
1310
|
-
const flights = /* @__PURE__ */ new Set();
|
|
1311
|
-
const idle = /* @__PURE__ */ new Set();
|
|
1312
|
-
let intake = true;
|
|
1313
|
-
/** This function gives the operation of a request: its exact route, else the first pattern that matches it. */
|
|
1314
|
-
const operationOf = (controller, route, detail) => {
|
|
1315
|
-
const held = byName.get(controller);
|
|
1316
|
-
if (held === void 0) throw new Error(`there is no controller ${controller}`);
|
|
1317
|
-
const exact = held.controller.routes[route];
|
|
1318
|
-
if (exact !== void 0) return exact;
|
|
1319
|
-
for (const { declared, compiled } of held.patterns) {
|
|
1320
|
-
const found = compiled.match(route);
|
|
1321
|
-
if (found === null) continue;
|
|
1322
|
-
const names = Object.keys(declared.operations);
|
|
1323
|
-
const choose = choices.get(controller)?.get(declared.pattern);
|
|
1324
|
-
const name = choose === void 0 ? names[0] : choose({
|
|
1325
|
-
controller,
|
|
1326
|
-
route,
|
|
1327
|
-
...found,
|
|
1328
|
-
detail
|
|
1329
|
-
});
|
|
1330
|
-
const operation = declared.operations[name];
|
|
1331
|
-
if (operation === void 0) throw new Error(`the pattern ${declared.pattern} of ${controller} declares no operation ${String(name)} for ${route}`);
|
|
1332
|
-
return operation;
|
|
1333
|
-
}
|
|
1334
|
-
throw new Error(`controller ${controller} does not declare the route ${route}`);
|
|
1335
|
-
};
|
|
1336
|
-
const settle = (flight) => {
|
|
1337
|
-
if (!flights.delete(flight)) return;
|
|
1338
|
-
if (flights.size === 0) for (const done of [...idle]) done();
|
|
1339
|
-
};
|
|
1340
|
-
/** This function starts the work of an admitted request, and gives its stream. */
|
|
1341
|
-
const open = (controller, operation, work) => {
|
|
1342
|
-
const started = clock.now();
|
|
1343
|
-
const abort = new AbortController();
|
|
1344
|
-
let delivered = 0;
|
|
1345
|
-
let end = null;
|
|
1346
|
-
let ended = false;
|
|
1347
|
-
let returned = false;
|
|
1348
|
-
let source = null;
|
|
1349
|
-
let unary = null;
|
|
1350
|
-
const interruption = new AbortController();
|
|
1351
|
-
const interrupt = () => interruption.abort();
|
|
1352
|
-
const cutEnd = (cause) => {
|
|
1353
|
-
const since = quantity(Math.max(0, clock.now() - started) / 1e3, "s");
|
|
1354
|
-
const retryable = mayReissue(operation, since);
|
|
1355
|
-
return {
|
|
1356
|
-
kind: "cut",
|
|
1357
|
-
status: delivered > 0 ? null : retryable ? 503 : 500,
|
|
1358
|
-
retryable,
|
|
1359
|
-
cause,
|
|
1360
|
-
delivered
|
|
1361
|
-
};
|
|
1362
|
-
};
|
|
1363
|
-
/** This function ends the work of the request: the end is known, and the request leaves the flights. */
|
|
1364
|
-
const finish = (given) => {
|
|
1365
|
-
if (end !== null) return;
|
|
1366
|
-
end = given;
|
|
1367
|
-
settle(flight);
|
|
1368
|
-
interrupt();
|
|
1369
|
-
};
|
|
1370
|
-
/** This function ends the work after a throw: a failure after the shutdown aborted it is a cut. */
|
|
1371
|
-
const failed = (error) => finish(abort.signal.aborted ? cutEnd("the shutdown aborted it, and it failed") : {
|
|
1372
|
-
kind: "failed",
|
|
1373
|
-
error,
|
|
1374
|
-
delivered
|
|
1375
|
-
});
|
|
1376
|
-
const closeSource = () => {
|
|
1377
|
-
if (source === null || returned) return;
|
|
1378
|
-
returned = true;
|
|
1379
|
-
try {
|
|
1380
|
-
Promise.resolve(source.return?.()).catch(() => {});
|
|
1381
|
-
} catch {}
|
|
1382
|
-
};
|
|
1383
|
-
const flight = {
|
|
1384
|
-
controller,
|
|
1385
|
-
cooperative: () => {
|
|
1386
|
-
abort.abort(/* @__PURE__ */ new Error("the cooperative abort of the drain"));
|
|
1387
|
-
closeSource();
|
|
1388
|
-
},
|
|
1389
|
-
cut: (cause) => {
|
|
1390
|
-
abort.abort(new Error(cause));
|
|
1391
|
-
finish(cutEnd(cause));
|
|
1392
|
-
closeSource();
|
|
1393
|
-
}
|
|
1394
|
-
};
|
|
1395
|
-
flights.add(flight);
|
|
1396
|
-
/** This function takes the result of the work: a stream, or one value. */
|
|
1397
|
-
const take = (result) => {
|
|
1398
|
-
if (end !== null) return;
|
|
1399
|
-
if (isAsyncIterable$1(result)) {
|
|
1400
|
-
source = result[Symbol.asyncIterator]();
|
|
1401
|
-
if (abort.signal.aborted) closeSource();
|
|
1402
|
-
} else {
|
|
1403
|
-
unary = { value: result };
|
|
1404
|
-
settle(flight);
|
|
1405
|
-
}
|
|
1406
|
-
};
|
|
1407
|
-
let ready;
|
|
1408
|
-
try {
|
|
1409
|
-
const result = work(abort.signal);
|
|
1410
|
-
if (isAsyncIterable$1(result) || !isPromiseLike(result)) {
|
|
1411
|
-
take(result);
|
|
1412
|
-
ready = Promise.resolve();
|
|
1413
|
-
} else ready = Promise.resolve(result).then(take, failed);
|
|
1414
|
-
} catch (error) {
|
|
1415
|
-
failed(error);
|
|
1416
|
-
ready = Promise.resolve();
|
|
1417
|
-
}
|
|
1418
|
-
const next = async () => {
|
|
1419
|
-
if (ended) return null;
|
|
1420
|
-
if (end === null) await untilAbort(ready, interruption.signal, "interrupted");
|
|
1421
|
-
if (end === null && unary !== null) {
|
|
1422
|
-
const { value } = unary;
|
|
1423
|
-
unary = null;
|
|
1424
|
-
delivered = 1;
|
|
1425
|
-
end = {
|
|
1426
|
-
kind: "done",
|
|
1427
|
-
delivered
|
|
1428
|
-
};
|
|
1429
|
-
return {
|
|
1430
|
-
kind: "item",
|
|
1431
|
-
value
|
|
1432
|
-
};
|
|
1433
|
-
}
|
|
1434
|
-
if (end === null && source !== null) {
|
|
1435
|
-
const pending = source.next();
|
|
1436
|
-
let step;
|
|
1437
|
-
try {
|
|
1438
|
-
step = await untilAbort(Promise.resolve(pending), interruption.signal, "interrupted");
|
|
1439
|
-
} catch (error) {
|
|
1440
|
-
failed(error);
|
|
1441
|
-
step = "interrupted";
|
|
1442
|
-
}
|
|
1443
|
-
if (step !== "interrupted") {
|
|
1444
|
-
if (step.done !== true) {
|
|
1445
|
-
delivered += 1;
|
|
1446
|
-
return {
|
|
1447
|
-
kind: "item",
|
|
1448
|
-
value: step.value
|
|
1449
|
-
};
|
|
1450
|
-
}
|
|
1451
|
-
finish(abort.signal.aborted ? cutEnd("the shutdown ended the stream") : {
|
|
1452
|
-
kind: "done",
|
|
1453
|
-
delivered
|
|
1454
|
-
});
|
|
1455
|
-
}
|
|
1456
|
-
}
|
|
1457
|
-
ended = true;
|
|
1458
|
-
return end;
|
|
1459
|
-
};
|
|
1460
|
-
const stop = () => {
|
|
1461
|
-
if (end === null) {
|
|
1462
|
-
abort.abort(/* @__PURE__ */ new Error("the reader stopped the stream"));
|
|
1463
|
-
finish({
|
|
1464
|
-
kind: "done",
|
|
1465
|
-
delivered
|
|
1466
|
-
});
|
|
1467
|
-
closeSource();
|
|
1468
|
-
}
|
|
1469
|
-
ended = true;
|
|
1470
|
-
};
|
|
1471
|
-
return streamOf(next, stop);
|
|
1472
|
-
};
|
|
1473
|
-
return {
|
|
1474
|
-
request(controller, route, work, options = {}) {
|
|
1475
|
-
const operation = operationOf(controller, route, options.detail);
|
|
1476
|
-
const refusal = intake ? refuses(controller) : "the service stopped its intake";
|
|
1477
|
-
if (refusal !== null) {
|
|
1478
|
-
let given = false;
|
|
1479
|
-
const refused = {
|
|
1480
|
-
kind: "refused",
|
|
1481
|
-
status: 503,
|
|
1482
|
-
retryable: true,
|
|
1483
|
-
cause: refusal,
|
|
1484
|
-
delivered: 0
|
|
1485
|
-
};
|
|
1486
|
-
return streamOf(() => {
|
|
1487
|
-
if (given) return Promise.resolve(null);
|
|
1488
|
-
given = true;
|
|
1489
|
-
return Promise.resolve(refused);
|
|
1490
|
-
}, () => {
|
|
1491
|
-
given = true;
|
|
1492
|
-
});
|
|
1493
|
-
}
|
|
1494
|
-
return open(controller, operation, work);
|
|
1495
|
-
},
|
|
1496
|
-
inFlight(controller) {
|
|
1497
|
-
let count = 0;
|
|
1498
|
-
for (const flight of flights) if (controller === void 0 || flight.controller === controller) count += 1;
|
|
1499
|
-
return count;
|
|
1500
|
-
},
|
|
1501
|
-
stopIntake() {
|
|
1502
|
-
intake = false;
|
|
1503
|
-
},
|
|
1504
|
-
cooperativeAbort() {
|
|
1505
|
-
for (const flight of [...flights]) flight.cooperative();
|
|
1506
|
-
},
|
|
1507
|
-
drain(deadline) {
|
|
1508
|
-
if (flights.size === 0) return Promise.resolve();
|
|
1509
|
-
return new Promise((resolve) => {
|
|
1510
|
-
let finished = false;
|
|
1511
|
-
const done = () => {
|
|
1512
|
-
if (finished) return;
|
|
1513
|
-
finished = true;
|
|
1514
|
-
idle.delete(done);
|
|
1515
|
-
cancel();
|
|
1516
|
-
resolve();
|
|
1517
|
-
};
|
|
1518
|
-
idle.add(done);
|
|
1519
|
-
const cancel = clock.after(Math.max(0, deadline - clock.now()), () => {
|
|
1520
|
-
for (const flight of [...flights]) flight.cut("the drain reached its deadline");
|
|
1521
|
-
done();
|
|
1522
|
-
});
|
|
1523
|
-
});
|
|
1524
|
-
}
|
|
1525
|
-
};
|
|
1526
|
-
};
|
|
1527
|
-
//#endregion
|
|
1528
784
|
//#region src/lifecycle.ts
|
|
1529
785
|
/**
|
|
1530
786
|
* These are the stages of the shutdown, in their sequence. A declared phase with one of these
|