@mark1russell7/service 0.6.3 → 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 +2 -2
- package/build/index.js +2 -580
- package/build/index.js.map +1 -1
- package/build/requests-BIq9Tr4P.js +586 -0
- package/build/requests-BIq9Tr4P.js.map +1 -0
- package/build/testing.d.ts +54 -3
- package/build/testing.js +259 -12
- package/build/testing.js.map +1 -1
- package/package.json +3 -2
package/README.md
CHANGED
|
@@ -632,7 +632,7 @@ console.log(outcome.kind);
|
|
|
632
632
|
- `quietServer()` starts a TCP echo server that can go quiet. Its connections stay open while it is quiet, as with a frozen peer.
|
|
633
633
|
- `detachedLifecycle()` gives a lifecycle with no service, for the start of a transport in a test.
|
|
634
634
|
- `fakeHost(options)` gives the ports of the host entry on a fake clock, a fake file system and fake processes. It records each command and each signal. A test ends a child with `exit(code)`, and `polite` makes each child obey a staged stop.
|
|
635
|
-
- `transportConformance(make, options)` generates the conformance test of a transport. `make` gives a subject: the transport, a way to offer new work and a way to hold work in flight. The cases check that `stopIntake` refuses new work, and that the count is correct and comes from events. They also check that `drain` holds to its deadline and gives the cooperative abort, and that `close` is idempotent and bounded. The fake clock makes the cases fast, and the real clock measures them.
|
|
635
|
+
- `transportConformance(make, options)` generates the conformance test of a transport. `make` gives a subject: the transport, a way to offer new work and a way to hold work in flight. The cases check that `stopIntake` refuses new work, and that the count is correct and comes from events. They also check that `drain` holds to its deadline and gives the cooperative abort, and that `close` is idempotent and bounded. The fake clock makes the cases fast, and the real clock measures them. The option `streams` adds six cases of the streams (S6 D4). Then the subject also gives `call`, which sends the work of the kit through `life.request`. A unary call gives one value and its status. A stream stays in flight until its end, and a failure after its first item is an error line, not a status. The drain waits for an open stream, and its cooperative abort and its cut end the stream with an end that the caller gets.
|
|
636
636
|
- `contractCases(declare)` generates the contract test of a declaration. The cases are the service at rest, each dependency unavailable, each dependency degraded by 100 ms, each recovery, and a negative control. A dependency that no check exercises is a finding, and so is a check whose timeout a latency of 100 ms fails.
|
|
637
637
|
|
|
638
638
|
## The Redis client
|
|
@@ -893,7 +893,7 @@ for await (const event of stream) {
|
|
|
893
893
|
- **The idle close of a slow answer (SYS-33).** A client that does not read gets each byte of a body of 64 MB. The stop of the intake and a sweep after it come before the client reads. These tests use real sockets of the loopback, but not on Windows.
|
|
894
894
|
- **The headers of the outcomes.** Each path of the request tracker gets its status. Each 503 gets `Retry-After`: the refusals, the retryable cut and the cut of a unary stream. No 500 gets it.
|
|
895
895
|
- **The headers on the port.** The transport adds `Retry-After` to each 503 of a probe and of a handler that writes its own status, in each form of `writeHead`. It takes `Retry-After` from each 500. The probe server gives its 503s the time of its knob.
|
|
896
|
-
- **The conformance of a transport.** A reference transport passes each case, and each of
|
|
896
|
+
- **The conformance of a transport.** A reference transport passes each case, and each of 14 mutations of it fails the case of its part. Five of the mutations break a stream. The HTTP transport passes each case on a fake server, for its requests and its upgrades, and for the streams of its requests.
|
|
897
897
|
- **The worker loop.** These tests use the fake clock. They cover the sleep, `"more"`, the backoff and its jitter, `wake()`, the stop of the intake and the drain with its cooperative abort. They also cover the bound of an iteration, the lanes, the setup, the teardown, the close bound and the law (O20).
|
|
898
898
|
- **The queue drain.** These tests cover the slots, the poll, the policy of A11 and the count of deliveries. They also cover a job that goes back at the stop, an async iterable and a failed take. A cut job of each kind of route, the close bound and the refusals of the service have tests too.
|
|
899
899
|
- **The conformance of the helpers.** The worker loop passes each case of the kit with three lanes, and the queue drain with three slots. Each one passes with the request tracking and without it. With one lane, the loop passes each case that holds one work.
|
package/build/index.js
CHANGED
|
@@ -5,11 +5,12 @@ 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
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";
|
|
8
9
|
import { backoffBand, backoffDelay, backoffProblems } from "./backoff.js";
|
|
9
10
|
import { DRAIN_RULES, O04, O09, O10, O16 } from "./rules.js";
|
|
10
11
|
import { t as retrier } from "./retrier-Bzkme_yM.js";
|
|
11
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";
|
|
12
|
-
import { convertExact, convertible,
|
|
13
|
+
import { convertExact, convertible, rationals, valueIn } from "@mark1russell7/quantity";
|
|
13
14
|
import { replay } from "@mark1russell7/algebra/replay";
|
|
14
15
|
//#region src/description.ts
|
|
15
16
|
/**
|
|
@@ -780,585 +781,6 @@ const logEvents = (logger, description) => {
|
|
|
780
781
|
};
|
|
781
782
|
};
|
|
782
783
|
//#endregion
|
|
783
|
-
//#region src/routes.ts
|
|
784
|
-
const NAME = /^[A-Za-z_][A-Za-z0-9_]*/;
|
|
785
|
-
/** This function gives the problems of the syntax of a pattern: an empty pattern, a part without a name, or a name two times. */
|
|
786
|
-
const patternProblems = (owner, pattern) => {
|
|
787
|
-
const problems = [];
|
|
788
|
-
if (pattern.length === 0) problems.push(`${owner} has an empty route pattern`);
|
|
789
|
-
const names = /* @__PURE__ */ new Set();
|
|
790
|
-
for (let at = 0; at < pattern.length; at += 1) {
|
|
791
|
-
if (pattern[at] !== ":") continue;
|
|
792
|
-
const name = NAME.exec(pattern.slice(at + 1))?.[0];
|
|
793
|
-
if (name === void 0) {
|
|
794
|
-
problems.push(`${owner}'s pattern ${pattern} has a ":" without a name at ${at}`);
|
|
795
|
-
continue;
|
|
796
|
-
}
|
|
797
|
-
if (names.has(name)) problems.push(`${owner}'s pattern ${pattern} names :${name} two times`);
|
|
798
|
-
names.add(name);
|
|
799
|
-
at += name.length;
|
|
800
|
-
}
|
|
801
|
-
return problems;
|
|
802
|
-
};
|
|
803
|
-
/** This function gives the problems of a pattern and of each of its operations, which `describe()` refuses. */
|
|
804
|
-
const routePatternProblems = (owner, declared) => {
|
|
805
|
-
const problems = patternProblems(owner, declared.pattern);
|
|
806
|
-
const names = Object.keys(declared.operations);
|
|
807
|
-
if (names.length === 0) problems.push(`${owner}'s pattern ${declared.pattern} declares no operation`);
|
|
808
|
-
for (const name of names) problems.push(...operationProblems(owner, `${declared.pattern} (${name})`, declared.operations[name]));
|
|
809
|
-
return problems;
|
|
810
|
-
};
|
|
811
|
-
/** This function compiles a pattern to a matcher. The match must cover the whole route. */
|
|
812
|
-
const compilePattern = (pattern) => {
|
|
813
|
-
const kinds = [];
|
|
814
|
-
let source = "^";
|
|
815
|
-
for (let at = 0; at < pattern.length; at += 1) {
|
|
816
|
-
const char = pattern[at];
|
|
817
|
-
if (char === "*") {
|
|
818
|
-
source += "([\\s\\S]*)";
|
|
819
|
-
kinds.push({ kind: "wildcard" });
|
|
820
|
-
continue;
|
|
821
|
-
}
|
|
822
|
-
if (char === ":") {
|
|
823
|
-
const name = NAME.exec(pattern.slice(at + 1))?.[0];
|
|
824
|
-
if (name !== void 0) {
|
|
825
|
-
source += "([^/]+)";
|
|
826
|
-
kinds.push({
|
|
827
|
-
kind: "param",
|
|
828
|
-
name
|
|
829
|
-
});
|
|
830
|
-
at += name.length;
|
|
831
|
-
continue;
|
|
832
|
-
}
|
|
833
|
-
}
|
|
834
|
-
source += char.replace(/[.*+?^${}()|[\]\\/]/g, "\\$&");
|
|
835
|
-
}
|
|
836
|
-
const matcher = new RegExp(`${source}$`);
|
|
837
|
-
return {
|
|
838
|
-
pattern,
|
|
839
|
-
match(route) {
|
|
840
|
-
const found = matcher.exec(route);
|
|
841
|
-
if (found === null) return null;
|
|
842
|
-
const params = {};
|
|
843
|
-
const wildcards = [];
|
|
844
|
-
kinds.forEach((part, index) => {
|
|
845
|
-
const text = found[index + 1] ?? "";
|
|
846
|
-
if (part.kind === "param") params[part.name] = text;
|
|
847
|
-
else wildcards.push(text);
|
|
848
|
-
});
|
|
849
|
-
return {
|
|
850
|
-
params,
|
|
851
|
-
wildcards
|
|
852
|
-
};
|
|
853
|
-
}
|
|
854
|
-
};
|
|
855
|
-
};
|
|
856
|
-
//#endregion
|
|
857
|
-
//#region src/units.ts
|
|
858
|
-
/** This function gives the cause of an abort as text. */
|
|
859
|
-
const causeOf = (signal) => signal.reason instanceof Error ? signal.reason.message : String(signal.reason ?? "the unit got an abort");
|
|
860
|
-
/**
|
|
861
|
-
* This function does one unit of work, and gives its end. With a request, the unit goes through
|
|
862
|
-
* `life.request`, thus the service counts it, drains it and can cut it. A throw after the abort of
|
|
863
|
-
* the transport is a cut, not a failure (S6 A10). A throw after the bound is a failure.
|
|
864
|
-
*/
|
|
865
|
-
const runUnit = async (run) => {
|
|
866
|
-
const { clock, bound, abort, life, request, unit, work } = run;
|
|
867
|
-
const timeout = new AbortController();
|
|
868
|
-
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`))) : () => {};
|
|
869
|
-
const call = async (tracker) => work(AbortSignal.any(tracker === void 0 ? [abort.signal, timeout.signal] : [
|
|
870
|
-
abort.signal,
|
|
871
|
-
timeout.signal,
|
|
872
|
-
tracker
|
|
873
|
-
]));
|
|
874
|
-
const thrown = (error) => {
|
|
875
|
-
if (abort.signal.aborted) return {
|
|
876
|
-
kind: "cut",
|
|
877
|
-
cause: causeOf(abort.signal),
|
|
878
|
-
retryable: false
|
|
879
|
-
};
|
|
880
|
-
if (timeout.signal.aborted) return {
|
|
881
|
-
kind: "failed",
|
|
882
|
-
error: timeout.signal.reason
|
|
883
|
-
};
|
|
884
|
-
return {
|
|
885
|
-
kind: "failed",
|
|
886
|
-
error
|
|
887
|
-
};
|
|
888
|
-
};
|
|
889
|
-
try {
|
|
890
|
-
if (request === void 0) try {
|
|
891
|
-
return {
|
|
892
|
-
kind: "done",
|
|
893
|
-
value: await call()
|
|
894
|
-
};
|
|
895
|
-
} catch (error) {
|
|
896
|
-
return thrown(error);
|
|
897
|
-
}
|
|
898
|
-
let collected;
|
|
899
|
-
try {
|
|
900
|
-
const route = typeof request.route === "string" ? request.route : request.route(unit);
|
|
901
|
-
const detail = request.detail?.(unit);
|
|
902
|
-
collected = life.request(request.controller, route, (signal) => call(signal).then((value) => ({ value })), detail === void 0 ? {} : { detail }).collect();
|
|
903
|
-
} catch (error) {
|
|
904
|
-
return {
|
|
905
|
-
kind: "failed",
|
|
906
|
-
error
|
|
907
|
-
};
|
|
908
|
-
}
|
|
909
|
-
const { items, end } = await collected;
|
|
910
|
-
if (end.kind === "done") return {
|
|
911
|
-
kind: "done",
|
|
912
|
-
value: items[0]?.value
|
|
913
|
-
};
|
|
914
|
-
if (end.kind === "failed") return thrown(end.error);
|
|
915
|
-
if (end.kind === "refused") return {
|
|
916
|
-
kind: "refused",
|
|
917
|
-
cause: end.cause
|
|
918
|
-
};
|
|
919
|
-
return {
|
|
920
|
-
kind: "cut",
|
|
921
|
-
cause: end.cause,
|
|
922
|
-
retryable: end.retryable
|
|
923
|
-
};
|
|
924
|
-
} finally {
|
|
925
|
-
cancel();
|
|
926
|
-
}
|
|
927
|
-
};
|
|
928
|
-
/** This function makes a flight set on a clock. */
|
|
929
|
-
const flights = (clock) => {
|
|
930
|
-
const units = /* @__PURE__ */ new Set();
|
|
931
|
-
const idle = /* @__PURE__ */ new Set();
|
|
932
|
-
const lower = /* @__PURE__ */ new Set();
|
|
933
|
-
let aborted = null;
|
|
934
|
-
const settle = () => {
|
|
935
|
-
for (const done of [...lower]) done();
|
|
936
|
-
lower.clear();
|
|
937
|
-
if (units.size === 0) for (const done of [...idle]) done();
|
|
938
|
-
};
|
|
939
|
-
const abort = (reason) => {
|
|
940
|
-
aborted ??= { reason };
|
|
941
|
-
for (const unit of [...units]) if (!unit.signal.aborted) unit.abort(aborted.reason);
|
|
942
|
-
};
|
|
943
|
-
return {
|
|
944
|
-
get size() {
|
|
945
|
-
return units.size;
|
|
946
|
-
},
|
|
947
|
-
add(unit) {
|
|
948
|
-
units.add(unit);
|
|
949
|
-
if (aborted !== null && !unit.signal.aborted) unit.abort(aborted.reason);
|
|
950
|
-
return () => {
|
|
951
|
-
if (units.delete(unit)) settle();
|
|
952
|
-
};
|
|
953
|
-
},
|
|
954
|
-
abort,
|
|
955
|
-
clear() {
|
|
956
|
-
units.clear();
|
|
957
|
-
settle();
|
|
958
|
-
},
|
|
959
|
-
drain(deadline, signal) {
|
|
960
|
-
const cooperative = () => abort(signal.reason);
|
|
961
|
-
if (signal.aborted) cooperative();
|
|
962
|
-
else signal.addEventListener("abort", cooperative, { once: true });
|
|
963
|
-
if (units.size === 0 || deadline <= clock.now()) {
|
|
964
|
-
signal.removeEventListener("abort", cooperative);
|
|
965
|
-
return Promise.resolve();
|
|
966
|
-
}
|
|
967
|
-
return new Promise((resolve) => {
|
|
968
|
-
let cancel = () => {};
|
|
969
|
-
const done = () => {
|
|
970
|
-
idle.delete(done);
|
|
971
|
-
cancel();
|
|
972
|
-
signal.removeEventListener("abort", cooperative);
|
|
973
|
-
resolve();
|
|
974
|
-
};
|
|
975
|
-
idle.add(done);
|
|
976
|
-
cancel = clock.after(Math.max(0, deadline - clock.now()), done);
|
|
977
|
-
});
|
|
978
|
-
},
|
|
979
|
-
less() {
|
|
980
|
-
return new Promise((resolve) => lower.add(resolve));
|
|
981
|
-
},
|
|
982
|
-
release() {
|
|
983
|
-
for (const done of [...lower]) done();
|
|
984
|
-
lower.clear();
|
|
985
|
-
}
|
|
986
|
-
};
|
|
987
|
-
};
|
|
988
|
-
/** This function makes a sleeper on a clock. */
|
|
989
|
-
const sleeper = (clock) => {
|
|
990
|
-
let wakes = 0;
|
|
991
|
-
let stopped = false;
|
|
992
|
-
const sleeps = /* @__PURE__ */ new Set();
|
|
993
|
-
const backoffs = /* @__PURE__ */ new Set();
|
|
994
|
-
const wait = (ms, into) => stopped ? Promise.resolve() : new Promise((resolve) => {
|
|
995
|
-
let cancel = () => {};
|
|
996
|
-
const done = () => {
|
|
997
|
-
if (!into.delete(done)) return;
|
|
998
|
-
cancel();
|
|
999
|
-
resolve();
|
|
1000
|
-
};
|
|
1001
|
-
into.add(done);
|
|
1002
|
-
if (Number.isFinite(ms)) cancel = clock.after(ms, done);
|
|
1003
|
-
});
|
|
1004
|
-
return {
|
|
1005
|
-
get wakes() {
|
|
1006
|
-
return wakes;
|
|
1007
|
-
},
|
|
1008
|
-
sleep(ms, since) {
|
|
1009
|
-
if (since !== wakes) return Promise.resolve();
|
|
1010
|
-
return wait(ms, sleeps);
|
|
1011
|
-
},
|
|
1012
|
-
backoff(ms) {
|
|
1013
|
-
return wait(ms, backoffs);
|
|
1014
|
-
},
|
|
1015
|
-
wake() {
|
|
1016
|
-
wakes += 1;
|
|
1017
|
-
const ended = sleeps.size;
|
|
1018
|
-
for (const done of [...sleeps]) done();
|
|
1019
|
-
return ended;
|
|
1020
|
-
},
|
|
1021
|
-
stop() {
|
|
1022
|
-
stopped = true;
|
|
1023
|
-
for (const done of [...sleeps, ...backoffs]) done();
|
|
1024
|
-
}
|
|
1025
|
-
};
|
|
1026
|
-
};
|
|
1027
|
-
/** This function waits for a work until a bound on the clock, in milliseconds. */
|
|
1028
|
-
const settleWithin = (clock, ms, work) => new Promise((resolve) => {
|
|
1029
|
-
const cancel = Number.isFinite(ms) ? clock.after(ms, () => resolve({ kind: "bound" })) : () => {};
|
|
1030
|
-
work.then(() => {
|
|
1031
|
-
cancel();
|
|
1032
|
-
resolve({ kind: "done" });
|
|
1033
|
-
}, (error) => {
|
|
1034
|
-
cancel();
|
|
1035
|
-
resolve({
|
|
1036
|
-
kind: "failed",
|
|
1037
|
-
error
|
|
1038
|
-
});
|
|
1039
|
-
});
|
|
1040
|
-
});
|
|
1041
|
-
/**
|
|
1042
|
-
* This function gives the value of a promise, or `stop` when the signal aborts first. Each wait adds
|
|
1043
|
-
* its own listener, and removes it when the promise settles. A race against one pending promise
|
|
1044
|
-
* keeps each settled value until that promise ends.
|
|
1045
|
-
*/
|
|
1046
|
-
const untilAbort = (promise, signal, stop) => {
|
|
1047
|
-
if (signal.aborted) return Promise.race([promise, Promise.resolve(stop)]);
|
|
1048
|
-
return new Promise((resolve, reject) => {
|
|
1049
|
-
const onAbort = () => resolve(stop);
|
|
1050
|
-
signal.addEventListener("abort", onAbort, { once: true });
|
|
1051
|
-
promise.then((value) => {
|
|
1052
|
-
signal.removeEventListener("abort", onAbort);
|
|
1053
|
-
resolve(value);
|
|
1054
|
-
}, (error) => {
|
|
1055
|
-
signal.removeEventListener("abort", onAbort);
|
|
1056
|
-
reject(error);
|
|
1057
|
-
});
|
|
1058
|
-
});
|
|
1059
|
-
};
|
|
1060
|
-
/**
|
|
1061
|
-
* This function gives the backoff of the knobs of a loop. The first delay is `retry_base`, and each
|
|
1062
|
-
* delay is two times longer, up to `retry_cap`. Each delay has a jitter up to the base, as in the Redis client.
|
|
1063
|
-
*/
|
|
1064
|
-
const retryOf = (knobs, owner) => {
|
|
1065
|
-
const base = boundedMillisecondsOf(knobs, "retry_base", owner);
|
|
1066
|
-
const backoff = {
|
|
1067
|
-
base,
|
|
1068
|
-
factor: 2,
|
|
1069
|
-
cap: boundedMillisecondsOf(knobs, "retry_cap", owner),
|
|
1070
|
-
jitter: {
|
|
1071
|
-
kind: "add",
|
|
1072
|
-
amount: base
|
|
1073
|
-
}
|
|
1074
|
-
};
|
|
1075
|
-
const problems = backoffProblems(backoff);
|
|
1076
|
-
if (problems.length > 0) throw new Error(`${owner}: the retry_base and retry_cap give no backoff: ${problems.join(", ")}`);
|
|
1077
|
-
return backoff;
|
|
1078
|
-
};
|
|
1079
|
-
/**
|
|
1080
|
-
* This function gives the problems of a loop in a declaration: each phase that the loop needs and
|
|
1081
|
-
* that the declaration does not give. A loop closes in `close transports`, and it ends its units in
|
|
1082
|
-
* `drain`. `lint()` compares the bounds with the caps of the phases (`BOUND_RULES`, S6 A10).
|
|
1083
|
-
*/
|
|
1084
|
-
const boundProblems = (owner, description) => {
|
|
1085
|
-
const has = (name) => description.shutdown.phases.some((each) => each.name === name);
|
|
1086
|
-
return [...has("close transports") ? [] : [`${owner} needs the phase "close transports" to close`], ...has("drain") ? [] : [`${owner} needs the phase "drain" to end its units`]];
|
|
1087
|
-
};
|
|
1088
|
-
//#endregion
|
|
1089
|
-
//#region src/requests.ts
|
|
1090
|
-
const isAsyncIterable$1 = (value) => value !== null && (typeof value === "object" || typeof value === "function") && typeof value[Symbol.asyncIterator] === "function";
|
|
1091
|
-
const isPromiseLike = (value) => value !== null && (typeof value === "object" || typeof value === "function") && typeof value.then === "function";
|
|
1092
|
-
/** This function gives a stream of events from a function that gives the next event. */
|
|
1093
|
-
const streamOf = (next, stop) => {
|
|
1094
|
-
let chain = Promise.resolve();
|
|
1095
|
-
const iterator = {
|
|
1096
|
-
next() {
|
|
1097
|
-
const step = chain.then(next).then((event) => event === null ? {
|
|
1098
|
-
value: void 0,
|
|
1099
|
-
done: true
|
|
1100
|
-
} : {
|
|
1101
|
-
value: event,
|
|
1102
|
-
done: false
|
|
1103
|
-
});
|
|
1104
|
-
chain = step.catch(() => {});
|
|
1105
|
-
return step;
|
|
1106
|
-
},
|
|
1107
|
-
return() {
|
|
1108
|
-
stop();
|
|
1109
|
-
return Promise.resolve({
|
|
1110
|
-
value: void 0,
|
|
1111
|
-
done: true
|
|
1112
|
-
});
|
|
1113
|
-
}
|
|
1114
|
-
};
|
|
1115
|
-
return {
|
|
1116
|
-
[Symbol.asyncIterator]: () => iterator,
|
|
1117
|
-
async collect() {
|
|
1118
|
-
const items = [];
|
|
1119
|
-
for (;;) {
|
|
1120
|
-
const step = await iterator.next();
|
|
1121
|
-
if (step.done === true) throw new Error("the stream ended without its end");
|
|
1122
|
-
if (step.value.kind === "item") items.push(step.value.value);
|
|
1123
|
-
else return {
|
|
1124
|
-
items,
|
|
1125
|
-
end: step.value
|
|
1126
|
-
};
|
|
1127
|
-
}
|
|
1128
|
-
}
|
|
1129
|
-
};
|
|
1130
|
-
};
|
|
1131
|
-
/**
|
|
1132
|
-
* This function makes a tracker. `refuses` gives the reason that the service refuses a new request
|
|
1133
|
-
* for a controller at this time, or null when it admits it. `choices` gives the function of each
|
|
1134
|
-
* route pattern, by controller and by pattern.
|
|
1135
|
-
*/
|
|
1136
|
-
const tracker = (controllers, clock, refuses, choices = /* @__PURE__ */ new Map()) => {
|
|
1137
|
-
const byName = new Map(controllers.map((controller) => [controller.name, {
|
|
1138
|
-
controller,
|
|
1139
|
-
patterns: (controller.patterns ?? []).map((declared) => ({
|
|
1140
|
-
declared,
|
|
1141
|
-
compiled: compilePattern(declared.pattern)
|
|
1142
|
-
}))
|
|
1143
|
-
}]));
|
|
1144
|
-
const flights = /* @__PURE__ */ new Set();
|
|
1145
|
-
const idle = /* @__PURE__ */ new Set();
|
|
1146
|
-
let intake = true;
|
|
1147
|
-
/** This function gives the operation of a request: its exact route, else the first pattern that matches it. */
|
|
1148
|
-
const operationOf = (controller, route, detail) => {
|
|
1149
|
-
const held = byName.get(controller);
|
|
1150
|
-
if (held === void 0) throw new Error(`there is no controller ${controller}`);
|
|
1151
|
-
const exact = held.controller.routes[route];
|
|
1152
|
-
if (exact !== void 0) return exact;
|
|
1153
|
-
for (const { declared, compiled } of held.patterns) {
|
|
1154
|
-
const found = compiled.match(route);
|
|
1155
|
-
if (found === null) continue;
|
|
1156
|
-
const names = Object.keys(declared.operations);
|
|
1157
|
-
const choose = choices.get(controller)?.get(declared.pattern);
|
|
1158
|
-
const name = choose === void 0 ? names[0] : choose({
|
|
1159
|
-
controller,
|
|
1160
|
-
route,
|
|
1161
|
-
...found,
|
|
1162
|
-
detail
|
|
1163
|
-
});
|
|
1164
|
-
const operation = declared.operations[name];
|
|
1165
|
-
if (operation === void 0) throw new Error(`the pattern ${declared.pattern} of ${controller} declares no operation ${String(name)} for ${route}`);
|
|
1166
|
-
return operation;
|
|
1167
|
-
}
|
|
1168
|
-
throw new Error(`controller ${controller} does not declare the route ${route}`);
|
|
1169
|
-
};
|
|
1170
|
-
const settle = (flight) => {
|
|
1171
|
-
if (!flights.delete(flight)) return;
|
|
1172
|
-
if (flights.size === 0) for (const done of [...idle]) done();
|
|
1173
|
-
};
|
|
1174
|
-
/** This function starts the work of an admitted request, and gives its stream. */
|
|
1175
|
-
const open = (controller, operation, work) => {
|
|
1176
|
-
const started = clock.now();
|
|
1177
|
-
const abort = new AbortController();
|
|
1178
|
-
let delivered = 0;
|
|
1179
|
-
let end = null;
|
|
1180
|
-
let ended = false;
|
|
1181
|
-
let returned = false;
|
|
1182
|
-
let source = null;
|
|
1183
|
-
let unary = null;
|
|
1184
|
-
const interruption = new AbortController();
|
|
1185
|
-
const interrupt = () => interruption.abort();
|
|
1186
|
-
const cutEnd = (cause) => {
|
|
1187
|
-
const since = quantity(Math.max(0, clock.now() - started) / 1e3, "s");
|
|
1188
|
-
const retryable = mayReissue(operation, since);
|
|
1189
|
-
return {
|
|
1190
|
-
kind: "cut",
|
|
1191
|
-
status: delivered > 0 ? null : retryable ? 503 : 500,
|
|
1192
|
-
retryable,
|
|
1193
|
-
cause,
|
|
1194
|
-
delivered
|
|
1195
|
-
};
|
|
1196
|
-
};
|
|
1197
|
-
/** This function ends the work of the request: the end is known, and the request leaves the flights. */
|
|
1198
|
-
const finish = (given) => {
|
|
1199
|
-
if (end !== null) return;
|
|
1200
|
-
end = given;
|
|
1201
|
-
settle(flight);
|
|
1202
|
-
interrupt();
|
|
1203
|
-
};
|
|
1204
|
-
/** This function ends the work after a throw: a failure after the shutdown aborted it is a cut. */
|
|
1205
|
-
const failed = (error) => finish(abort.signal.aborted ? cutEnd("the shutdown aborted it, and it failed") : {
|
|
1206
|
-
kind: "failed",
|
|
1207
|
-
error,
|
|
1208
|
-
delivered
|
|
1209
|
-
});
|
|
1210
|
-
const closeSource = () => {
|
|
1211
|
-
if (source === null || returned) return;
|
|
1212
|
-
returned = true;
|
|
1213
|
-
try {
|
|
1214
|
-
Promise.resolve(source.return?.()).catch(() => {});
|
|
1215
|
-
} catch {}
|
|
1216
|
-
};
|
|
1217
|
-
const flight = {
|
|
1218
|
-
controller,
|
|
1219
|
-
cooperative: () => {
|
|
1220
|
-
abort.abort(/* @__PURE__ */ new Error("the cooperative abort of the drain"));
|
|
1221
|
-
closeSource();
|
|
1222
|
-
},
|
|
1223
|
-
cut: (cause) => {
|
|
1224
|
-
abort.abort(new Error(cause));
|
|
1225
|
-
finish(cutEnd(cause));
|
|
1226
|
-
closeSource();
|
|
1227
|
-
}
|
|
1228
|
-
};
|
|
1229
|
-
flights.add(flight);
|
|
1230
|
-
/** This function takes the result of the work: a stream, or one value. */
|
|
1231
|
-
const take = (result) => {
|
|
1232
|
-
if (end !== null) return;
|
|
1233
|
-
if (isAsyncIterable$1(result)) {
|
|
1234
|
-
source = result[Symbol.asyncIterator]();
|
|
1235
|
-
if (abort.signal.aborted) closeSource();
|
|
1236
|
-
} else {
|
|
1237
|
-
unary = { value: result };
|
|
1238
|
-
settle(flight);
|
|
1239
|
-
}
|
|
1240
|
-
};
|
|
1241
|
-
let ready;
|
|
1242
|
-
try {
|
|
1243
|
-
const result = work(abort.signal);
|
|
1244
|
-
if (isAsyncIterable$1(result) || !isPromiseLike(result)) {
|
|
1245
|
-
take(result);
|
|
1246
|
-
ready = Promise.resolve();
|
|
1247
|
-
} else ready = Promise.resolve(result).then(take, failed);
|
|
1248
|
-
} catch (error) {
|
|
1249
|
-
failed(error);
|
|
1250
|
-
ready = Promise.resolve();
|
|
1251
|
-
}
|
|
1252
|
-
const next = async () => {
|
|
1253
|
-
if (ended) return null;
|
|
1254
|
-
if (end === null) await untilAbort(ready, interruption.signal, "interrupted");
|
|
1255
|
-
if (end === null && unary !== null) {
|
|
1256
|
-
const { value } = unary;
|
|
1257
|
-
unary = null;
|
|
1258
|
-
delivered = 1;
|
|
1259
|
-
end = {
|
|
1260
|
-
kind: "done",
|
|
1261
|
-
delivered
|
|
1262
|
-
};
|
|
1263
|
-
return {
|
|
1264
|
-
kind: "item",
|
|
1265
|
-
value
|
|
1266
|
-
};
|
|
1267
|
-
}
|
|
1268
|
-
if (end === null && source !== null) {
|
|
1269
|
-
const pending = source.next();
|
|
1270
|
-
let step;
|
|
1271
|
-
try {
|
|
1272
|
-
step = await untilAbort(Promise.resolve(pending), interruption.signal, "interrupted");
|
|
1273
|
-
} catch (error) {
|
|
1274
|
-
failed(error);
|
|
1275
|
-
step = "interrupted";
|
|
1276
|
-
}
|
|
1277
|
-
if (step !== "interrupted") {
|
|
1278
|
-
if (step.done !== true) {
|
|
1279
|
-
delivered += 1;
|
|
1280
|
-
return {
|
|
1281
|
-
kind: "item",
|
|
1282
|
-
value: step.value
|
|
1283
|
-
};
|
|
1284
|
-
}
|
|
1285
|
-
finish(abort.signal.aborted ? cutEnd("the shutdown ended the stream") : {
|
|
1286
|
-
kind: "done",
|
|
1287
|
-
delivered
|
|
1288
|
-
});
|
|
1289
|
-
}
|
|
1290
|
-
}
|
|
1291
|
-
ended = true;
|
|
1292
|
-
return end;
|
|
1293
|
-
};
|
|
1294
|
-
const stop = () => {
|
|
1295
|
-
if (end === null) {
|
|
1296
|
-
abort.abort(/* @__PURE__ */ new Error("the reader stopped the stream"));
|
|
1297
|
-
finish({
|
|
1298
|
-
kind: "done",
|
|
1299
|
-
delivered
|
|
1300
|
-
});
|
|
1301
|
-
closeSource();
|
|
1302
|
-
}
|
|
1303
|
-
ended = true;
|
|
1304
|
-
};
|
|
1305
|
-
return streamOf(next, stop);
|
|
1306
|
-
};
|
|
1307
|
-
return {
|
|
1308
|
-
request(controller, route, work, options = {}) {
|
|
1309
|
-
const operation = operationOf(controller, route, options.detail);
|
|
1310
|
-
const refusal = intake ? refuses(controller) : "the service stopped its intake";
|
|
1311
|
-
if (refusal !== null) {
|
|
1312
|
-
let given = false;
|
|
1313
|
-
const refused = {
|
|
1314
|
-
kind: "refused",
|
|
1315
|
-
status: 503,
|
|
1316
|
-
retryable: true,
|
|
1317
|
-
cause: refusal,
|
|
1318
|
-
delivered: 0
|
|
1319
|
-
};
|
|
1320
|
-
return streamOf(() => {
|
|
1321
|
-
if (given) return Promise.resolve(null);
|
|
1322
|
-
given = true;
|
|
1323
|
-
return Promise.resolve(refused);
|
|
1324
|
-
}, () => {
|
|
1325
|
-
given = true;
|
|
1326
|
-
});
|
|
1327
|
-
}
|
|
1328
|
-
return open(controller, operation, work);
|
|
1329
|
-
},
|
|
1330
|
-
inFlight(controller) {
|
|
1331
|
-
let count = 0;
|
|
1332
|
-
for (const flight of flights) if (controller === void 0 || flight.controller === controller) count += 1;
|
|
1333
|
-
return count;
|
|
1334
|
-
},
|
|
1335
|
-
stopIntake() {
|
|
1336
|
-
intake = false;
|
|
1337
|
-
},
|
|
1338
|
-
cooperativeAbort() {
|
|
1339
|
-
for (const flight of [...flights]) flight.cooperative();
|
|
1340
|
-
},
|
|
1341
|
-
drain(deadline) {
|
|
1342
|
-
if (flights.size === 0) return Promise.resolve();
|
|
1343
|
-
return new Promise((resolve) => {
|
|
1344
|
-
let finished = false;
|
|
1345
|
-
const done = () => {
|
|
1346
|
-
if (finished) return;
|
|
1347
|
-
finished = true;
|
|
1348
|
-
idle.delete(done);
|
|
1349
|
-
cancel();
|
|
1350
|
-
resolve();
|
|
1351
|
-
};
|
|
1352
|
-
idle.add(done);
|
|
1353
|
-
const cancel = clock.after(Math.max(0, deadline - clock.now()), () => {
|
|
1354
|
-
for (const flight of [...flights]) flight.cut("the drain reached its deadline");
|
|
1355
|
-
done();
|
|
1356
|
-
});
|
|
1357
|
-
});
|
|
1358
|
-
}
|
|
1359
|
-
};
|
|
1360
|
-
};
|
|
1361
|
-
//#endregion
|
|
1362
784
|
//#region src/lifecycle.ts
|
|
1363
785
|
/**
|
|
1364
786
|
* These are the stages of the shutdown, in their sequence. A declared phase with one of these
|