@mark1russell7/service 0.8.18 → 0.8.20
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 +19 -0
- 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/{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 +1 -1
- package/build/conformance.d.ts +122 -0
- package/build/conformance.js +470 -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 +223 -0
- package/build/health.js.map +1 -0
- package/build/heartbeat.d.ts +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 +517 -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 +517 -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 +24 -3277
- 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-Cm8nC-Gd.d.ts → limiter/quota.d.ts} +50 -76
- package/build/{limiter-Bgy6Gmvl.js → limiter/quota.js} +32 -425
- 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 +152 -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 +34 -0
- package/build/watchdog.js +53 -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 +4 -3
- 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-Bgy6Gmvl.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
|
@@ -0,0 +1,164 @@
|
|
|
1
|
+
import { overCapacity, unitsOf } from "../limiter/policy.js";
|
|
2
|
+
// src/redis/limiter.ts
|
|
3
|
+
/**
|
|
4
|
+
* This script decides one take in one atomic step of Redis. It does the arithmetic of `decide`, and
|
|
5
|
+
* it reads `TIME` of the server, the one clock of all replicas. A test can give the time in its place.
|
|
6
|
+
* - KEYS[1] is the state of the key. KEYS[2] is the index of the limiter, a sorted set of its keys
|
|
7
|
+
* by the time at which each key is free.
|
|
8
|
+
* - ARGV is the type of the rule, its two numbers, the cap of keys and the key. Then it has the
|
|
9
|
+
* time or "", and the units of the take. The store refuses a take over the capacity before the script, thus the units
|
|
10
|
+
* are at most the capacity.
|
|
11
|
+
* - The state of a bucket is its full time. The state of a window is the time of each take in it.
|
|
12
|
+
* - The state expires at its free time (PX), and the index lets the cap count only busy keys.
|
|
13
|
+
* - The reply is `{1, remaining}`, or `{0, retryAfter, cause}` with the time as text. Redis gives
|
|
14
|
+
* each Lua number as a whole number, thus the fraction of a millisecond goes as text.
|
|
15
|
+
*/
|
|
16
|
+
const LIMITER_SCRIPT = `
|
|
17
|
+
local kind = ARGV[1]
|
|
18
|
+
local a = tonumber(ARGV[2])
|
|
19
|
+
local b = tonumber(ARGV[3])
|
|
20
|
+
local cap = tonumber(ARGV[4])
|
|
21
|
+
local member = ARGV[5]
|
|
22
|
+
local units = tonumber(ARGV[7])
|
|
23
|
+
local now
|
|
24
|
+
if ARGV[6] == '' then
|
|
25
|
+
local t = redis.call('TIME')
|
|
26
|
+
now = tonumber(t[1]) * 1000 + tonumber(t[2]) / 1000
|
|
27
|
+
else
|
|
28
|
+
now = tonumber(ARGV[6])
|
|
29
|
+
end
|
|
30
|
+
local TOL = 1e-6
|
|
31
|
+
local function num(x) return string.format('%.6f', x) end
|
|
32
|
+
local raw = redis.call('GET', KEYS[1])
|
|
33
|
+
local full = nil
|
|
34
|
+
local stamps = nil
|
|
35
|
+
if raw then
|
|
36
|
+
if kind == 'token-bucket' then
|
|
37
|
+
full = tonumber(raw)
|
|
38
|
+
else
|
|
39
|
+
stamps = {}
|
|
40
|
+
for x in string.gmatch(raw, '[^,]+') do stamps[#stamps + 1] = tonumber(x) end
|
|
41
|
+
end
|
|
42
|
+
end
|
|
43
|
+
local function freeAt()
|
|
44
|
+
if kind == 'token-bucket' then return full end
|
|
45
|
+
if #stamps == 0 then return -math.huge end
|
|
46
|
+
return stamps[#stamps] + b
|
|
47
|
+
end
|
|
48
|
+
if raw and freeAt() <= now then
|
|
49
|
+
redis.call('DEL', KEYS[1])
|
|
50
|
+
redis.call('ZREM', KEYS[2], member)
|
|
51
|
+
raw = nil
|
|
52
|
+
full = nil
|
|
53
|
+
stamps = nil
|
|
54
|
+
end
|
|
55
|
+
if not raw then
|
|
56
|
+
if redis.call('ZCARD', KEYS[2]) >= cap then
|
|
57
|
+
redis.call('ZREMRANGEBYSCORE', KEYS[2], '-inf', num(now))
|
|
58
|
+
if redis.call('ZCARD', KEYS[2]) >= cap then
|
|
59
|
+
local first = redis.call('ZRANGE', KEYS[2], 0, 0, 'WITHSCORES')
|
|
60
|
+
return {0, num(tonumber(first[2]) - now), 'cap'}
|
|
61
|
+
end
|
|
62
|
+
end
|
|
63
|
+
end
|
|
64
|
+
local remaining
|
|
65
|
+
local state
|
|
66
|
+
if kind == 'token-bucket' then
|
|
67
|
+
local span = a * b
|
|
68
|
+
local base = now
|
|
69
|
+
if full and full > now then base = full end
|
|
70
|
+
local nxt = base + a * units
|
|
71
|
+
if nxt - now > span + TOL then
|
|
72
|
+
return {0, num(nxt - span - now), 'limit'}
|
|
73
|
+
end
|
|
74
|
+
remaining = math.floor((span - (nxt - now)) / a + TOL)
|
|
75
|
+
if remaining < 0 then remaining = 0 end
|
|
76
|
+
full = nxt
|
|
77
|
+
state = num(full)
|
|
78
|
+
else
|
|
79
|
+
local live = {}
|
|
80
|
+
for _, stamp in ipairs(stamps or {}) do
|
|
81
|
+
if stamp + b > now + TOL then live[#live + 1] = stamp end
|
|
82
|
+
end
|
|
83
|
+
if #live + units > a then
|
|
84
|
+
return {0, num(live[#live + units - a] + b - now), 'limit'}
|
|
85
|
+
end
|
|
86
|
+
for _ = 1, units do live[#live + 1] = now end
|
|
87
|
+
remaining = a - #live
|
|
88
|
+
stamps = live
|
|
89
|
+
local parts = {}
|
|
90
|
+
for i, stamp in ipairs(live) do parts[i] = num(stamp) end
|
|
91
|
+
state = table.concat(parts, ',')
|
|
92
|
+
end
|
|
93
|
+
local free = freeAt()
|
|
94
|
+
local ttl = math.ceil(free - now)
|
|
95
|
+
if ttl < 1 then ttl = 1 end
|
|
96
|
+
redis.call('SET', KEYS[1], state, 'PX', ttl)
|
|
97
|
+
redis.call('ZADD', KEYS[2], num(free), member)
|
|
98
|
+
if redis.call('PTTL', KEYS[2]) < ttl then redis.call('PEXPIRE', KEYS[2], ttl) end
|
|
99
|
+
return {1, remaining}
|
|
100
|
+
`;
|
|
101
|
+
/**
|
|
102
|
+
* This function makes a store that the replicas share, in Redis 7. Each take is one `EVAL` of
|
|
103
|
+
* `LIMITER_SCRIPT`, with all of its units. The client does not send it again, because a second take
|
|
104
|
+
* counts two times.
|
|
105
|
+
* - The names of the keys have the limiter in braces, a hash tag. Thus the state and the index of a
|
|
106
|
+
* limiter are in one slot of a Redis Cluster.
|
|
107
|
+
* - Each state expires at the time that the key is free, thus the eviction is exact, as in the memory
|
|
108
|
+
* store. The index of the limiter bounds the count of busy keys to `maxKeys`.
|
|
109
|
+
* - A reply of an error, an outcome that is not certain, or a reply of another shape rejects the
|
|
110
|
+
* take of the store. `keyedLimiter` then applies the `onStoreFailure` of the spec.
|
|
111
|
+
* - `TIME` is the wall clock of the Redis host, not a monotonic clock. Thus a step of that clock
|
|
112
|
+
* moves the counts of each key. The replicas still share one clock.
|
|
113
|
+
*/
|
|
114
|
+
const redisLimiterStore = (client, options) => {
|
|
115
|
+
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`);
|
|
116
|
+
return {
|
|
117
|
+
kind: "shared",
|
|
118
|
+
async take({ limiter, key, rule, maxKeys, units }) {
|
|
119
|
+
const n = unitsOf(units);
|
|
120
|
+
const over = overCapacity(rule, n);
|
|
121
|
+
if (over !== void 0) return over;
|
|
122
|
+
const base = `${options.prefix}{${limiter}}`;
|
|
123
|
+
const numbers = rule.type === "token-bucket" ? [rule.interval, rule.burst] : [rule.limit, rule.window];
|
|
124
|
+
const now = options.clock === void 0 ? "" : String(options.clock.now());
|
|
125
|
+
const sent = await client.send("EVAL", [
|
|
126
|
+
LIMITER_SCRIPT,
|
|
127
|
+
2,
|
|
128
|
+
`${base}:key:${key}`,
|
|
129
|
+
`${base}:keys`,
|
|
130
|
+
rule.type,
|
|
131
|
+
String(numbers[0]),
|
|
132
|
+
String(numbers[1]),
|
|
133
|
+
String(maxKeys),
|
|
134
|
+
key,
|
|
135
|
+
now,
|
|
136
|
+
String(n)
|
|
137
|
+
]);
|
|
138
|
+
if (sent.outcome !== "ok") throw new Error(`the limiter store ${limiter} failed (${sent.outcome}): ${sent.error}`);
|
|
139
|
+
return replyOf(limiter, sent.value);
|
|
140
|
+
}
|
|
141
|
+
};
|
|
142
|
+
};
|
|
143
|
+
/** This function gives the decision of a reply of `LIMITER_SCRIPT`. A reply of another shape throws. */
|
|
144
|
+
const replyOf = (limiter, reply) => {
|
|
145
|
+
if (Array.isArray(reply) && Number(reply[0]) === 1 && reply.length === 2) return {
|
|
146
|
+
allowed: true,
|
|
147
|
+
remaining: Number(reply[1])
|
|
148
|
+
};
|
|
149
|
+
if (Array.isArray(reply) && Number(reply[0]) === 0 && reply.length === 3 && (reply[2] === "limit" || reply[2] === "cap")) {
|
|
150
|
+
const retryAfter = Number(reply[1]);
|
|
151
|
+
if (Number.isFinite(retryAfter) && retryAfter > 0) return {
|
|
152
|
+
allowed: false,
|
|
153
|
+
retryAfter: {
|
|
154
|
+
value: retryAfter,
|
|
155
|
+
unit: "ms"
|
|
156
|
+
},
|
|
157
|
+
cause: reply[2]
|
|
158
|
+
};
|
|
159
|
+
}
|
|
160
|
+
throw new Error(`the limiter store ${limiter} gave a reply that is not a decision: ${JSON.stringify(reply)}`);
|
|
161
|
+
};
|
|
162
|
+
export { LIMITER_SCRIPT, redisLimiterStore };
|
|
163
|
+
|
|
164
|
+
//# sourceMappingURL=limiter.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"limiter.js","names":[],"sources":["../../src/redis/limiter.ts"],"sourcesContent":["import type { Clock } from \"../clock.ts\";\nimport type { Decision } from \"../limiter/policy.ts\";\nimport { overCapacity, unitsOf } from \"../limiter/policy.ts\";\nimport type { LimiterStore } from \"../limiter/store.ts\";\nimport type { RedisClient } from \"./client.ts\";\n\n/**\n * This script decides one take in one atomic step of Redis. It does the arithmetic of `decide`, and\n * it reads `TIME` of the server, the one clock of all replicas. A test can give the time in its place.\n * - KEYS[1] is the state of the key. KEYS[2] is the index of the limiter, a sorted set of its keys\n * by the time at which each key is free.\n * - ARGV is the type of the rule, its two numbers, the cap of keys and the key. Then it has the\n * time or \"\", and the units of the take. The store refuses a take over the capacity before the script, thus the units\n * are at most the capacity.\n * - The state of a bucket is its full time. The state of a window is the time of each take in it.\n * - The state expires at its free time (PX), and the index lets the cap count only busy keys.\n * - The reply is `{1, remaining}`, or `{0, retryAfter, cause}` with the time as text. Redis gives\n * each Lua number as a whole number, thus the fraction of a millisecond goes as text.\n */\nexport const LIMITER_SCRIPT = `\nlocal kind = ARGV[1]\nlocal a = tonumber(ARGV[2])\nlocal b = tonumber(ARGV[3])\nlocal cap = tonumber(ARGV[4])\nlocal member = ARGV[5]\nlocal units = tonumber(ARGV[7])\nlocal now\nif ARGV[6] == '' then\n local t = redis.call('TIME')\n now = tonumber(t[1]) * 1000 + tonumber(t[2]) / 1000\nelse\n now = tonumber(ARGV[6])\nend\nlocal TOL = 1e-6\nlocal function num(x) return string.format('%.6f', x) end\nlocal raw = redis.call('GET', KEYS[1])\nlocal full = nil\nlocal stamps = nil\nif raw then\n if kind == 'token-bucket' then\n full = tonumber(raw)\n else\n stamps = {}\n for x in string.gmatch(raw, '[^,]+') do stamps[#stamps + 1] = tonumber(x) end\n end\nend\nlocal function freeAt()\n if kind == 'token-bucket' then return full end\n if #stamps == 0 then return -math.huge end\n return stamps[#stamps] + b\nend\nif raw and freeAt() <= now then\n redis.call('DEL', KEYS[1])\n redis.call('ZREM', KEYS[2], member)\n raw = nil\n full = nil\n stamps = nil\nend\nif not raw then\n if redis.call('ZCARD', KEYS[2]) >= cap then\n redis.call('ZREMRANGEBYSCORE', KEYS[2], '-inf', num(now))\n if redis.call('ZCARD', KEYS[2]) >= cap then\n local first = redis.call('ZRANGE', KEYS[2], 0, 0, 'WITHSCORES')\n return {0, num(tonumber(first[2]) - now), 'cap'}\n end\n end\nend\nlocal remaining\nlocal state\nif kind == 'token-bucket' then\n local span = a * b\n local base = now\n if full and full > now then base = full end\n local nxt = base + a * units\n if nxt - now > span + TOL then\n return {0, num(nxt - span - now), 'limit'}\n end\n remaining = math.floor((span - (nxt - now)) / a + TOL)\n if remaining < 0 then remaining = 0 end\n full = nxt\n state = num(full)\nelse\n local live = {}\n for _, stamp in ipairs(stamps or {}) do\n if stamp + b > now + TOL then live[#live + 1] = stamp end\n end\n if #live + units > a then\n return {0, num(live[#live + units - a] + b - now), 'limit'}\n end\n for _ = 1, units do live[#live + 1] = now end\n remaining = a - #live\n stamps = live\n local parts = {}\n for i, stamp in ipairs(live) do parts[i] = num(stamp) end\n state = table.concat(parts, ',')\nend\nlocal free = freeAt()\nlocal ttl = math.ceil(free - now)\nif ttl < 1 then ttl = 1 end\nredis.call('SET', KEYS[1], state, 'PX', ttl)\nredis.call('ZADD', KEYS[2], num(free), member)\nif redis.call('PTTL', KEYS[2]) < ttl then redis.call('PEXPIRE', KEYS[2], ttl) end\nreturn {1, remaining}\n`;\n\n/** These options make a store of a limiter in Redis. */\nexport interface RedisLimiterOptions {\n /** This text starts the name of each key of the store in Redis, for example `limit:`. It has no braces. */\n readonly prefix: string;\n /**\n * This clock replaces `TIME` of the server, for a test that compares the store with `decide`. Without\n * it, the script reads `TIME`, thus each replica gets the one clock of the store.\n */\n readonly clock?: Clock | undefined;\n}\n\n/** A Redis client part is the part of a Redis client that the store uses: `send`. */\nexport type RedisSender = Pick<RedisClient, \"send\">;\n\n/**\n * This function makes a store that the replicas share, in Redis 7. Each take is one `EVAL` of\n * `LIMITER_SCRIPT`, with all of its units. The client does not send it again, because a second take\n * counts two times.\n * - The names of the keys have the limiter in braces, a hash tag. Thus the state and the index of a\n * limiter are in one slot of a Redis Cluster.\n * - Each state expires at the time that the key is free, thus the eviction is exact, as in the memory\n * store. The index of the limiter bounds the count of busy keys to `maxKeys`.\n * - A reply of an error, an outcome that is not certain, or a reply of another shape rejects the\n * take of the store. `keyedLimiter` then applies the `onStoreFailure` of the spec.\n * - `TIME` is the wall clock of the Redis host, not a monotonic clock. Thus a step of that clock\n * moves the counts of each key. The replicas still share one clock.\n */\nexport const redisLimiterStore = (client: RedisSender, options: RedisLimiterOptions): LimiterStore => {\n 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`);\n return {\n kind: \"shared\",\n async take({ limiter, key, rule, maxKeys, units }): Promise<Decision> {\n const n = unitsOf(units);\n // a take over the capacity is refused at each time, thus it does not go to Redis\n const over = overCapacity(rule, n);\n if (over !== undefined) return over;\n const base = `${options.prefix}{${limiter}}`;\n const numbers = rule.type === \"token-bucket\" ? [rule.interval, rule.burst] : [rule.limit, rule.window];\n const now = options.clock === undefined ? \"\" : String(options.clock.now());\n const sent = await client.send<unknown[]>(\"EVAL\", [\n LIMITER_SCRIPT,\n 2,\n `${base}:key:${key}`,\n `${base}:keys`,\n rule.type,\n String(numbers[0]),\n String(numbers[1]),\n String(maxKeys),\n key,\n now,\n String(n),\n ]);\n if (sent.outcome !== \"ok\") throw new Error(`the limiter store ${limiter} failed (${sent.outcome}): ${sent.error}`);\n return replyOf(limiter, sent.value);\n },\n };\n};\n\n/** This function gives the decision of a reply of `LIMITER_SCRIPT`. A reply of another shape throws. */\nconst replyOf = (limiter: string, reply: unknown): Decision => {\n if (Array.isArray(reply) && Number(reply[0]) === 1 && reply.length === 2) return { allowed: true, remaining: Number(reply[1]) };\n if (Array.isArray(reply) && Number(reply[0]) === 0 && reply.length === 3 && (reply[2] === \"limit\" || reply[2] === \"cap\")) {\n const retryAfter = Number(reply[1]);\n if (Number.isFinite(retryAfter) && retryAfter > 0) return { allowed: false, retryAfter: { value: retryAfter, unit: \"ms\" }, cause: reply[2] };\n }\n throw new Error(`the limiter store ${limiter} gave a reply that is not a decision: ${JSON.stringify(reply)}`);\n};\n"],"mappings":";;;;;;;;;;;;;;;AAmBA,MAAa,iBAAiB;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;AAiH9B,MAAa,qBAAqB,QAAqB,YAA+C;CACpG,IAAI,OAAO,KAAK,QAAQ,MAAM,GAAG,MAAM,IAAI,MAAM,cAAc,QAAQ,OAAO,mEAAmE;CACjJ,OAAO;EACL,MAAM;EACN,MAAM,KAAK,EAAE,SAAS,KAAK,MAAM,SAAS,SAA4B;GACpE,MAAM,IAAI,QAAQ,KAAK;GAEvB,MAAM,OAAO,aAAa,MAAM,CAAC;GACjC,IAAI,SAAS,KAAA,GAAW,OAAO;GAC/B,MAAM,OAAO,GAAG,QAAQ,OAAO,GAAG,QAAQ;GAC1C,MAAM,UAAU,KAAK,SAAS,iBAAiB,CAAC,KAAK,UAAU,KAAK,KAAK,IAAI,CAAC,KAAK,OAAO,KAAK,MAAM;GACrG,MAAM,MAAM,QAAQ,UAAU,KAAA,IAAY,KAAK,OAAO,QAAQ,MAAM,IAAI,CAAC;GACzE,MAAM,OAAO,MAAM,OAAO,KAAgB,QAAQ;IAChD;IACA;IACA,GAAG,KAAK,OAAO;IACf,GAAG,KAAK;IACR,KAAK;IACL,OAAO,QAAQ,EAAE;IACjB,OAAO,QAAQ,EAAE;IACjB,OAAO,OAAO;IACd;IACA;IACA,OAAO,CAAC;GACV,CAAC;GACD,IAAI,KAAK,YAAY,MAAM,MAAM,IAAI,MAAM,qBAAqB,QAAQ,WAAW,KAAK,QAAQ,KAAK,KAAK,OAAO;GACjH,OAAO,QAAQ,SAAS,KAAK,KAAK;EACpC;CACF;AACF;;AAGA,MAAM,WAAW,SAAiB,UAA6B;CAC7D,IAAI,MAAM,QAAQ,KAAK,KAAK,OAAO,MAAM,EAAE,MAAM,KAAK,MAAM,WAAW,GAAG,OAAO;EAAE,SAAS;EAAM,WAAW,OAAO,MAAM,EAAE;CAAE;CAC9H,IAAI,MAAM,QAAQ,KAAK,KAAK,OAAO,MAAM,EAAE,MAAM,KAAK,MAAM,WAAW,MAAM,MAAM,OAAO,WAAW,MAAM,OAAO,QAAQ;EACxH,MAAM,aAAa,OAAO,MAAM,EAAE;EAClC,IAAI,OAAO,SAAS,UAAU,KAAK,aAAa,GAAG,OAAO;GAAE,SAAS;GAAO,YAAY;IAAE,OAAO;IAAY,MAAM;GAAK;GAAG,OAAO,MAAM;EAAG;CAC7I;CACA,MAAM,IAAI,MAAM,qBAAqB,QAAQ,wCAAwC,KAAK,UAAU,KAAK,GAAG;AAC9G"}
|
|
@@ -0,0 +1,12 @@
|
|
|
1
|
+
import { Machine } from "@mark1russell7/algebra";
|
|
2
|
+
/**
|
|
3
|
+
* The Redis client as a `mark1russell7.machine/1` document. Its states are the states of ioredis.
|
|
4
|
+
* The wrapper adds its own events: the join at each `ready`, and the misses of its watchdog. It also
|
|
5
|
+
* adds a command that it sends again, and the bound of its close. A test records the transitions of the
|
|
6
|
+
* client and replays them against this document.
|
|
7
|
+
*
|
|
8
|
+
* The state `ready` hides states that the client cannot see (D4). In a black hole, ioredis stays
|
|
9
|
+
* `ready` while the kernel retransmits, until `driver_writeoff_time` or the watchdog ends that
|
|
10
|
+
* hidden interval. A timer of this document bounds each hidden interval.
|
|
11
|
+
*/
|
|
12
|
+
export declare const REDIS_CLIENT_MACHINE: Machine;
|
|
@@ -0,0 +1,144 @@
|
|
|
1
|
+
import { clientKnob, closeTimer, transition } from "../client/machine.js";
|
|
2
|
+
// src/redis/machine.ts
|
|
3
|
+
/** The states where the connection exists or tries to exist, thus where it can close. */
|
|
4
|
+
const LIVE = [
|
|
5
|
+
"connecting",
|
|
6
|
+
"connect",
|
|
7
|
+
"ready",
|
|
8
|
+
"reconnecting"
|
|
9
|
+
];
|
|
10
|
+
const ANY = [
|
|
11
|
+
"none",
|
|
12
|
+
"connecting",
|
|
13
|
+
"connect",
|
|
14
|
+
"ready",
|
|
15
|
+
"close",
|
|
16
|
+
"reconnecting",
|
|
17
|
+
"end"
|
|
18
|
+
];
|
|
19
|
+
/**
|
|
20
|
+
* The Redis client as a `mark1russell7.machine/1` document. Its states are the states of ioredis.
|
|
21
|
+
* The wrapper adds its own events: the join at each `ready`, and the misses of its watchdog. It also
|
|
22
|
+
* adds a command that it sends again, and the bound of its close. A test records the transitions of the
|
|
23
|
+
* client and replays them against this document.
|
|
24
|
+
*
|
|
25
|
+
* The state `ready` hides states that the client cannot see (D4). In a black hole, ioredis stays
|
|
26
|
+
* `ready` while the kernel retransmits, until `driver_writeoff_time` or the watchdog ends that
|
|
27
|
+
* hidden interval. A timer of this document bounds each hidden interval.
|
|
28
|
+
*/
|
|
29
|
+
const REDIS_CLIENT_MACHINE = {
|
|
30
|
+
format: "mark1russell7.machine/1",
|
|
31
|
+
id: "service.machine.redis-client",
|
|
32
|
+
title: "The Redis client of the service, over ioredis 6",
|
|
33
|
+
layer: "driver",
|
|
34
|
+
version: "0.2.0",
|
|
35
|
+
sources: [{
|
|
36
|
+
ref: "packages/service/src/redis (the client implements this document)",
|
|
37
|
+
rung: "implemented"
|
|
38
|
+
}],
|
|
39
|
+
states: {
|
|
40
|
+
none: {
|
|
41
|
+
initial: true,
|
|
42
|
+
note: "the client exists, and its connection does not"
|
|
43
|
+
},
|
|
44
|
+
connecting: { note: "a TCP connection attempt" },
|
|
45
|
+
connect: { note: "the handshake: HELLO, then the ready check (INFO)" },
|
|
46
|
+
ready: { note: "it can hide a black hole or a frozen server, until a timer of the client ends it" },
|
|
47
|
+
close: { note: "the socket closed. Each command in flight settles as indeterminate" },
|
|
48
|
+
reconnecting: { note: "the delay of the retry strategy, which doubles to its cap" },
|
|
49
|
+
end: { note: "the retry window ended, or the service closed the client. The watchdog starts it again" }
|
|
50
|
+
},
|
|
51
|
+
events: {
|
|
52
|
+
"status.connecting": { kind: "local" },
|
|
53
|
+
"status.connect": { kind: "rx" },
|
|
54
|
+
"status.ready": { kind: "rx" },
|
|
55
|
+
"status.close": { kind: "rx" },
|
|
56
|
+
"status.reconnecting": { kind: "timer" },
|
|
57
|
+
"status.end": { kind: "local" },
|
|
58
|
+
join: {
|
|
59
|
+
kind: "rx",
|
|
60
|
+
note: "the reply of CLIENT ID at ready (D1)"
|
|
61
|
+
},
|
|
62
|
+
error: { kind: "rx" },
|
|
63
|
+
"connect.failed": { kind: "local" },
|
|
64
|
+
"command.written": { kind: "tx" },
|
|
65
|
+
"command.reissue": { kind: "local" },
|
|
66
|
+
data: { kind: "rx" },
|
|
67
|
+
heartbeat: { kind: "timer" },
|
|
68
|
+
"watchdog.miss": { kind: "local" },
|
|
69
|
+
"watchdog.reconnect": { kind: "local" },
|
|
70
|
+
"watchdog.revive": { kind: "local" },
|
|
71
|
+
"retry.window-ended": { kind: "local" },
|
|
72
|
+
"close.request": { kind: "local" },
|
|
73
|
+
"close.bound": { kind: "timer" },
|
|
74
|
+
"close.destroy": {
|
|
75
|
+
kind: "local",
|
|
76
|
+
note: "the bound destroys a socket that a silent server does not close"
|
|
77
|
+
}
|
|
78
|
+
},
|
|
79
|
+
knobs: {
|
|
80
|
+
connect_bound: clientKnob("connectTimeout"),
|
|
81
|
+
driver_writeoff_time: clientKnob("socketTimeout"),
|
|
82
|
+
heartbeat_interval: clientKnob("watchdog.interval"),
|
|
83
|
+
close_bound: clientKnob("close.bound")
|
|
84
|
+
},
|
|
85
|
+
timers: {
|
|
86
|
+
connect_timeout: {
|
|
87
|
+
duration: {
|
|
88
|
+
knob: "connect_bound",
|
|
89
|
+
unit: "s"
|
|
90
|
+
},
|
|
91
|
+
arms: ["enter:connecting"],
|
|
92
|
+
cancels: ["status.connect", "status.close"],
|
|
93
|
+
fires: "status.close",
|
|
94
|
+
note: "ioredis destroys the attempt"
|
|
95
|
+
},
|
|
96
|
+
writeoff: {
|
|
97
|
+
duration: {
|
|
98
|
+
knob: "driver_writeoff_time",
|
|
99
|
+
unit: "s"
|
|
100
|
+
},
|
|
101
|
+
arms: ["command.written"],
|
|
102
|
+
cancels: ["data", "status.close"],
|
|
103
|
+
fires: "status.close",
|
|
104
|
+
note: "ioredis destroys a socket that gives no data while a command waits: it ends the hidden interval of a black hole"
|
|
105
|
+
},
|
|
106
|
+
heartbeat: {
|
|
107
|
+
duration: {
|
|
108
|
+
knob: "heartbeat_interval",
|
|
109
|
+
unit: "s"
|
|
110
|
+
},
|
|
111
|
+
arms: ["enter:ready", "heartbeat"],
|
|
112
|
+
cancels: ["status.close", "status.end"],
|
|
113
|
+
fires: "heartbeat",
|
|
114
|
+
note: "a PING on the connection of the application"
|
|
115
|
+
},
|
|
116
|
+
close: closeTimer()
|
|
117
|
+
},
|
|
118
|
+
transitions: [
|
|
119
|
+
transition("connect", [
|
|
120
|
+
"none",
|
|
121
|
+
"reconnecting",
|
|
122
|
+
"end",
|
|
123
|
+
"close"
|
|
124
|
+
], "status.connecting", "connecting"),
|
|
125
|
+
transition("tcp", "connecting", "status.connect", "connect"),
|
|
126
|
+
transition("ready", "connect", "status.ready", "ready"),
|
|
127
|
+
transition("close", [...LIVE], "status.close", "close"),
|
|
128
|
+
transition("retry", "close", "status.reconnecting", "reconnecting"),
|
|
129
|
+
transition("end", [...LIVE, "close"], "status.end", "end"),
|
|
130
|
+
transition("join", "ready", "join", "ready"),
|
|
131
|
+
transition("miss", "ready", "watchdog.miss", "ready"),
|
|
132
|
+
transition("watchdog-reconnect", "ready", "watchdog.reconnect", "ready"),
|
|
133
|
+
transition("revive", "end", "watchdog.revive", "end"),
|
|
134
|
+
transition("window-ended", "close", "retry.window-ended", "close"),
|
|
135
|
+
...ANY.map((state) => transition(`error-${state}`, state, "error", state)),
|
|
136
|
+
...ANY.map((state) => transition(`connect-failed-${state}`, state, "connect.failed", state)),
|
|
137
|
+
...ANY.map((state) => transition(`reissue-${state}`, state, "command.reissue", state)),
|
|
138
|
+
...ANY.map((state) => transition(`close-bound-${state}`, state, "close.bound", state, { timer: "close" })),
|
|
139
|
+
...ANY.map((state) => transition(`close-destroy-${state}`, state, "close.destroy", state))
|
|
140
|
+
]
|
|
141
|
+
};
|
|
142
|
+
export { REDIS_CLIENT_MACHINE };
|
|
143
|
+
|
|
144
|
+
//# sourceMappingURL=machine.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"machine.js","names":["knob","t"],"sources":["../../src/redis/machine.ts"],"sourcesContent":["import type { Machine } from \"@mark1russell7/algebra\";\nimport { clientKnob as knob, closeTimer, transition as t } from \"../client/machine.ts\";\n\n/** The states where the connection exists or tries to exist, thus where it can close. */\nconst LIVE = [\"connecting\", \"connect\", \"ready\", \"reconnecting\"] as const;\nconst ANY = [\"none\", \"connecting\", \"connect\", \"ready\", \"close\", \"reconnecting\", \"end\"] as const;\n\n/**\n * The Redis client as a `mark1russell7.machine/1` document. Its states are the states of ioredis.\n * The wrapper adds its own events: the join at each `ready`, and the misses of its watchdog. It also\n * adds a command that it sends again, and the bound of its close. A test records the transitions of the\n * client and replays them against this document.\n *\n * The state `ready` hides states that the client cannot see (D4). In a black hole, ioredis stays\n * `ready` while the kernel retransmits, until `driver_writeoff_time` or the watchdog ends that\n * hidden interval. A timer of this document bounds each hidden interval.\n */\nexport const REDIS_CLIENT_MACHINE: Machine = {\n format: \"mark1russell7.machine/1\",\n id: \"service.machine.redis-client\",\n title: \"The Redis client of the service, over ioredis 6\",\n layer: \"driver\",\n version: \"0.2.0\",\n sources: [{ ref: \"packages/service/src/redis (the client implements this document)\", rung: \"implemented\" }],\n states: {\n none: { initial: true, note: \"the client exists, and its connection does not\" },\n connecting: { note: \"a TCP connection attempt\" },\n connect: { note: \"the handshake: HELLO, then the ready check (INFO)\" },\n ready: { note: \"it can hide a black hole or a frozen server, until a timer of the client ends it\" },\n close: { note: \"the socket closed. Each command in flight settles as indeterminate\" },\n reconnecting: { note: \"the delay of the retry strategy, which doubles to its cap\" },\n end: { note: \"the retry window ended, or the service closed the client. The watchdog starts it again\" },\n },\n events: {\n \"status.connecting\": { kind: \"local\" },\n \"status.connect\": { kind: \"rx\" },\n \"status.ready\": { kind: \"rx\" },\n \"status.close\": { kind: \"rx\" },\n \"status.reconnecting\": { kind: \"timer\" },\n \"status.end\": { kind: \"local\" },\n join: { kind: \"rx\", note: \"the reply of CLIENT ID at ready (D1)\" },\n error: { kind: \"rx\" },\n \"connect.failed\": { kind: \"local\" },\n \"command.written\": { kind: \"tx\" },\n \"command.reissue\": { kind: \"local\" },\n data: { kind: \"rx\" },\n heartbeat: { kind: \"timer\" },\n \"watchdog.miss\": { kind: \"local\" },\n \"watchdog.reconnect\": { kind: \"local\" },\n \"watchdog.revive\": { kind: \"local\" },\n \"retry.window-ended\": { kind: \"local\" },\n \"close.request\": { kind: \"local\" },\n \"close.bound\": { kind: \"timer\" },\n \"close.destroy\": { kind: \"local\", note: \"the bound destroys a socket that a silent server does not close\" },\n },\n knobs: {\n connect_bound: knob(\"connectTimeout\"),\n driver_writeoff_time: knob(\"socketTimeout\"),\n heartbeat_interval: knob(\"watchdog.interval\"),\n close_bound: knob(\"close.bound\"),\n },\n timers: {\n connect_timeout: {\n duration: { knob: \"connect_bound\", unit: \"s\" },\n arms: [\"enter:connecting\"],\n cancels: [\"status.connect\", \"status.close\"],\n fires: \"status.close\",\n note: \"ioredis destroys the attempt\",\n },\n writeoff: {\n duration: { knob: \"driver_writeoff_time\", unit: \"s\" },\n arms: [\"command.written\"],\n cancels: [\"data\", \"status.close\"],\n fires: \"status.close\",\n note: \"ioredis destroys a socket that gives no data while a command waits: it ends the hidden interval of a black hole\",\n },\n heartbeat: {\n duration: { knob: \"heartbeat_interval\", unit: \"s\" },\n arms: [\"enter:ready\", \"heartbeat\"],\n cancels: [\"status.close\", \"status.end\"],\n fires: \"heartbeat\",\n note: \"a PING on the connection of the application\",\n },\n close: closeTimer(),\n },\n transitions: [\n t(\"connect\", [\"none\", \"reconnecting\", \"end\", \"close\"], \"status.connecting\", \"connecting\"),\n t(\"tcp\", \"connecting\", \"status.connect\", \"connect\"),\n t(\"ready\", \"connect\", \"status.ready\", \"ready\"),\n t(\"close\", [...LIVE], \"status.close\", \"close\"),\n t(\"retry\", \"close\", \"status.reconnecting\", \"reconnecting\"),\n t(\"end\", [...LIVE, \"close\"], \"status.end\", \"end\"),\n t(\"join\", \"ready\", \"join\", \"ready\"),\n t(\"miss\", \"ready\", \"watchdog.miss\", \"ready\"),\n t(\"watchdog-reconnect\", \"ready\", \"watchdog.reconnect\", \"ready\"),\n t(\"revive\", \"end\", \"watchdog.revive\", \"end\"),\n // the retry strategy answers in `close`, before ioredis moves to `reconnecting` or `end`\n t(\"window-ended\", \"close\", \"retry.window-ended\", \"close\"),\n ...ANY.map((state) => t(`error-${state}`, state, \"error\", state)),\n ...ANY.map((state) => t(`connect-failed-${state}`, state, \"connect.failed\", state)),\n ...ANY.map((state) => t(`reissue-${state}`, state, \"command.reissue\", state)),\n ...ANY.map((state) => t(`close-bound-${state}`, state, \"close.bound\", state, { timer: \"close\" })),\n ...ANY.map((state) => t(`close-destroy-${state}`, state, \"close.destroy\", state)),\n ],\n};\n"],"mappings":";;;AAIA,MAAM,OAAO;CAAC;CAAc;CAAW;CAAS;AAAc;AAC9D,MAAM,MAAM;CAAC;CAAQ;CAAc;CAAW;CAAS;CAAS;CAAgB;AAAK;;;;;;;;;;;AAYrF,MAAa,uBAAgC;CAC3C,QAAQ;CACR,IAAI;CACJ,OAAO;CACP,OAAO;CACP,SAAS;CACT,SAAS,CAAC;EAAE,KAAK;EAAoE,MAAM;CAAc,CAAC;CAC1G,QAAQ;EACN,MAAM;GAAE,SAAS;GAAM,MAAM;EAAiD;EAC9E,YAAY,EAAE,MAAM,2BAA2B;EAC/C,SAAS,EAAE,MAAM,oDAAoD;EACrE,OAAO,EAAE,MAAM,mFAAmF;EAClG,OAAO,EAAE,MAAM,qEAAqE;EACpF,cAAc,EAAE,MAAM,4DAA4D;EAClF,KAAK,EAAE,MAAM,yFAAyF;CACxG;CACA,QAAQ;EACN,qBAAqB,EAAE,MAAM,QAAQ;EACrC,kBAAkB,EAAE,MAAM,KAAK;EAC/B,gBAAgB,EAAE,MAAM,KAAK;EAC7B,gBAAgB,EAAE,MAAM,KAAK;EAC7B,uBAAuB,EAAE,MAAM,QAAQ;EACvC,cAAc,EAAE,MAAM,QAAQ;EAC9B,MAAM;GAAE,MAAM;GAAM,MAAM;EAAuC;EACjE,OAAO,EAAE,MAAM,KAAK;EACpB,kBAAkB,EAAE,MAAM,QAAQ;EAClC,mBAAmB,EAAE,MAAM,KAAK;EAChC,mBAAmB,EAAE,MAAM,QAAQ;EACnC,MAAM,EAAE,MAAM,KAAK;EACnB,WAAW,EAAE,MAAM,QAAQ;EAC3B,iBAAiB,EAAE,MAAM,QAAQ;EACjC,sBAAsB,EAAE,MAAM,QAAQ;EACtC,mBAAmB,EAAE,MAAM,QAAQ;EACnC,sBAAsB,EAAE,MAAM,QAAQ;EACtC,iBAAiB,EAAE,MAAM,QAAQ;EACjC,eAAe,EAAE,MAAM,QAAQ;EAC/B,iBAAiB;GAAE,MAAM;GAAS,MAAM;EAAkE;CAC5G;CACA,OAAO;EACL,eAAeA,WAAK,gBAAgB;EACpC,sBAAsBA,WAAK,eAAe;EAC1C,oBAAoBA,WAAK,mBAAmB;EAC5C,aAAaA,WAAK,aAAa;CACjC;CACA,QAAQ;EACN,iBAAiB;GACf,UAAU;IAAE,MAAM;IAAiB,MAAM;GAAI;GAC7C,MAAM,CAAC,kBAAkB;GACzB,SAAS,CAAC,kBAAkB,cAAc;GAC1C,OAAO;GACP,MAAM;EACR;EACA,UAAU;GACR,UAAU;IAAE,MAAM;IAAwB,MAAM;GAAI;GACpD,MAAM,CAAC,iBAAiB;GACxB,SAAS,CAAC,QAAQ,cAAc;GAChC,OAAO;GACP,MAAM;EACR;EACA,WAAW;GACT,UAAU;IAAE,MAAM;IAAsB,MAAM;GAAI;GAClD,MAAM,CAAC,eAAe,WAAW;GACjC,SAAS,CAAC,gBAAgB,YAAY;GACtC,OAAO;GACP,MAAM;EACR;EACA,OAAO,WAAW;CACpB;CACA,aAAa;EACXC,WAAE,WAAW;GAAC;GAAQ;GAAgB;GAAO;EAAO,GAAG,qBAAqB,YAAY;EACxFA,WAAE,OAAO,cAAc,kBAAkB,SAAS;EAClDA,WAAE,SAAS,WAAW,gBAAgB,OAAO;EAC7CA,WAAE,SAAS,CAAC,GAAG,IAAI,GAAG,gBAAgB,OAAO;EAC7CA,WAAE,SAAS,SAAS,uBAAuB,cAAc;EACzDA,WAAE,OAAO,CAAC,GAAG,MAAM,OAAO,GAAG,cAAc,KAAK;EAChDA,WAAE,QAAQ,SAAS,QAAQ,OAAO;EAClCA,WAAE,QAAQ,SAAS,iBAAiB,OAAO;EAC3CA,WAAE,sBAAsB,SAAS,sBAAsB,OAAO;EAC9DA,WAAE,UAAU,OAAO,mBAAmB,KAAK;EAE3CA,WAAE,gBAAgB,SAAS,sBAAsB,OAAO;EACxD,GAAG,IAAI,KAAK,UAAUA,WAAE,SAAS,SAAS,OAAO,SAAS,KAAK,CAAC;EAChE,GAAG,IAAI,KAAK,UAAUA,WAAE,kBAAkB,SAAS,OAAO,kBAAkB,KAAK,CAAC;EAClF,GAAG,IAAI,KAAK,UAAUA,WAAE,WAAW,SAAS,OAAO,mBAAmB,KAAK,CAAC;EAC5E,GAAG,IAAI,KAAK,UAAUA,WAAE,eAAe,SAAS,OAAO,eAAe,OAAO,EAAE,OAAO,QAAQ,CAAC,CAAC;EAChG,GAAG,IAAI,KAAK,UAAUA,WAAE,iBAAiB,SAAS,OAAO,iBAAiB,KAAK,CAAC;CAClF;AACF"}
|
|
@@ -0,0 +1,11 @@
|
|
|
1
|
+
import { Detector, Meeting, Situation, firstDetector, unwatched } from "../client/meets.js";
|
|
2
|
+
import { RedisKnobs } from "./client.js";
|
|
3
|
+
import { Extent } from "@mark1russell7/quantity";
|
|
4
|
+
/**
|
|
5
|
+
* This function gives the manifestations that a Redis client can meet, and the detectors of each, from
|
|
6
|
+
* its knobs. A reset or a FIN arrives on the next segment. A black hole and a host that is gone give
|
|
7
|
+
* no answer at all. A frozen process and a stalled server keep a kernel that acknowledges each
|
|
8
|
+
* segment. Thus TCP keepalive does not fire on them, and only the client's own timers find them.
|
|
9
|
+
*/
|
|
10
|
+
export declare const meetings: (name: string, knobs: RedisKnobs, heartbeat: Extent) => Meeting[];
|
|
11
|
+
export { type Detector, type Meeting, type Situation, firstDetector, unwatched };
|
|
@@ -0,0 +1,75 @@
|
|
|
1
|
+
import { WHAT, firstDetector, immediate, keepaliveAnswered, keepaliveReset, keepaliveSilent, kernelGiveUp, unwatched } from "../client/meets.js";
|
|
2
|
+
// src/redis/meets.ts
|
|
3
|
+
/**
|
|
4
|
+
* This function gives the manifestations that a Redis client can meet, and the detectors of each, from
|
|
5
|
+
* its knobs. A reset or a FIN arrives on the next segment. A black hole and a host that is gone give
|
|
6
|
+
* no answer at all. A frozen process and a stalled server keep a kernel that acknowledges each
|
|
7
|
+
* segment. Thus TCP keepalive does not fire on them, and only the client's own timers find them.
|
|
8
|
+
*/
|
|
9
|
+
const meetings = (name, knobs, heartbeat) => {
|
|
10
|
+
const watchdog = (situation) => ({
|
|
11
|
+
by: "heartbeat_detection_time",
|
|
12
|
+
within: heartbeat,
|
|
13
|
+
situation,
|
|
14
|
+
effect: `the watchdog of ${name} closes the connection, and the client connects again`
|
|
15
|
+
});
|
|
16
|
+
const writeoff = {
|
|
17
|
+
by: "driver_writeoff_time",
|
|
18
|
+
within: knobs.driver_writeoff_time,
|
|
19
|
+
situation: "in flight",
|
|
20
|
+
effect: "ioredis destroys the socket of a command without data, and the client connects again"
|
|
21
|
+
};
|
|
22
|
+
const command = {
|
|
23
|
+
by: "request_timeout",
|
|
24
|
+
within: knobs.request_timeout,
|
|
25
|
+
situation: "in flight",
|
|
26
|
+
effect: "the command fails as indeterminate, and the state of the connection does not change"
|
|
27
|
+
};
|
|
28
|
+
const giveUp = kernelGiveUp(knobs.retrans_giveup_time);
|
|
29
|
+
const silent = (manifestation) => ({
|
|
30
|
+
manifestation,
|
|
31
|
+
what: WHAT[manifestation],
|
|
32
|
+
detectors: [
|
|
33
|
+
command,
|
|
34
|
+
writeoff,
|
|
35
|
+
watchdog("in flight"),
|
|
36
|
+
giveUp,
|
|
37
|
+
watchdog("idle"),
|
|
38
|
+
keepaliveSilent(knobs.keepalive_idle)
|
|
39
|
+
]
|
|
40
|
+
});
|
|
41
|
+
const acknowledged = (manifestation, what) => ({
|
|
42
|
+
manifestation,
|
|
43
|
+
what,
|
|
44
|
+
detectors: [
|
|
45
|
+
command,
|
|
46
|
+
writeoff,
|
|
47
|
+
watchdog("in flight"),
|
|
48
|
+
watchdog("idle"),
|
|
49
|
+
keepaliveAnswered()
|
|
50
|
+
]
|
|
51
|
+
});
|
|
52
|
+
return [
|
|
53
|
+
{
|
|
54
|
+
manifestation: "rst",
|
|
55
|
+
what: WHAT.rst,
|
|
56
|
+
detectors: [
|
|
57
|
+
immediate("in flight", "the command fails, and the client connects again, and each attempt is refused"),
|
|
58
|
+
watchdog("idle"),
|
|
59
|
+
keepaliveReset(knobs.keepalive_idle)
|
|
60
|
+
]
|
|
61
|
+
},
|
|
62
|
+
{
|
|
63
|
+
manifestation: "fin",
|
|
64
|
+
what: WHAT.fin,
|
|
65
|
+
detectors: [immediate("in flight", "the close arrives, and the command fails"), immediate("idle", "the close arrives")]
|
|
66
|
+
},
|
|
67
|
+
silent("blackhole"),
|
|
68
|
+
silent("freeze-host"),
|
|
69
|
+
acknowledged("freeze-process", WHAT["freeze-process"]),
|
|
70
|
+
acknowledged("app-stall", "the server stops its replies for a time, for example a long command, and its kernel stays up")
|
|
71
|
+
];
|
|
72
|
+
};
|
|
73
|
+
export { firstDetector, meetings, unwatched };
|
|
74
|
+
|
|
75
|
+
//# sourceMappingURL=meets.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"meets.js","names":[],"sources":["../../src/redis/meets.ts"],"sourcesContent":["import type { Extent } from \"@mark1russell7/quantity\";\nimport type { Detector, Meeting, Situation } from \"../client/meets.ts\";\nimport { WHAT, immediate, keepaliveAnswered, keepaliveReset, keepaliveSilent, kernelGiveUp } from \"../client/meets.ts\";\nimport type { RedisKnobs } from \"./client.ts\";\n\n// the shapes and the queries moved to the core of the clients: this entry keeps them for the users of 0.5\nexport type { Detector, Meeting, Situation } from \"../client/meets.ts\";\nexport { firstDetector, unwatched } from \"../client/meets.ts\";\n\n/**\n * This function gives the manifestations that a Redis client can meet, and the detectors of each, from\n * its knobs. A reset or a FIN arrives on the next segment. A black hole and a host that is gone give\n * no answer at all. A frozen process and a stalled server keep a kernel that acknowledges each\n * segment. Thus TCP keepalive does not fire on them, and only the client's own timers find them.\n */\nexport const meetings = (name: string, knobs: RedisKnobs, heartbeat: Extent): Meeting[] => {\n const watchdog = (situation: Situation): Detector => ({\n by: \"heartbeat_detection_time\",\n within: heartbeat,\n situation,\n effect: `the watchdog of ${name} closes the connection, and the client connects again`,\n });\n const writeoff: Detector = {\n by: \"driver_writeoff_time\",\n within: knobs.driver_writeoff_time,\n situation: \"in flight\",\n effect: \"ioredis destroys the socket of a command without data, and the client connects again\",\n };\n const command: Detector = {\n by: \"request_timeout\",\n within: knobs.request_timeout,\n situation: \"in flight\",\n effect: \"the command fails as indeterminate, and the state of the connection does not change\",\n };\n const giveUp = kernelGiveUp(knobs.retrans_giveup_time);\n const silent = (manifestation: \"blackhole\" | \"freeze-host\"): Meeting => ({\n manifestation,\n what: WHAT[manifestation],\n detectors: [command, writeoff, watchdog(\"in flight\"), giveUp, watchdog(\"idle\"), keepaliveSilent(knobs.keepalive_idle)],\n });\n const acknowledged = (manifestation: string, what: string): Meeting => ({\n manifestation,\n what,\n detectors: [command, writeoff, watchdog(\"in flight\"), watchdog(\"idle\"), keepaliveAnswered()],\n });\n return [\n {\n manifestation: \"rst\",\n what: WHAT.rst,\n detectors: [\n immediate(\"in flight\", \"the command fails, and the client connects again, and each attempt is refused\"),\n watchdog(\"idle\"),\n keepaliveReset(knobs.keepalive_idle),\n ],\n },\n {\n manifestation: \"fin\",\n what: WHAT.fin,\n detectors: [immediate(\"in flight\", \"the close arrives, and the command fails\"), immediate(\"idle\", \"the close arrives\")],\n },\n silent(\"blackhole\"),\n silent(\"freeze-host\"),\n acknowledged(\"freeze-process\", WHAT[\"freeze-process\"]),\n acknowledged(\"app-stall\", \"the server stops its replies for a time, for example a long command, and its kernel stays up\"),\n ];\n};\n"],"mappings":";;;;;;;;AAeA,MAAa,YAAY,MAAc,OAAmB,cAAiC;CACzF,MAAM,YAAY,eAAoC;EACpD,IAAI;EACJ,QAAQ;EACR;EACA,QAAQ,mBAAmB,KAAK;CAClC;CACA,MAAM,WAAqB;EACzB,IAAI;EACJ,QAAQ,MAAM;EACd,WAAW;EACX,QAAQ;CACV;CACA,MAAM,UAAoB;EACxB,IAAI;EACJ,QAAQ,MAAM;EACd,WAAW;EACX,QAAQ;CACV;CACA,MAAM,SAAS,aAAa,MAAM,mBAAmB;CACrD,MAAM,UAAU,mBAAyD;EACvE;EACA,MAAM,KAAK;EACX,WAAW;GAAC;GAAS;GAAU,SAAS,WAAW;GAAG;GAAQ,SAAS,MAAM;GAAG,gBAAgB,MAAM,cAAc;EAAC;CACvH;CACA,MAAM,gBAAgB,eAAuB,UAA2B;EACtE;EACA;EACA,WAAW;GAAC;GAAS;GAAU,SAAS,WAAW;GAAG,SAAS,MAAM;GAAG,kBAAkB;EAAC;CAC7F;CACA,OAAO;EACL;GACE,eAAe;GACf,MAAM,KAAK;GACX,WAAW;IACT,UAAU,aAAa,+EAA+E;IACtG,SAAS,MAAM;IACf,eAAe,MAAM,cAAc;GACrC;EACF;EACA;GACE,eAAe;GACf,MAAM,KAAK;GACX,WAAW,CAAC,UAAU,aAAa,0CAA0C,GAAG,UAAU,QAAQ,mBAAmB,CAAC;EACxH;EACA,OAAO,WAAW;EAClB,OAAO,aAAa;EACpB,aAAa,kBAAkB,KAAK,iBAAiB;EACrD,aAAa,aAAa,8FAA8F;CAC1H;AACF"}
|
|
@@ -0,0 +1,5 @@
|
|
|
1
|
+
import { Operation } from "../operation.js";
|
|
2
|
+
/** These are the operations of the client: the idempotency of each command that the table names. */
|
|
3
|
+
export declare const REDIS_OPERATIONS: Readonly<Record<string, Operation>>;
|
|
4
|
+
/** This function gives the name of a command as the table writes it: upper case, and CLIENT ID with its subcommand. */
|
|
5
|
+
export declare const commandName: (command: string, args: readonly unknown[]) => string;
|
|
@@ -0,0 +1,114 @@
|
|
|
1
|
+
// src/redis/operations.ts
|
|
2
|
+
/**
|
|
3
|
+
* These are the Redis commands whose idempotency the client declares when the declaration does not
|
|
4
|
+
* say. A second issue of an idempotent command has the same effect. Thus the client can send it
|
|
5
|
+
* again after a failure that left its result unknown. The client does not send again a command
|
|
6
|
+
* that is not in this table: a declaration that uses it says its idempotency itself.
|
|
7
|
+
*/
|
|
8
|
+
const SAME_EFFECT = [
|
|
9
|
+
"PING",
|
|
10
|
+
"ECHO",
|
|
11
|
+
"GET",
|
|
12
|
+
"MGET",
|
|
13
|
+
"STRLEN",
|
|
14
|
+
"GETRANGE",
|
|
15
|
+
"EXISTS",
|
|
16
|
+
"TYPE",
|
|
17
|
+
"TTL",
|
|
18
|
+
"PTTL",
|
|
19
|
+
"EXPIRETIME",
|
|
20
|
+
"HGET",
|
|
21
|
+
"HMGET",
|
|
22
|
+
"HGETALL",
|
|
23
|
+
"HKEYS",
|
|
24
|
+
"HVALS",
|
|
25
|
+
"HLEN",
|
|
26
|
+
"HEXISTS",
|
|
27
|
+
"SMEMBERS",
|
|
28
|
+
"SISMEMBER",
|
|
29
|
+
"SMISMEMBER",
|
|
30
|
+
"SCARD",
|
|
31
|
+
"ZRANGE",
|
|
32
|
+
"ZRANGEBYSCORE",
|
|
33
|
+
"ZSCORE",
|
|
34
|
+
"ZMSCORE",
|
|
35
|
+
"ZRANK",
|
|
36
|
+
"ZCARD",
|
|
37
|
+
"ZCOUNT",
|
|
38
|
+
"LRANGE",
|
|
39
|
+
"LINDEX",
|
|
40
|
+
"LLEN",
|
|
41
|
+
"XRANGE",
|
|
42
|
+
"XREVRANGE",
|
|
43
|
+
"XLEN",
|
|
44
|
+
"SCAN",
|
|
45
|
+
"SSCAN",
|
|
46
|
+
"HSCAN",
|
|
47
|
+
"ZSCAN",
|
|
48
|
+
"DBSIZE",
|
|
49
|
+
"INFO",
|
|
50
|
+
"CLIENT ID",
|
|
51
|
+
"SET",
|
|
52
|
+
"MSET",
|
|
53
|
+
"SETEX",
|
|
54
|
+
"PSETEX",
|
|
55
|
+
"DEL",
|
|
56
|
+
"UNLINK",
|
|
57
|
+
"EXPIRE",
|
|
58
|
+
"PEXPIRE",
|
|
59
|
+
"EXPIREAT",
|
|
60
|
+
"PEXPIREAT",
|
|
61
|
+
"PERSIST",
|
|
62
|
+
"HSET",
|
|
63
|
+
"HDEL",
|
|
64
|
+
"SADD",
|
|
65
|
+
"SREM",
|
|
66
|
+
"ZADD",
|
|
67
|
+
"ZREM"
|
|
68
|
+
];
|
|
69
|
+
/**
|
|
70
|
+
* These commands change a state relative to the state before them. A second issue changes it again:
|
|
71
|
+
* an INCR that the client sends again after a timeout counts two times.
|
|
72
|
+
*/
|
|
73
|
+
const CHANGE = [
|
|
74
|
+
"INCR",
|
|
75
|
+
"INCRBY",
|
|
76
|
+
"INCRBYFLOAT",
|
|
77
|
+
"DECR",
|
|
78
|
+
"DECRBY",
|
|
79
|
+
"HINCRBY",
|
|
80
|
+
"HINCRBYFLOAT",
|
|
81
|
+
"ZINCRBY",
|
|
82
|
+
"APPEND",
|
|
83
|
+
"LPUSH",
|
|
84
|
+
"RPUSH",
|
|
85
|
+
"LPOP",
|
|
86
|
+
"RPOP",
|
|
87
|
+
"BLPOP",
|
|
88
|
+
"BRPOP",
|
|
89
|
+
"LMOVE",
|
|
90
|
+
"BLMOVE",
|
|
91
|
+
"SPOP",
|
|
92
|
+
"XADD",
|
|
93
|
+
"XACK",
|
|
94
|
+
"PUBLISH",
|
|
95
|
+
"EVAL",
|
|
96
|
+
"EVALSHA",
|
|
97
|
+
"FCALL",
|
|
98
|
+
"MULTI",
|
|
99
|
+
"EXEC",
|
|
100
|
+
"GETDEL",
|
|
101
|
+
"GETSET",
|
|
102
|
+
"SETNX"
|
|
103
|
+
];
|
|
104
|
+
/** These are the operations of the client: the idempotency of each command that the table names. */
|
|
105
|
+
const REDIS_OPERATIONS = Object.fromEntries([...SAME_EFFECT.map((command) => [command, { idempotent: true }]), ...CHANGE.map((command) => [command, { idempotent: false }])]);
|
|
106
|
+
/** This function gives the name of a command as the table writes it: upper case, and CLIENT ID with its subcommand. */
|
|
107
|
+
const commandName = (command, args) => {
|
|
108
|
+
const name = command.toUpperCase();
|
|
109
|
+
if ((name === "CLIENT" || name === "CONFIG" || name === "DEBUG") && typeof args[0] === "string") return `${name} ${args[0].toUpperCase()}`;
|
|
110
|
+
return name;
|
|
111
|
+
};
|
|
112
|
+
export { REDIS_OPERATIONS, commandName };
|
|
113
|
+
|
|
114
|
+
//# sourceMappingURL=operations.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"operations.js","names":[],"sources":["../../src/redis/operations.ts"],"sourcesContent":["import type { Operation } from \"../operation.ts\";\n\n/**\n * These are the Redis commands whose idempotency the client declares when the declaration does not\n * say. A second issue of an idempotent command has the same effect. Thus the client can send it\n * again after a failure that left its result unknown. The client does not send again a command\n * that is not in this table: a declaration that uses it says its idempotency itself.\n */\nconst SAME_EFFECT = [\n // reads\n \"PING\",\n \"ECHO\",\n \"GET\",\n \"MGET\",\n \"STRLEN\",\n \"GETRANGE\",\n \"EXISTS\",\n \"TYPE\",\n \"TTL\",\n \"PTTL\",\n \"EXPIRETIME\",\n \"HGET\",\n \"HMGET\",\n \"HGETALL\",\n \"HKEYS\",\n \"HVALS\",\n \"HLEN\",\n \"HEXISTS\",\n \"SMEMBERS\",\n \"SISMEMBER\",\n \"SMISMEMBER\",\n \"SCARD\",\n \"ZRANGE\",\n \"ZRANGEBYSCORE\",\n \"ZSCORE\",\n \"ZMSCORE\",\n \"ZRANK\",\n \"ZCARD\",\n \"ZCOUNT\",\n \"LRANGE\",\n \"LINDEX\",\n \"LLEN\",\n \"XRANGE\",\n \"XREVRANGE\",\n \"XLEN\",\n \"SCAN\",\n \"SSCAN\",\n \"HSCAN\",\n \"ZSCAN\",\n \"DBSIZE\",\n \"INFO\",\n \"CLIENT ID\",\n // writes that put a state, not a change of a state: a second issue puts the same state\n \"SET\",\n \"MSET\",\n \"SETEX\",\n \"PSETEX\",\n \"DEL\",\n \"UNLINK\",\n \"EXPIRE\",\n \"PEXPIRE\",\n \"EXPIREAT\",\n \"PEXPIREAT\",\n \"PERSIST\",\n \"HSET\",\n \"HDEL\",\n \"SADD\",\n \"SREM\",\n \"ZADD\",\n \"ZREM\",\n];\n\n/**\n * These commands change a state relative to the state before them. A second issue changes it again:\n * an INCR that the client sends again after a timeout counts two times.\n */\nconst CHANGE = [\n \"INCR\",\n \"INCRBY\",\n \"INCRBYFLOAT\",\n \"DECR\",\n \"DECRBY\",\n \"HINCRBY\",\n \"HINCRBYFLOAT\",\n \"ZINCRBY\",\n \"APPEND\",\n \"LPUSH\",\n \"RPUSH\",\n \"LPOP\",\n \"RPOP\",\n \"BLPOP\",\n \"BRPOP\",\n \"LMOVE\",\n \"BLMOVE\",\n \"SPOP\",\n \"XADD\",\n \"XACK\",\n \"PUBLISH\",\n \"EVAL\",\n \"EVALSHA\",\n \"FCALL\",\n \"MULTI\",\n \"EXEC\",\n \"GETDEL\",\n \"GETSET\",\n \"SETNX\",\n];\n\n/** These are the operations of the client: the idempotency of each command that the table names. */\nexport const REDIS_OPERATIONS: Readonly<Record<string, Operation>> = Object.fromEntries([\n ...SAME_EFFECT.map((command) => [command, { idempotent: true }] as const),\n ...CHANGE.map((command) => [command, { idempotent: false }] as const),\n]);\n\n/** This function gives the name of a command as the table writes it: upper case, and CLIENT ID with its subcommand. */\nexport const commandName = (command: string, args: readonly unknown[]): string => {\n const name = command.toUpperCase();\n if ((name === \"CLIENT\" || name === \"CONFIG\" || name === \"DEBUG\") && typeof args[0] === \"string\")\n return `${name} ${args[0].toUpperCase()}`;\n return name;\n};\n"],"mappings":";;;;;;;AAQA,MAAM,cAAc;CAElB;CACA;CACA;CACA;CACA;CACA;CACA;CACA;CACA;CACA;CACA;CACA;CACA;CACA;CACA;CACA;CACA;CACA;CACA;CACA;CACA;CACA;CACA;CACA;CACA;CACA;CACA;CACA;CACA;CACA;CACA;CACA;CACA;CACA;CACA;CACA;CACA;CACA;CACA;CACA;CACA;CACA;CAEA;CACA;CACA;CACA;CACA;CACA;CACA;CACA;CACA;CACA;CACA;CACA;CACA;CACA;CACA;CACA;CACA;AACF;;;;;AAMA,MAAM,SAAS;CACb;CACA;CACA;CACA;CACA;CACA;CACA;CACA;CACA;CACA;CACA;CACA;CACA;CACA;CACA;CACA;CACA;CACA;CACA;CACA;CACA;CACA;CACA;CACA;CACA;CACA;CACA;CACA;CACA;AACF;;AAGA,MAAa,mBAAwD,OAAO,YAAY,CACtF,GAAG,YAAY,KAAK,YAAY,CAAC,SAAS,EAAE,YAAY,KAAK,CAAC,CAAU,GACxE,GAAG,OAAO,KAAK,YAAY,CAAC,SAAS,EAAE,YAAY,MAAM,CAAC,CAAU,CACtE,CAAC;;AAGD,MAAa,eAAe,SAAiB,SAAqC;CAChF,MAAM,OAAO,QAAQ,YAAY;CACjC,KAAK,SAAS,YAAY,SAAS,YAAY,SAAS,YAAY,OAAO,KAAK,OAAO,UACrF,OAAO,GAAG,KAAK,GAAG,KAAK,EAAE,CAAC,YAAY;CACxC,OAAO;AACT"}
|