@mark1russell7/service 0.8.19 → 0.9.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/README.md +6 -2
- package/build/backoff.d.ts +44 -2
- package/build/{builder-zO48Vgoj.d.ts → builder.d.ts} +14 -101
- package/build/builder.js +499 -0
- package/build/builder.js.map +1 -0
- package/build/{call-context-DDhcVvn5.d.ts → call-context.d.ts} +3 -4
- package/build/channel.d.ts +1 -1
- package/build/channel.js.map +1 -1
- package/build/{core-BSzt4RI2.d.ts → client/core.d.ts} +32 -61
- package/build/{machine-BhVeHr_m.js → client/core.js} +4 -153
- package/build/client/core.js.map +1 -0
- package/build/{dial-ChwIcvd4.d.ts → client/dial.d.ts} +3 -4
- package/build/client/dial.js +52 -0
- package/build/client/dial.js.map +1 -0
- package/build/client/machine.js +29 -0
- package/build/client/machine.js.map +1 -0
- package/build/client/meets.d.ts +35 -0
- package/build/client/meets.js +78 -0
- package/build/client/meets.js.map +1 -0
- package/build/clock.d.ts +2 -2
- package/build/clock.js +1 -1
- package/build/conformance.d.ts +122 -0
- package/build/conformance.js +469 -0
- package/build/conformance.js.map +1 -0
- package/build/contract.d.ts +28 -0
- package/build/contract.js +184 -0
- package/build/contract.js.map +1 -0
- package/build/description.d.ts +161 -0
- package/build/description.js +11 -0
- package/build/description.js.map +1 -0
- package/build/drains.js +35 -0
- package/build/drains.js.map +1 -0
- package/build/events.d.ts +25 -0
- package/build/fakes.d.ts +43 -0
- package/build/fakes.js +93 -0
- package/build/fakes.js.map +1 -0
- package/build/grpc/client.d.ts +153 -0
- package/build/grpc/client.js +474 -0
- package/build/grpc/client.js.map +1 -0
- package/build/grpc/connection.d.ts +97 -0
- package/build/grpc/connection.js +59 -0
- package/build/grpc/connection.js.map +1 -0
- package/build/grpc/machine.d.ts +12 -0
- package/build/grpc/machine.js +149 -0
- package/build/grpc/machine.js.map +1 -0
- package/build/grpc/meets.d.ts +14 -0
- package/build/grpc/meets.js +105 -0
- package/build/grpc/meets.js.map +1 -0
- package/build/grpc/starting.d.ts +33 -0
- package/build/grpc/starting.js +89 -0
- package/build/grpc/starting.js.map +1 -0
- package/build/grpc.d.ts +9 -303
- package/build/grpc.js +7 -854
- package/build/health.d.ts +93 -0
- package/build/health.js +225 -0
- package/build/health.js.map +1 -0
- package/build/heartbeat.d.ts +6 -2
- package/build/heartbeat.js.map +1 -1
- package/build/host/discovery.d.ts +61 -0
- package/build/host/discovery.js +113 -0
- package/build/host/discovery.js.map +1 -0
- package/build/host/env.d.ts +14 -0
- package/build/host/env.js +57 -0
- package/build/host/env.js.map +1 -0
- package/build/host/fakes.d.ts +82 -0
- package/build/host/fakes.js +239 -0
- package/build/host/fakes.js.map +1 -0
- package/build/host/group.d.ts +142 -0
- package/build/host/group.js +524 -0
- package/build/host/group.js.map +1 -0
- package/build/host/install.d.ts +96 -0
- package/build/host/install.js +330 -0
- package/build/host/install.js.map +1 -0
- package/build/host/lock.d.ts +78 -0
- package/build/host/lock.js +190 -0
- package/build/host/lock.js.map +1 -0
- package/build/host/logs.d.ts +26 -0
- package/build/host/logs.js +48 -0
- package/build/host/logs.js.map +1 -0
- package/build/host/node.d.ts +14 -0
- package/build/host/node.js +309 -0
- package/build/host/node.js.map +1 -0
- package/build/{ports-B1ayTL-j.d.ts → host/ports.d.ts} +15 -16
- package/build/host/ports.js +17 -0
- package/build/host/ports.js.map +1 -0
- package/build/host/probes.d.ts +51 -0
- package/build/host/probes.js +139 -0
- package/build/host/probes.js.map +1 -0
- package/build/host/processes.d.ts +32 -0
- package/build/host/processes.js +63 -0
- package/build/host/processes.js.map +1 -0
- package/build/host/render.d.ts +84 -0
- package/build/host/render.js +235 -0
- package/build/host/render.js.map +1 -0
- package/build/host/watch.d.ts +90 -0
- package/build/host/watch.js +165 -0
- package/build/host/watch.js.map +1 -0
- package/build/host.d.ts +13 -671
- package/build/host.js +12 -2126
- package/build/http/answers.js +29 -0
- package/build/http/answers.js.map +1 -0
- package/build/http/fake-server.js +61 -0
- package/build/http/fake-server.js.map +1 -0
- package/build/http/outcome.d.ts +115 -0
- package/build/http/outcome.js +325 -0
- package/build/http/outcome.js.map +1 -0
- package/build/http/probes.d.ts +83 -0
- package/build/http/probes.js +232 -0
- package/build/http/probes.js.map +1 -0
- package/build/http/transport.d.ts +165 -0
- package/build/http/transport.js +518 -0
- package/build/http/transport.js.map +1 -0
- package/build/http-client/client.d.ts +121 -0
- package/build/http-client/client.js +330 -0
- package/build/http-client/client.js.map +1 -0
- package/build/http-client/connection.d.ts +78 -0
- package/build/http-client/connection.js +54 -0
- package/build/http-client/connection.js.map +1 -0
- package/build/http-client/machine.d.ts +14 -0
- package/build/http-client/machine.js +109 -0
- package/build/http-client/machine.js.map +1 -0
- package/build/http-client/meets.d.ts +11 -0
- package/build/http-client/meets.js +74 -0
- package/build/http-client/meets.js.map +1 -0
- package/build/http-client/session.d.ts +87 -0
- package/build/http-client/session.js +99 -0
- package/build/http-client/session.js.map +1 -0
- package/build/http-client/starting.d.ts +25 -0
- package/build/http-client/starting.js +65 -0
- package/build/http-client/starting.js.map +1 -0
- package/build/http-client.d.ts +9 -330
- package/build/http-client.js +8 -707
- package/build/http.d.ts +5 -353
- package/build/http.js +3 -1086
- package/build/identity.d.ts +16 -0
- package/build/identity.js +44 -0
- package/build/identity.js.map +1 -0
- package/build/index.d.ts +35 -412
- package/build/index.js +25 -3278
- package/build/knobs.d.ts +1 -0
- package/build/{knobs-BJcz6m01.js → knobs.js} +2 -34
- package/build/knobs.js.map +1 -0
- package/build/latch.d.ts +29 -0
- package/build/latch.js +122 -0
- package/build/latch.js.map +1 -0
- package/build/{rules-HyFCBStQ.d.ts → law.d.ts} +19 -37
- package/build/law.js +149 -0
- package/build/law.js.map +1 -0
- package/build/lifecycle.d.ts +144 -0
- package/build/lifecycle.js +411 -0
- package/build/lifecycle.js.map +1 -0
- package/build/limiter/machine.d.ts +40 -0
- package/build/limiter/machine.js +279 -0
- package/build/limiter/machine.js.map +1 -0
- package/build/{policy-CBcK2XGg.d.ts → limiter/policy.d.ts} +28 -29
- package/build/{policy-UYALkqVq.js → limiter/policy.js} +10 -9
- package/build/limiter/policy.js.map +1 -0
- package/build/{limiter-BNKnwzeY.d.ts → limiter/quota.d.ts} +32 -74
- package/build/{limiter-DrM0PQV9.js → limiter/quota.js} +3 -418
- package/build/limiter/quota.js.map +1 -0
- package/build/{store-DFwFKrsB.d.ts → limiter/store.d.ts} +10 -11
- package/build/limiter/store.js +145 -0
- package/build/limiter/store.js.map +1 -0
- package/build/limiter.d.ts +4 -3
- package/build/limiter.js +4 -2
- package/build/lint.d.ts +152 -0
- package/build/lint.js +576 -0
- package/build/lint.js.map +1 -0
- package/build/logger.d.ts +71 -0
- package/build/logger.js +183 -0
- package/build/logger.js.map +1 -0
- package/build/machines.d.ts +41 -0
- package/build/machines.js +451 -0
- package/build/machines.js.map +1 -0
- package/build/metrics.d.ts +53 -0
- package/build/metrics.js +111 -0
- package/build/metrics.js.map +1 -0
- package/build/mongodb/client.d.ts +131 -0
- package/build/mongodb/client.js +388 -0
- package/build/mongodb/client.js.map +1 -0
- package/build/mongodb/connection.d.ts +112 -0
- package/build/mongodb/connection.js +12 -0
- package/build/mongodb/connection.js.map +1 -0
- package/build/mongodb/machine.d.ts +14 -0
- package/build/mongodb/machine.js +122 -0
- package/build/mongodb/machine.js.map +1 -0
- package/build/mongodb/meets.d.ts +15 -0
- package/build/mongodb/meets.js +103 -0
- package/build/mongodb/meets.js.map +1 -0
- package/build/mongodb/starting.d.ts +29 -0
- package/build/mongodb/starting.js +77 -0
- package/build/mongodb/starting.js.map +1 -0
- package/build/mongodb.d.ts +8 -296
- package/build/mongodb.js +7 -682
- package/build/operation.d.ts +27 -0
- package/build/{operation-DF73RZOI.js → operation.js} +2 -2
- package/build/operation.js.map +1 -0
- package/build/otel.d.ts +2 -2
- package/build/pg/client.d.ts +96 -0
- package/build/pg/client.js +277 -0
- package/build/pg/client.js.map +1 -0
- package/build/pg/connection.d.ts +75 -0
- package/build/pg/connection.js +11 -0
- package/build/pg/connection.js.map +1 -0
- package/build/pg/machine.d.ts +13 -0
- package/build/pg/machine.js +111 -0
- package/build/pg/machine.js.map +1 -0
- package/build/pg/meets.d.ts +14 -0
- package/build/pg/meets.js +83 -0
- package/build/pg/meets.js.map +1 -0
- package/build/pg/starting.d.ts +26 -0
- package/build/pg/starting.js +62 -0
- package/build/pg/starting.js.map +1 -0
- package/build/pg.d.ts +8 -219
- package/build/pg.js +7 -525
- package/build/poller.d.ts +9 -0
- package/build/poller.js +154 -0
- package/build/poller.js.map +1 -0
- package/build/process.d.ts +114 -0
- package/build/process.js +170 -0
- package/build/process.js.map +1 -0
- package/build/profiles.d.ts +68 -0
- package/build/profiles.js +120 -0
- package/build/profiles.js.map +1 -0
- package/build/{queue-DXdw1vLO.d.ts → queue.d.ts} +16 -29
- package/build/queue.js +279 -0
- package/build/queue.js.map +1 -0
- package/build/rabbitmq/client.d.ts +144 -0
- package/build/rabbitmq/client.js +426 -0
- package/build/rabbitmq/client.js.map +1 -0
- package/build/rabbitmq/connection.d.ts +91 -0
- package/build/rabbitmq/connection.js +15 -0
- package/build/rabbitmq/connection.js.map +1 -0
- package/build/rabbitmq/machine.d.ts +13 -0
- package/build/rabbitmq/machine.js +159 -0
- package/build/rabbitmq/machine.js.map +1 -0
- package/build/rabbitmq/meets.d.ts +12 -0
- package/build/rabbitmq/meets.js +96 -0
- package/build/rabbitmq/meets.js.map +1 -0
- package/build/rabbitmq/starting.d.ts +27 -0
- package/build/rabbitmq/starting.js +67 -0
- package/build/rabbitmq/starting.js.map +1 -0
- package/build/rabbitmq.d.ts +8 -283
- package/build/rabbitmq.js +7 -742
- package/build/redis/client.d.ts +86 -0
- package/build/redis/client.js +285 -0
- package/build/redis/client.js.map +1 -0
- package/build/redis/connection.d.ts +57 -0
- package/build/redis/connection.js +11 -0
- package/build/redis/connection.js.map +1 -0
- package/build/redis/limiter.d.ts +43 -0
- package/build/redis/limiter.js +164 -0
- package/build/redis/limiter.js.map +1 -0
- package/build/redis/machine.d.ts +12 -0
- package/build/redis/machine.js +144 -0
- package/build/redis/machine.js.map +1 -0
- package/build/redis/meets.d.ts +11 -0
- package/build/redis/meets.js +75 -0
- package/build/redis/meets.js.map +1 -0
- package/build/redis/operations.d.ts +5 -0
- package/build/redis/operations.js +114 -0
- package/build/redis/operations.js.map +1 -0
- package/build/redis.d.ts +10 -207
- package/build/redis.js +9 -771
- package/build/requests.d.ts +66 -0
- package/build/requests.js +279 -0
- package/build/requests.js.map +1 -0
- package/build/retrier.d.ts +62 -0
- package/build/{retrier-Dnjhvxol.js → retrier.js} +2 -2
- package/build/retrier.js.map +1 -0
- package/build/routes.d.ts +35 -0
- package/build/routes.js +77 -0
- package/build/routes.js.map +1 -0
- package/build/rules.d.ts +19 -2
- package/build/stops.d.ts +51 -0
- package/build/stops.js +86 -0
- package/build/stops.js.map +1 -0
- package/build/testing.d.ts +8 -270
- package/build/testing.js +4 -1027
- package/build/testing.js.map +1 -1
- package/build/timing.d.ts +63 -0
- package/build/{timing-DZl36uXs.js → timing.js} +2 -2
- package/build/timing.js.map +1 -0
- package/build/units.d.ts +17 -0
- package/build/units.js +236 -0
- package/build/units.js.map +1 -0
- package/build/watchdog.d.ts +38 -0
- package/build/watchdog.js +57 -0
- package/build/watchdog.js.map +1 -0
- package/build/websocket/client.d.ts +136 -0
- package/build/websocket/client.js +422 -0
- package/build/websocket/client.js.map +1 -0
- package/build/websocket/connection.d.ts +83 -0
- package/build/websocket/connection.js +57 -0
- package/build/websocket/connection.js.map +1 -0
- package/build/websocket/machine.d.ts +12 -0
- package/build/websocket/machine.js +155 -0
- package/build/websocket/machine.js.map +1 -0
- package/build/websocket/meets.d.ts +14 -0
- package/build/websocket/meets.js +92 -0
- package/build/websocket/meets.js.map +1 -0
- package/build/websocket/starting.d.ts +25 -0
- package/build/websocket/starting.js +69 -0
- package/build/websocket/starting.js.map +1 -0
- package/build/websocket.d.ts +9 -264
- package/build/websocket.js +8 -774
- package/build/worker.d.ts +92 -0
- package/build/worker.js +153 -0
- package/build/worker.js.map +1 -0
- package/package.json +5 -4
- package/build/backoff-QTRGfkEK.d.ts +0 -45
- package/build/clock-DpVgpY4h.d.ts +0 -2
- package/build/grpc.js.map +0 -1
- package/build/host.js.map +0 -1
- package/build/http-client.js.map +0 -1
- package/build/http.js.map +0 -1
- package/build/index.js.map +0 -1
- package/build/knobs-BJcz6m01.js.map +0 -1
- package/build/lifecycle-D0J-zwZF.d.ts +0 -374
- package/build/limiter-DrM0PQV9.js.map +0 -1
- package/build/logger-B6drv9w0.d.ts +0 -488
- package/build/machine-BhVeHr_m.js.map +0 -1
- package/build/mongodb.js.map +0 -1
- package/build/operation-DF73RZOI.js.map +0 -1
- package/build/pg.js.map +0 -1
- package/build/policy-UYALkqVq.js.map +0 -1
- package/build/process-7R9M29wN.js +0 -437
- package/build/process-7R9M29wN.js.map +0 -1
- package/build/rabbitmq.js.map +0 -1
- package/build/redis.js.map +0 -1
- package/build/requests-DD2ALv0_.js +0 -583
- package/build/requests-DD2ALv0_.js.map +0 -1
- package/build/retrier-Dnjhvxol.js.map +0 -1
- package/build/timing-DZl36uXs.js.map +0 -1
- package/build/websocket.js.map +0 -1
package/build/redis.js
CHANGED
|
@@ -1,772 +1,10 @@
|
|
|
1
|
-
import {
|
|
2
|
-
import {
|
|
3
|
-
import {
|
|
4
|
-
import {
|
|
5
|
-
import {
|
|
6
|
-
import {
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
* service that does not use Redis does not need ioredis.
|
|
11
|
-
*/
|
|
12
|
-
const ioredis = async (options) => {
|
|
13
|
-
return new (await (import("ioredis"))).default(options);
|
|
14
|
-
};
|
|
15
|
-
// src/redis/meets.ts
|
|
16
|
-
/**
|
|
17
|
-
* This function gives the manifestations that a Redis client can meet, and the detectors of each, from
|
|
18
|
-
* its knobs. A reset or a FIN arrives on the next segment. A black hole and a host that is gone give
|
|
19
|
-
* no answer at all. A frozen process and a stalled server keep a kernel that acknowledges each
|
|
20
|
-
* segment. Thus TCP keepalive does not fire on them, and only the client's own timers find them.
|
|
21
|
-
*/
|
|
22
|
-
const meetings = (name, knobs, heartbeat) => {
|
|
23
|
-
const watchdog = (situation) => ({
|
|
24
|
-
by: "heartbeat_detection_time",
|
|
25
|
-
within: heartbeat,
|
|
26
|
-
situation,
|
|
27
|
-
effect: `the watchdog of ${name} closes the connection, and the client connects again`
|
|
28
|
-
});
|
|
29
|
-
const writeoff = {
|
|
30
|
-
by: "driver_writeoff_time",
|
|
31
|
-
within: knobs.driver_writeoff_time,
|
|
32
|
-
situation: "in flight",
|
|
33
|
-
effect: "ioredis destroys the socket of a command without data, and the client connects again"
|
|
34
|
-
};
|
|
35
|
-
const command = {
|
|
36
|
-
by: "request_timeout",
|
|
37
|
-
within: knobs.request_timeout,
|
|
38
|
-
situation: "in flight",
|
|
39
|
-
effect: "the command fails as indeterminate, and the state of the connection does not change"
|
|
40
|
-
};
|
|
41
|
-
const giveUp = kernelGiveUp(knobs.retrans_giveup_time);
|
|
42
|
-
const silent = (manifestation) => ({
|
|
43
|
-
manifestation,
|
|
44
|
-
what: WHAT[manifestation],
|
|
45
|
-
detectors: [
|
|
46
|
-
command,
|
|
47
|
-
writeoff,
|
|
48
|
-
watchdog("in flight"),
|
|
49
|
-
giveUp,
|
|
50
|
-
watchdog("idle"),
|
|
51
|
-
keepaliveSilent(knobs.keepalive_idle)
|
|
52
|
-
]
|
|
53
|
-
});
|
|
54
|
-
const acknowledged = (manifestation, what) => ({
|
|
55
|
-
manifestation,
|
|
56
|
-
what,
|
|
57
|
-
detectors: [
|
|
58
|
-
command,
|
|
59
|
-
writeoff,
|
|
60
|
-
watchdog("in flight"),
|
|
61
|
-
watchdog("idle"),
|
|
62
|
-
keepaliveAnswered()
|
|
63
|
-
]
|
|
64
|
-
});
|
|
65
|
-
return [
|
|
66
|
-
{
|
|
67
|
-
manifestation: "rst",
|
|
68
|
-
what: WHAT.rst,
|
|
69
|
-
detectors: [
|
|
70
|
-
immediate("in flight", "the command fails, and the client connects again, and each attempt is refused"),
|
|
71
|
-
watchdog("idle"),
|
|
72
|
-
keepaliveReset(knobs.keepalive_idle)
|
|
73
|
-
]
|
|
74
|
-
},
|
|
75
|
-
{
|
|
76
|
-
manifestation: "fin",
|
|
77
|
-
what: WHAT.fin,
|
|
78
|
-
detectors: [immediate("in flight", "the close arrives, and the command fails"), immediate("idle", "the close arrives")]
|
|
79
|
-
},
|
|
80
|
-
silent("blackhole"),
|
|
81
|
-
silent("freeze-host"),
|
|
82
|
-
acknowledged("freeze-process", WHAT["freeze-process"]),
|
|
83
|
-
acknowledged("app-stall", "the server stops its replies for a time, for example a long command, and its kernel stays up")
|
|
84
|
-
];
|
|
85
|
-
};
|
|
86
|
-
// src/redis/operations.ts
|
|
87
|
-
/**
|
|
88
|
-
* These are the Redis commands whose idempotency the client declares when the declaration does not
|
|
89
|
-
* say. A second issue of an idempotent command has the same effect. Thus the client can send it
|
|
90
|
-
* again after a failure that left its result unknown. The client does not send again a command
|
|
91
|
-
* that is not in this table: a declaration that uses it says its idempotency itself.
|
|
92
|
-
*/
|
|
93
|
-
const SAME_EFFECT = [
|
|
94
|
-
"PING",
|
|
95
|
-
"ECHO",
|
|
96
|
-
"GET",
|
|
97
|
-
"MGET",
|
|
98
|
-
"STRLEN",
|
|
99
|
-
"GETRANGE",
|
|
100
|
-
"EXISTS",
|
|
101
|
-
"TYPE",
|
|
102
|
-
"TTL",
|
|
103
|
-
"PTTL",
|
|
104
|
-
"EXPIRETIME",
|
|
105
|
-
"HGET",
|
|
106
|
-
"HMGET",
|
|
107
|
-
"HGETALL",
|
|
108
|
-
"HKEYS",
|
|
109
|
-
"HVALS",
|
|
110
|
-
"HLEN",
|
|
111
|
-
"HEXISTS",
|
|
112
|
-
"SMEMBERS",
|
|
113
|
-
"SISMEMBER",
|
|
114
|
-
"SMISMEMBER",
|
|
115
|
-
"SCARD",
|
|
116
|
-
"ZRANGE",
|
|
117
|
-
"ZRANGEBYSCORE",
|
|
118
|
-
"ZSCORE",
|
|
119
|
-
"ZMSCORE",
|
|
120
|
-
"ZRANK",
|
|
121
|
-
"ZCARD",
|
|
122
|
-
"ZCOUNT",
|
|
123
|
-
"LRANGE",
|
|
124
|
-
"LINDEX",
|
|
125
|
-
"LLEN",
|
|
126
|
-
"XRANGE",
|
|
127
|
-
"XREVRANGE",
|
|
128
|
-
"XLEN",
|
|
129
|
-
"SCAN",
|
|
130
|
-
"SSCAN",
|
|
131
|
-
"HSCAN",
|
|
132
|
-
"ZSCAN",
|
|
133
|
-
"DBSIZE",
|
|
134
|
-
"INFO",
|
|
135
|
-
"CLIENT ID",
|
|
136
|
-
"SET",
|
|
137
|
-
"MSET",
|
|
138
|
-
"SETEX",
|
|
139
|
-
"PSETEX",
|
|
140
|
-
"DEL",
|
|
141
|
-
"UNLINK",
|
|
142
|
-
"EXPIRE",
|
|
143
|
-
"PEXPIRE",
|
|
144
|
-
"EXPIREAT",
|
|
145
|
-
"PEXPIREAT",
|
|
146
|
-
"PERSIST",
|
|
147
|
-
"HSET",
|
|
148
|
-
"HDEL",
|
|
149
|
-
"SADD",
|
|
150
|
-
"SREM",
|
|
151
|
-
"ZADD",
|
|
152
|
-
"ZREM"
|
|
153
|
-
];
|
|
154
|
-
/**
|
|
155
|
-
* These commands change a state relative to the state before them. A second issue changes it again:
|
|
156
|
-
* an INCR that the client sends again after a timeout counts two times.
|
|
157
|
-
*/
|
|
158
|
-
const CHANGE = [
|
|
159
|
-
"INCR",
|
|
160
|
-
"INCRBY",
|
|
161
|
-
"INCRBYFLOAT",
|
|
162
|
-
"DECR",
|
|
163
|
-
"DECRBY",
|
|
164
|
-
"HINCRBY",
|
|
165
|
-
"HINCRBYFLOAT",
|
|
166
|
-
"ZINCRBY",
|
|
167
|
-
"APPEND",
|
|
168
|
-
"LPUSH",
|
|
169
|
-
"RPUSH",
|
|
170
|
-
"LPOP",
|
|
171
|
-
"RPOP",
|
|
172
|
-
"BLPOP",
|
|
173
|
-
"BRPOP",
|
|
174
|
-
"LMOVE",
|
|
175
|
-
"BLMOVE",
|
|
176
|
-
"SPOP",
|
|
177
|
-
"XADD",
|
|
178
|
-
"XACK",
|
|
179
|
-
"PUBLISH",
|
|
180
|
-
"EVAL",
|
|
181
|
-
"EVALSHA",
|
|
182
|
-
"FCALL",
|
|
183
|
-
"MULTI",
|
|
184
|
-
"EXEC",
|
|
185
|
-
"GETDEL",
|
|
186
|
-
"GETSET",
|
|
187
|
-
"SETNX"
|
|
188
|
-
];
|
|
189
|
-
/** These are the operations of the client: the idempotency of each command that the table names. */
|
|
190
|
-
const REDIS_OPERATIONS = Object.fromEntries([...SAME_EFFECT.map((command) => [command, { idempotent: true }]), ...CHANGE.map((command) => [command, { idempotent: false }])]);
|
|
191
|
-
/** This function gives the name of a command as the table writes it: upper case, and CLIENT ID with its subcommand. */
|
|
192
|
-
const commandName = (command, args) => {
|
|
193
|
-
const name = command.toUpperCase();
|
|
194
|
-
if ((name === "CLIENT" || name === "CONFIG" || name === "DEBUG") && typeof args[0] === "string") return `${name} ${args[0].toUpperCase()}`;
|
|
195
|
-
return name;
|
|
196
|
-
};
|
|
197
|
-
// src/redis/client.ts
|
|
198
|
-
/** This function gives the problems of a spec: the knobs that the client cannot use. */
|
|
199
|
-
const specProblems = (name, spec) => {
|
|
200
|
-
const problems = [];
|
|
201
|
-
const { knobs } = spec;
|
|
202
|
-
problems.push(...missesProblems(name, spec.heartbeatMisses, "watchdog"));
|
|
203
|
-
if (knobs.max_connection_age.value !== "infinite") problems.push(`${name} cannot replace its connection at an age: max_connection_age must be "infinite"`);
|
|
204
|
-
problems.push(...timeProblems(name, knobs));
|
|
205
|
-
if (ms(knobs.request_timeout) <= 0) problems.push(`${name}'s request_timeout is not above zero: each command must have a bound`);
|
|
206
|
-
if (ms(knobs.heartbeat_timeout) <= 0) problems.push(`${name}'s heartbeat_timeout is not above zero`);
|
|
207
|
-
problems.push(...retryProblems(name, knobs));
|
|
208
|
-
return problems;
|
|
209
|
-
};
|
|
210
|
-
/** This dial function opens a TCP connection, sends an inline PING, and waits for +PONG. */
|
|
211
|
-
const tcpDial = helloDial("PING\r\n", (received) => {
|
|
212
|
-
const reply = received.toString();
|
|
213
|
-
if (!reply.includes("\r\n")) return "more";
|
|
214
|
-
return reply.startsWith("+PONG") ? null : /* @__PURE__ */ new Error(`the reply was ${reply.trim()}`);
|
|
215
|
-
});
|
|
216
|
-
/**
|
|
217
|
-
* This function makes a Redis client. It is a dependency of a service, with ioredis under it:
|
|
218
|
-
* - It sets each timer of ioredis from the declaration, and it does not let ioredis send a command
|
|
219
|
-
* again (R5 §8).
|
|
220
|
-
* - It tells the outcome of a command in three values, and it settles each command in flight as
|
|
221
|
-
* indeterminate when its connection closes (C8, C10).
|
|
222
|
-
* - It records the server's id of each connection at `ready` (D1).
|
|
223
|
-
* - A watchdog sends a PING in the band of the connection, and it connects again after the misses
|
|
224
|
-
* of the declaration. It also starts a client that stopped (principle 19).
|
|
225
|
-
*
|
|
226
|
-
* The outcomes, the second try, the commands in flight, the bounded close and the checks come
|
|
227
|
-
* from the core of the clients. This entry gives what ioredis does in its own way.
|
|
228
|
-
*/
|
|
229
|
-
const redisClient = (name, spec, options = {}) => {
|
|
230
|
-
const problems = specProblems(name, spec);
|
|
231
|
-
if (problems.length > 0) throw new Error(problems.join("; "));
|
|
232
|
-
const { knobs } = spec;
|
|
233
|
-
const clock = options.clock ?? realClock;
|
|
234
|
-
const random = options.random ?? Math.random;
|
|
235
|
-
const create = options.create ?? ioredis;
|
|
236
|
-
const dial = options.dial ?? tcpDial;
|
|
237
|
-
const turn = options.turn ?? (() => new Promise((resolve) => setImmediate(resolve)));
|
|
238
|
-
const operations = operationTable(REDIS_OPERATIONS, spec.operations);
|
|
239
|
-
const requestTimeout = ms(knobs.request_timeout);
|
|
240
|
-
const detection = heartbeatDetection(knobs.heartbeat_interval, knobs.heartbeat_timeout, spec.heartbeatMisses);
|
|
241
|
-
const backoff = retryBackoff(knobs);
|
|
242
|
-
let connection = null;
|
|
243
|
-
let status = "none";
|
|
244
|
-
let closing = false;
|
|
245
|
-
const joins = [];
|
|
246
|
-
const flight = inFlight();
|
|
247
|
-
const readied = readyWait(clock, () => connection?.status === "ready");
|
|
248
|
-
const listeners = [];
|
|
249
|
-
const record = clientRecorder(options.record, clock, name);
|
|
250
|
-
let retrySince = null;
|
|
251
|
-
const retryStrategy = (times) => {
|
|
252
|
-
const now = clock.now();
|
|
253
|
-
retrySince ??= now;
|
|
254
|
-
if (now - retrySince > ms(knobs.client_retry_window)) {
|
|
255
|
-
record(status, status, "retry.window-ended");
|
|
256
|
-
return null;
|
|
257
|
-
}
|
|
258
|
-
return Math.round(backoffDelay(backoff, times, random));
|
|
259
|
-
};
|
|
260
|
-
const connectionOptions = {
|
|
261
|
-
host: spec.host,
|
|
262
|
-
port: spec.port,
|
|
263
|
-
...spec.db === void 0 ? {} : { db: spec.db },
|
|
264
|
-
...spec.username === void 0 ? {} : { username: spec.username },
|
|
265
|
-
...spec.password === void 0 ? {} : { password: spec.password },
|
|
266
|
-
lazyConnect: true,
|
|
267
|
-
connectTimeout: ms(knobs.connect_bound),
|
|
268
|
-
commandTimeout: requestTimeout,
|
|
269
|
-
socketTimeout: finiteMs(knobs.driver_writeoff_time),
|
|
270
|
-
keepAlive: finiteMs(knobs.keepalive_idle) ?? false,
|
|
271
|
-
enableOfflineQueue: false,
|
|
272
|
-
autoResendUnfulfilledCommands: false,
|
|
273
|
-
maxRetriesPerRequest: null,
|
|
274
|
-
enableReadyCheck: true,
|
|
275
|
-
connectionName: name,
|
|
276
|
-
retryStrategy
|
|
277
|
-
};
|
|
278
|
-
/** This function gives the outcome of a failed command: a reply of the server, a command that did not leave the client, or neither. */
|
|
279
|
-
const failed = (error) => {
|
|
280
|
-
const text = message(error);
|
|
281
|
-
if (error instanceof Error && error.name === "ReplyError") return {
|
|
282
|
-
outcome: "error",
|
|
283
|
-
error: text,
|
|
284
|
-
reply: true
|
|
285
|
-
};
|
|
286
|
-
if (/isn't writeable|enableOfflineQueue/i.test(text)) return notSent(text);
|
|
287
|
-
return {
|
|
288
|
-
outcome: "indeterminate",
|
|
289
|
-
error: text
|
|
290
|
-
};
|
|
291
|
-
};
|
|
292
|
-
const onReady = async () => {
|
|
293
|
-
retrySince = null;
|
|
294
|
-
const c = connection;
|
|
295
|
-
if (c === null) return;
|
|
296
|
-
const generation = joins.length + 1;
|
|
297
|
-
const at = clock.now();
|
|
298
|
-
const socket = c.stream;
|
|
299
|
-
let clientId = null;
|
|
300
|
-
try {
|
|
301
|
-
const reply = await c.call("CLIENT", "ID");
|
|
302
|
-
clientId = typeof reply === "number" ? reply : Number(reply);
|
|
303
|
-
if (!Number.isFinite(clientId)) clientId = null;
|
|
304
|
-
} catch {
|
|
305
|
-
clientId = null;
|
|
306
|
-
}
|
|
307
|
-
if (connection !== c || c.status !== "ready") return;
|
|
308
|
-
joins.push({
|
|
309
|
-
generation,
|
|
310
|
-
at,
|
|
311
|
-
clientId,
|
|
312
|
-
...socketEnds(socket)
|
|
313
|
-
});
|
|
314
|
-
record("ready", "ready", "join", `generation ${generation}, client id ${clientId ?? "unknown"}`);
|
|
315
|
-
readied.wake();
|
|
316
|
-
};
|
|
317
|
-
const onStatus = (to) => () => {
|
|
318
|
-
const from = status;
|
|
319
|
-
status = to;
|
|
320
|
-
record(from, to, `status.${to}`);
|
|
321
|
-
if (to === "close" || to === "end") flight.settleAll(`the connection closed with the command in flight (${from})`);
|
|
322
|
-
if (to === "ready") onReady();
|
|
323
|
-
};
|
|
324
|
-
const watch = (c) => {
|
|
325
|
-
for (const state of [
|
|
326
|
-
"connecting",
|
|
327
|
-
"connect",
|
|
328
|
-
"ready",
|
|
329
|
-
"close",
|
|
330
|
-
"reconnecting",
|
|
331
|
-
"end"
|
|
332
|
-
]) {
|
|
333
|
-
const listener = onStatus(state);
|
|
334
|
-
c.on(state, listener);
|
|
335
|
-
listeners.push([state, listener]);
|
|
336
|
-
}
|
|
337
|
-
const onError = (error) => record(status, status, "error", message(error));
|
|
338
|
-
c.on("error", onError);
|
|
339
|
-
listeners.push(["error", onError]);
|
|
340
|
-
};
|
|
341
|
-
const attempt = (command, args, signal) => {
|
|
342
|
-
const c = connection;
|
|
343
|
-
if (c === null) return Promise.resolve(notSent(`${name} has not connected`));
|
|
344
|
-
if (c.status !== "ready") return Promise.resolve(notSent(`${name} is ${c.status}, not ready: the command did not leave the client`));
|
|
345
|
-
if (signal?.aborted) return Promise.resolve(notSent("the caller aborted the command before it left the client"));
|
|
346
|
-
return flight.track(signal, "the caller abandoned the command after it was sent", (settle) => {
|
|
347
|
-
c.call(command, ...args).then((value) => settle({
|
|
348
|
-
outcome: "ok",
|
|
349
|
-
value
|
|
350
|
-
}), (error) => settle(failed(error)));
|
|
351
|
-
});
|
|
352
|
-
};
|
|
353
|
-
const send = (command, args = [], sendOptions = {}) => sendWithReissue({
|
|
354
|
-
clock,
|
|
355
|
-
bound: requestTimeout,
|
|
356
|
-
operation: operations[commandName(command, args)],
|
|
357
|
-
options: sendOptions,
|
|
358
|
-
attempt: (signal) => attempt(command, args, signal),
|
|
359
|
-
beforeAgain: (left, signal) => readied.wait(left, signal),
|
|
360
|
-
onReissue: () => record(status, status, "command.reissue", commandName(command, args))
|
|
361
|
-
});
|
|
362
|
-
let watchdog = null;
|
|
363
|
-
const startWatchdog = () => heartbeat({
|
|
364
|
-
interval: ms(knobs.heartbeat_interval),
|
|
365
|
-
timeout: ms(knobs.heartbeat_timeout),
|
|
366
|
-
misses: spec.heartbeatMisses,
|
|
367
|
-
clock,
|
|
368
|
-
turn,
|
|
369
|
-
overlap: true,
|
|
370
|
-
due: () => {
|
|
371
|
-
const c = connection;
|
|
372
|
-
if (c === null || closing) return false;
|
|
373
|
-
if (c.status === "end") {
|
|
374
|
-
record(status, status, "watchdog.revive");
|
|
375
|
-
retrySince = null;
|
|
376
|
-
c.connect().catch(() => {});
|
|
377
|
-
return false;
|
|
378
|
-
}
|
|
379
|
-
return c.status === "ready";
|
|
380
|
-
},
|
|
381
|
-
ping: (signal) => attempt("PING", [], signal).then((result) => result.outcome === "ok" || result.outcome === "error" && result.reply),
|
|
382
|
-
live: () => connection?.status === "ready" && !closing,
|
|
383
|
-
onMiss: (misses) => record(status, status, "watchdog.miss", `${misses} of ${spec.heartbeatMisses}`),
|
|
384
|
-
onDead: () => {
|
|
385
|
-
record(status, status, "watchdog.reconnect");
|
|
386
|
-
connection?.disconnect(true);
|
|
387
|
-
}
|
|
388
|
-
});
|
|
389
|
-
const handle = {
|
|
390
|
-
async connect(signal) {
|
|
391
|
-
closing = false;
|
|
392
|
-
const c = await create(connectionOptions);
|
|
393
|
-
connection = c;
|
|
394
|
-
watch(c);
|
|
395
|
-
if (signal.aborted) throw new Error(`${name}: the service stopped before the connect`);
|
|
396
|
-
await c.connect().catch((error) => record(status, status, "connect.failed", message(error)));
|
|
397
|
-
watchdog?.stop();
|
|
398
|
-
watchdog = startWatchdog();
|
|
399
|
-
},
|
|
400
|
-
async close() {
|
|
401
|
-
closing = true;
|
|
402
|
-
watchdog?.stop();
|
|
403
|
-
watchdog = null;
|
|
404
|
-
const c = connection;
|
|
405
|
-
if (c === null) return;
|
|
406
|
-
const startedClose = clock.now();
|
|
407
|
-
if (!await withinBound(clock, ms(knobs.close_bound), c.quit())) record(status, status, "close.bound");
|
|
408
|
-
if (c.status !== "end") c.disconnect(false);
|
|
409
|
-
const ended = (within) => c.status === "end" ? Promise.resolve() : new Promise((resolve) => {
|
|
410
|
-
let stop = () => {};
|
|
411
|
-
const end = () => {
|
|
412
|
-
c.off("end", end);
|
|
413
|
-
stop();
|
|
414
|
-
resolve();
|
|
415
|
-
};
|
|
416
|
-
c.on("end", end);
|
|
417
|
-
stop = clock.after(Math.max(0, within), end);
|
|
418
|
-
});
|
|
419
|
-
await ended(ms(knobs.close_bound) - (clock.now() - startedClose));
|
|
420
|
-
if (c.status !== "end") {
|
|
421
|
-
record(status, status, "close.destroy");
|
|
422
|
-
c.stream?.destroy?.();
|
|
423
|
-
await ended(0);
|
|
424
|
-
}
|
|
425
|
-
for (const [event, listener] of listeners) c.off(event, listener);
|
|
426
|
-
listeners.length = 0;
|
|
427
|
-
flight.settleAll("the client closed with the command in flight");
|
|
428
|
-
status = "end";
|
|
429
|
-
}
|
|
430
|
-
};
|
|
431
|
-
const declaration = {
|
|
432
|
-
system: spec.system,
|
|
433
|
-
knobs: {
|
|
434
|
-
connect_bound: knobs.connect_bound,
|
|
435
|
-
request_timeout: knobs.request_timeout,
|
|
436
|
-
driver_writeoff_time: knobs.driver_writeoff_time,
|
|
437
|
-
keepalive_idle: knobs.keepalive_idle,
|
|
438
|
-
heartbeat_interval: knobs.heartbeat_interval,
|
|
439
|
-
heartbeat_timeout: knobs.heartbeat_timeout,
|
|
440
|
-
heartbeat_detection_time: detection,
|
|
441
|
-
client_retry_window: knobs.client_retry_window,
|
|
442
|
-
retry_base: knobs.retry_base,
|
|
443
|
-
retry_cap: knobs.retry_cap,
|
|
444
|
-
close_bound: knobs.close_bound,
|
|
445
|
-
max_connection_age: knobs.max_connection_age,
|
|
446
|
-
retrans_giveup_time: knobs.retrans_giveup_time
|
|
447
|
-
},
|
|
448
|
-
operations
|
|
449
|
-
};
|
|
450
|
-
const checks = (checkOptions) => stepChecks(name, checkOptions, {
|
|
451
|
-
"local-state": localStateCheck(() => connection?.status === "ready"),
|
|
452
|
-
"round-trip": roundTripCheck((sendOptions) => send("PING", [], sendOptions)),
|
|
453
|
-
dial: dialCheck(dial, spec.host, spec.port)
|
|
454
|
-
});
|
|
455
|
-
return {
|
|
456
|
-
name,
|
|
457
|
-
declaration,
|
|
458
|
-
handle,
|
|
459
|
-
meets: meetings(name, knobs, detection),
|
|
460
|
-
get status() {
|
|
461
|
-
return connection === null ? "none" : connection.status;
|
|
462
|
-
},
|
|
463
|
-
joins,
|
|
464
|
-
send,
|
|
465
|
-
checks,
|
|
466
|
-
attach: (svc, checkOptions) => attachClient(svc, name, declaration, handle, checks(checkOptions)),
|
|
467
|
-
problems: (description) => clientProblems(name, description, clientBounds(knobs))
|
|
468
|
-
};
|
|
469
|
-
};
|
|
470
|
-
// src/redis/limiter.ts
|
|
471
|
-
/**
|
|
472
|
-
* This script decides one take in one atomic step of Redis. It does the arithmetic of `decide`, and
|
|
473
|
-
* it reads `TIME` of the server, the one clock of all replicas. A test can give the time in its place.
|
|
474
|
-
* - KEYS[1] is the state of the key. KEYS[2] is the index of the limiter, a sorted set of its keys
|
|
475
|
-
* by the time at which each key is free.
|
|
476
|
-
* - ARGV is the type of the rule, its two numbers, the cap of keys and the key. Then it has the
|
|
477
|
-
* time or "", and the units of the take. The store refuses a take over the capacity before the script, thus the units
|
|
478
|
-
* are at most the capacity.
|
|
479
|
-
* - The state of a bucket is its full time. The state of a window is the time of each take in it.
|
|
480
|
-
* - The state expires at its free time (PX), and the index lets the cap count only busy keys.
|
|
481
|
-
* - The reply is `{1, remaining}`, or `{0, retryAfter, cause}` with the time as text. Redis gives
|
|
482
|
-
* each Lua number as a whole number, thus the fraction of a millisecond goes as text.
|
|
483
|
-
*/
|
|
484
|
-
const LIMITER_SCRIPT = `
|
|
485
|
-
local kind = ARGV[1]
|
|
486
|
-
local a = tonumber(ARGV[2])
|
|
487
|
-
local b = tonumber(ARGV[3])
|
|
488
|
-
local cap = tonumber(ARGV[4])
|
|
489
|
-
local member = ARGV[5]
|
|
490
|
-
local units = tonumber(ARGV[7])
|
|
491
|
-
local now
|
|
492
|
-
if ARGV[6] == '' then
|
|
493
|
-
local t = redis.call('TIME')
|
|
494
|
-
now = tonumber(t[1]) * 1000 + tonumber(t[2]) / 1000
|
|
495
|
-
else
|
|
496
|
-
now = tonumber(ARGV[6])
|
|
497
|
-
end
|
|
498
|
-
local TOL = 1e-6
|
|
499
|
-
local function num(x) return string.format('%.6f', x) end
|
|
500
|
-
local raw = redis.call('GET', KEYS[1])
|
|
501
|
-
local full = nil
|
|
502
|
-
local stamps = nil
|
|
503
|
-
if raw then
|
|
504
|
-
if kind == 'token-bucket' then
|
|
505
|
-
full = tonumber(raw)
|
|
506
|
-
else
|
|
507
|
-
stamps = {}
|
|
508
|
-
for x in string.gmatch(raw, '[^,]+') do stamps[#stamps + 1] = tonumber(x) end
|
|
509
|
-
end
|
|
510
|
-
end
|
|
511
|
-
local function freeAt()
|
|
512
|
-
if kind == 'token-bucket' then return full end
|
|
513
|
-
if #stamps == 0 then return -math.huge end
|
|
514
|
-
return stamps[#stamps] + b
|
|
515
|
-
end
|
|
516
|
-
if raw and freeAt() <= now then
|
|
517
|
-
redis.call('DEL', KEYS[1])
|
|
518
|
-
redis.call('ZREM', KEYS[2], member)
|
|
519
|
-
raw = nil
|
|
520
|
-
full = nil
|
|
521
|
-
stamps = nil
|
|
522
|
-
end
|
|
523
|
-
if not raw then
|
|
524
|
-
if redis.call('ZCARD', KEYS[2]) >= cap then
|
|
525
|
-
redis.call('ZREMRANGEBYSCORE', KEYS[2], '-inf', num(now))
|
|
526
|
-
if redis.call('ZCARD', KEYS[2]) >= cap then
|
|
527
|
-
local first = redis.call('ZRANGE', KEYS[2], 0, 0, 'WITHSCORES')
|
|
528
|
-
return {0, num(tonumber(first[2]) - now), 'cap'}
|
|
529
|
-
end
|
|
530
|
-
end
|
|
531
|
-
end
|
|
532
|
-
local remaining
|
|
533
|
-
local state
|
|
534
|
-
if kind == 'token-bucket' then
|
|
535
|
-
local span = a * b
|
|
536
|
-
local base = now
|
|
537
|
-
if full and full > now then base = full end
|
|
538
|
-
local nxt = base + a * units
|
|
539
|
-
if nxt - now > span + TOL then
|
|
540
|
-
return {0, num(nxt - span - now), 'limit'}
|
|
541
|
-
end
|
|
542
|
-
remaining = math.floor((span - (nxt - now)) / a + TOL)
|
|
543
|
-
if remaining < 0 then remaining = 0 end
|
|
544
|
-
full = nxt
|
|
545
|
-
state = num(full)
|
|
546
|
-
else
|
|
547
|
-
local live = {}
|
|
548
|
-
for _, stamp in ipairs(stamps or {}) do
|
|
549
|
-
if stamp + b > now + TOL then live[#live + 1] = stamp end
|
|
550
|
-
end
|
|
551
|
-
if #live + units > a then
|
|
552
|
-
return {0, num(live[#live + units - a] + b - now), 'limit'}
|
|
553
|
-
end
|
|
554
|
-
for _ = 1, units do live[#live + 1] = now end
|
|
555
|
-
remaining = a - #live
|
|
556
|
-
stamps = live
|
|
557
|
-
local parts = {}
|
|
558
|
-
for i, stamp in ipairs(live) do parts[i] = num(stamp) end
|
|
559
|
-
state = table.concat(parts, ',')
|
|
560
|
-
end
|
|
561
|
-
local free = freeAt()
|
|
562
|
-
local ttl = math.ceil(free - now)
|
|
563
|
-
if ttl < 1 then ttl = 1 end
|
|
564
|
-
redis.call('SET', KEYS[1], state, 'PX', ttl)
|
|
565
|
-
redis.call('ZADD', KEYS[2], num(free), member)
|
|
566
|
-
if redis.call('PTTL', KEYS[2]) < ttl then redis.call('PEXPIRE', KEYS[2], ttl) end
|
|
567
|
-
return {1, remaining}
|
|
568
|
-
`;
|
|
569
|
-
/**
|
|
570
|
-
* This function makes a store that the replicas share, in Redis 7. Each take is one `EVAL` of
|
|
571
|
-
* `LIMITER_SCRIPT`, with all of its units. The client does not send it again, because a second take
|
|
572
|
-
* counts two times.
|
|
573
|
-
* - The names of the keys have the limiter in braces, a hash tag. Thus the state and the index of a
|
|
574
|
-
* limiter are in one slot of a Redis Cluster.
|
|
575
|
-
* - Each state expires at the time that the key is free, thus the eviction is exact, as in the memory
|
|
576
|
-
* store. The index of the limiter bounds the count of busy keys to `maxKeys`.
|
|
577
|
-
* - A reply of an error, an outcome that is not certain, or a reply of another shape rejects the
|
|
578
|
-
* take of the store. `keyedLimiter` then applies the `onStoreFailure` of the spec.
|
|
579
|
-
* - `TIME` is the wall clock of the Redis host, not a monotonic clock. Thus a step of that clock
|
|
580
|
-
* moves the counts of each key. The replicas still share one clock.
|
|
581
|
-
*/
|
|
582
|
-
const redisLimiterStore = (client, options) => {
|
|
583
|
-
if (/[{}]/.test(options.prefix)) throw new Error(`the prefix ${options.prefix} of a limiter store has a brace: the store puts the hash tag in it`);
|
|
584
|
-
return {
|
|
585
|
-
kind: "shared",
|
|
586
|
-
async take({ limiter, key, rule, maxKeys, units }) {
|
|
587
|
-
const n = unitsOf(units);
|
|
588
|
-
const over = overCapacity(rule, n);
|
|
589
|
-
if (over !== void 0) return over;
|
|
590
|
-
const base = `${options.prefix}{${limiter}}`;
|
|
591
|
-
const numbers = rule.type === "token-bucket" ? [rule.interval, rule.burst] : [rule.limit, rule.window];
|
|
592
|
-
const now = options.clock === void 0 ? "" : String(options.clock.now());
|
|
593
|
-
const sent = await client.send("EVAL", [
|
|
594
|
-
LIMITER_SCRIPT,
|
|
595
|
-
2,
|
|
596
|
-
`${base}:key:${key}`,
|
|
597
|
-
`${base}:keys`,
|
|
598
|
-
rule.type,
|
|
599
|
-
String(numbers[0]),
|
|
600
|
-
String(numbers[1]),
|
|
601
|
-
String(maxKeys),
|
|
602
|
-
key,
|
|
603
|
-
now,
|
|
604
|
-
String(n)
|
|
605
|
-
]);
|
|
606
|
-
if (sent.outcome !== "ok") throw new Error(`the limiter store ${limiter} failed (${sent.outcome}): ${sent.error}`);
|
|
607
|
-
return replyOf(limiter, sent.value);
|
|
608
|
-
}
|
|
609
|
-
};
|
|
610
|
-
};
|
|
611
|
-
/** This function gives the decision of a reply of `LIMITER_SCRIPT`. A reply of another shape throws. */
|
|
612
|
-
const replyOf = (limiter, reply) => {
|
|
613
|
-
if (Array.isArray(reply) && Number(reply[0]) === 1 && reply.length === 2) return {
|
|
614
|
-
allowed: true,
|
|
615
|
-
remaining: Number(reply[1])
|
|
616
|
-
};
|
|
617
|
-
if (Array.isArray(reply) && Number(reply[0]) === 0 && reply.length === 3 && (reply[2] === "limit" || reply[2] === "cap")) {
|
|
618
|
-
const retryAfter = Number(reply[1]);
|
|
619
|
-
if (Number.isFinite(retryAfter) && retryAfter > 0) return {
|
|
620
|
-
allowed: false,
|
|
621
|
-
retryAfter: {
|
|
622
|
-
value: retryAfter,
|
|
623
|
-
unit: "ms"
|
|
624
|
-
},
|
|
625
|
-
cause: reply[2]
|
|
626
|
-
};
|
|
627
|
-
}
|
|
628
|
-
throw new Error(`the limiter store ${limiter} gave a reply that is not a decision: ${JSON.stringify(reply)}`);
|
|
629
|
-
};
|
|
630
|
-
// src/redis/machine.ts
|
|
631
|
-
/** The states where the connection exists or tries to exist, thus where it can close. */
|
|
632
|
-
const LIVE = [
|
|
633
|
-
"connecting",
|
|
634
|
-
"connect",
|
|
635
|
-
"ready",
|
|
636
|
-
"reconnecting"
|
|
637
|
-
];
|
|
638
|
-
const ANY = [
|
|
639
|
-
"none",
|
|
640
|
-
"connecting",
|
|
641
|
-
"connect",
|
|
642
|
-
"ready",
|
|
643
|
-
"close",
|
|
644
|
-
"reconnecting",
|
|
645
|
-
"end"
|
|
646
|
-
];
|
|
647
|
-
/**
|
|
648
|
-
* The Redis client as a `mark1russell7.machine/1` document. Its states are the states of ioredis.
|
|
649
|
-
* The wrapper adds its own events: the join at each `ready`, and the misses of its watchdog. It also
|
|
650
|
-
* adds a command that it sends again, and the bound of its close. A test records the transitions of the
|
|
651
|
-
* client and replays them against this document.
|
|
652
|
-
*
|
|
653
|
-
* The state `ready` hides states that the client cannot see (D4). In a black hole, ioredis stays
|
|
654
|
-
* `ready` while the kernel retransmits, until `driver_writeoff_time` or the watchdog ends that
|
|
655
|
-
* hidden interval. A timer of this document bounds each hidden interval.
|
|
656
|
-
*/
|
|
657
|
-
const REDIS_CLIENT_MACHINE = {
|
|
658
|
-
format: "mark1russell7.machine/1",
|
|
659
|
-
id: "service.machine.redis-client",
|
|
660
|
-
title: "The Redis client of the service, over ioredis 6",
|
|
661
|
-
layer: "driver",
|
|
662
|
-
version: "0.2.0",
|
|
663
|
-
sources: [{
|
|
664
|
-
ref: "packages/service/src/redis (the client implements this document)",
|
|
665
|
-
rung: "implemented"
|
|
666
|
-
}],
|
|
667
|
-
states: {
|
|
668
|
-
none: {
|
|
669
|
-
initial: true,
|
|
670
|
-
note: "the client exists, and its connection does not"
|
|
671
|
-
},
|
|
672
|
-
connecting: { note: "a TCP connection attempt" },
|
|
673
|
-
connect: { note: "the handshake: HELLO, then the ready check (INFO)" },
|
|
674
|
-
ready: { note: "it can hide a black hole or a frozen server, until a timer of the client ends it" },
|
|
675
|
-
close: { note: "the socket closed. Each command in flight settles as indeterminate" },
|
|
676
|
-
reconnecting: { note: "the delay of the retry strategy, which doubles to its cap" },
|
|
677
|
-
end: { note: "the retry window ended, or the service closed the client. The watchdog starts it again" }
|
|
678
|
-
},
|
|
679
|
-
events: {
|
|
680
|
-
"status.connecting": { kind: "local" },
|
|
681
|
-
"status.connect": { kind: "rx" },
|
|
682
|
-
"status.ready": { kind: "rx" },
|
|
683
|
-
"status.close": { kind: "rx" },
|
|
684
|
-
"status.reconnecting": { kind: "timer" },
|
|
685
|
-
"status.end": { kind: "local" },
|
|
686
|
-
join: {
|
|
687
|
-
kind: "rx",
|
|
688
|
-
note: "the reply of CLIENT ID at ready (D1)"
|
|
689
|
-
},
|
|
690
|
-
error: { kind: "rx" },
|
|
691
|
-
"connect.failed": { kind: "local" },
|
|
692
|
-
"command.written": { kind: "tx" },
|
|
693
|
-
"command.reissue": { kind: "local" },
|
|
694
|
-
data: { kind: "rx" },
|
|
695
|
-
heartbeat: { kind: "timer" },
|
|
696
|
-
"watchdog.miss": { kind: "local" },
|
|
697
|
-
"watchdog.reconnect": { kind: "local" },
|
|
698
|
-
"watchdog.revive": { kind: "local" },
|
|
699
|
-
"retry.window-ended": { kind: "local" },
|
|
700
|
-
"close.request": { kind: "local" },
|
|
701
|
-
"close.bound": { kind: "timer" },
|
|
702
|
-
"close.destroy": {
|
|
703
|
-
kind: "local",
|
|
704
|
-
note: "the bound destroys a socket that a silent server does not close"
|
|
705
|
-
}
|
|
706
|
-
},
|
|
707
|
-
knobs: {
|
|
708
|
-
connect_bound: clientKnob("connectTimeout"),
|
|
709
|
-
driver_writeoff_time: clientKnob("socketTimeout"),
|
|
710
|
-
heartbeat_interval: clientKnob("watchdog.interval"),
|
|
711
|
-
close_bound: clientKnob("close.bound")
|
|
712
|
-
},
|
|
713
|
-
timers: {
|
|
714
|
-
connect_timeout: {
|
|
715
|
-
duration: {
|
|
716
|
-
knob: "connect_bound",
|
|
717
|
-
unit: "s"
|
|
718
|
-
},
|
|
719
|
-
arms: ["enter:connecting"],
|
|
720
|
-
cancels: ["status.connect", "status.close"],
|
|
721
|
-
fires: "status.close",
|
|
722
|
-
note: "ioredis destroys the attempt"
|
|
723
|
-
},
|
|
724
|
-
writeoff: {
|
|
725
|
-
duration: {
|
|
726
|
-
knob: "driver_writeoff_time",
|
|
727
|
-
unit: "s"
|
|
728
|
-
},
|
|
729
|
-
arms: ["command.written"],
|
|
730
|
-
cancels: ["data", "status.close"],
|
|
731
|
-
fires: "status.close",
|
|
732
|
-
note: "ioredis destroys a socket that gives no data while a command waits: it ends the hidden interval of a black hole"
|
|
733
|
-
},
|
|
734
|
-
heartbeat: {
|
|
735
|
-
duration: {
|
|
736
|
-
knob: "heartbeat_interval",
|
|
737
|
-
unit: "s"
|
|
738
|
-
},
|
|
739
|
-
arms: ["enter:ready", "heartbeat"],
|
|
740
|
-
cancels: ["status.close", "status.end"],
|
|
741
|
-
fires: "heartbeat",
|
|
742
|
-
note: "a PING on the connection of the application"
|
|
743
|
-
},
|
|
744
|
-
close: closeTimer()
|
|
745
|
-
},
|
|
746
|
-
transitions: [
|
|
747
|
-
transition("connect", [
|
|
748
|
-
"none",
|
|
749
|
-
"reconnecting",
|
|
750
|
-
"end",
|
|
751
|
-
"close"
|
|
752
|
-
], "status.connecting", "connecting"),
|
|
753
|
-
transition("tcp", "connecting", "status.connect", "connect"),
|
|
754
|
-
transition("ready", "connect", "status.ready", "ready"),
|
|
755
|
-
transition("close", [...LIVE], "status.close", "close"),
|
|
756
|
-
transition("retry", "close", "status.reconnecting", "reconnecting"),
|
|
757
|
-
transition("end", [...LIVE, "close"], "status.end", "end"),
|
|
758
|
-
transition("join", "ready", "join", "ready"),
|
|
759
|
-
transition("miss", "ready", "watchdog.miss", "ready"),
|
|
760
|
-
transition("watchdog-reconnect", "ready", "watchdog.reconnect", "ready"),
|
|
761
|
-
transition("revive", "end", "watchdog.revive", "end"),
|
|
762
|
-
transition("window-ended", "close", "retry.window-ended", "close"),
|
|
763
|
-
...ANY.map((state) => transition(`error-${state}`, state, "error", state)),
|
|
764
|
-
...ANY.map((state) => transition(`connect-failed-${state}`, state, "connect.failed", state)),
|
|
765
|
-
...ANY.map((state) => transition(`reissue-${state}`, state, "command.reissue", state)),
|
|
766
|
-
...ANY.map((state) => transition(`close-bound-${state}`, state, "close.bound", state, { timer: "close" })),
|
|
767
|
-
...ANY.map((state) => transition(`close-destroy-${state}`, state, "close.destroy", state))
|
|
768
|
-
]
|
|
769
|
-
};
|
|
1
|
+
import { MANIFESTATIONS, firstDetector, unwatched } from "./client/meets.js";
|
|
2
|
+
import { redisClient, specProblems, tcpDial } from "./redis/client.js";
|
|
3
|
+
import { retryBackoff } from "./client/core.js";
|
|
4
|
+
import { heartbeatDetection } from "./timing.js";
|
|
5
|
+
import { ioredis } from "./redis/connection.js";
|
|
6
|
+
import { meetings } from "./redis/meets.js";
|
|
7
|
+
import { LIMITER_SCRIPT, redisLimiterStore } from "./redis/limiter.js";
|
|
8
|
+
import { REDIS_CLIENT_MACHINE } from "./redis/machine.js";
|
|
9
|
+
import { REDIS_OPERATIONS, commandName } from "./redis/operations.js";
|
|
770
10
|
export { LIMITER_SCRIPT, MANIFESTATIONS, REDIS_CLIENT_MACHINE, REDIS_OPERATIONS, commandName, firstDetector, heartbeatDetection, ioredis, meetings, redisClient, redisLimiterStore, retryBackoff, specProblems, tcpDial, unwatched };
|
|
771
|
-
|
|
772
|
-
//# sourceMappingURL=redis.js.map
|