@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.
Files changed (335) hide show
  1. package/README.md +6 -2
  2. package/build/backoff.d.ts +44 -2
  3. package/build/{builder-zO48Vgoj.d.ts → builder.d.ts} +14 -101
  4. package/build/builder.js +499 -0
  5. package/build/builder.js.map +1 -0
  6. package/build/{call-context-DDhcVvn5.d.ts → call-context.d.ts} +3 -4
  7. package/build/channel.d.ts +1 -1
  8. package/build/channel.js.map +1 -1
  9. package/build/{core-BSzt4RI2.d.ts → client/core.d.ts} +32 -61
  10. package/build/{machine-BhVeHr_m.js → client/core.js} +4 -153
  11. package/build/client/core.js.map +1 -0
  12. package/build/{dial-ChwIcvd4.d.ts → client/dial.d.ts} +3 -4
  13. package/build/client/dial.js +52 -0
  14. package/build/client/dial.js.map +1 -0
  15. package/build/client/machine.js +29 -0
  16. package/build/client/machine.js.map +1 -0
  17. package/build/client/meets.d.ts +35 -0
  18. package/build/client/meets.js +78 -0
  19. package/build/client/meets.js.map +1 -0
  20. package/build/clock.d.ts +2 -2
  21. package/build/clock.js +1 -1
  22. package/build/conformance.d.ts +122 -0
  23. package/build/conformance.js +469 -0
  24. package/build/conformance.js.map +1 -0
  25. package/build/contract.d.ts +28 -0
  26. package/build/contract.js +184 -0
  27. package/build/contract.js.map +1 -0
  28. package/build/description.d.ts +161 -0
  29. package/build/description.js +11 -0
  30. package/build/description.js.map +1 -0
  31. package/build/drains.js +35 -0
  32. package/build/drains.js.map +1 -0
  33. package/build/events.d.ts +25 -0
  34. package/build/fakes.d.ts +43 -0
  35. package/build/fakes.js +93 -0
  36. package/build/fakes.js.map +1 -0
  37. package/build/grpc/client.d.ts +153 -0
  38. package/build/grpc/client.js +474 -0
  39. package/build/grpc/client.js.map +1 -0
  40. package/build/grpc/connection.d.ts +97 -0
  41. package/build/grpc/connection.js +59 -0
  42. package/build/grpc/connection.js.map +1 -0
  43. package/build/grpc/machine.d.ts +12 -0
  44. package/build/grpc/machine.js +149 -0
  45. package/build/grpc/machine.js.map +1 -0
  46. package/build/grpc/meets.d.ts +14 -0
  47. package/build/grpc/meets.js +105 -0
  48. package/build/grpc/meets.js.map +1 -0
  49. package/build/grpc/starting.d.ts +33 -0
  50. package/build/grpc/starting.js +89 -0
  51. package/build/grpc/starting.js.map +1 -0
  52. package/build/grpc.d.ts +9 -303
  53. package/build/grpc.js +7 -854
  54. package/build/health.d.ts +93 -0
  55. package/build/health.js +225 -0
  56. package/build/health.js.map +1 -0
  57. package/build/heartbeat.d.ts +6 -2
  58. package/build/heartbeat.js.map +1 -1
  59. package/build/host/discovery.d.ts +61 -0
  60. package/build/host/discovery.js +113 -0
  61. package/build/host/discovery.js.map +1 -0
  62. package/build/host/env.d.ts +14 -0
  63. package/build/host/env.js +57 -0
  64. package/build/host/env.js.map +1 -0
  65. package/build/host/fakes.d.ts +82 -0
  66. package/build/host/fakes.js +239 -0
  67. package/build/host/fakes.js.map +1 -0
  68. package/build/host/group.d.ts +142 -0
  69. package/build/host/group.js +524 -0
  70. package/build/host/group.js.map +1 -0
  71. package/build/host/install.d.ts +96 -0
  72. package/build/host/install.js +330 -0
  73. package/build/host/install.js.map +1 -0
  74. package/build/host/lock.d.ts +78 -0
  75. package/build/host/lock.js +190 -0
  76. package/build/host/lock.js.map +1 -0
  77. package/build/host/logs.d.ts +26 -0
  78. package/build/host/logs.js +48 -0
  79. package/build/host/logs.js.map +1 -0
  80. package/build/host/node.d.ts +14 -0
  81. package/build/host/node.js +309 -0
  82. package/build/host/node.js.map +1 -0
  83. package/build/{ports-B1ayTL-j.d.ts → host/ports.d.ts} +15 -16
  84. package/build/host/ports.js +17 -0
  85. package/build/host/ports.js.map +1 -0
  86. package/build/host/probes.d.ts +51 -0
  87. package/build/host/probes.js +139 -0
  88. package/build/host/probes.js.map +1 -0
  89. package/build/host/processes.d.ts +32 -0
  90. package/build/host/processes.js +63 -0
  91. package/build/host/processes.js.map +1 -0
  92. package/build/host/render.d.ts +84 -0
  93. package/build/host/render.js +235 -0
  94. package/build/host/render.js.map +1 -0
  95. package/build/host/watch.d.ts +90 -0
  96. package/build/host/watch.js +165 -0
  97. package/build/host/watch.js.map +1 -0
  98. package/build/host.d.ts +13 -671
  99. package/build/host.js +12 -2126
  100. package/build/http/answers.js +29 -0
  101. package/build/http/answers.js.map +1 -0
  102. package/build/http/fake-server.js +61 -0
  103. package/build/http/fake-server.js.map +1 -0
  104. package/build/http/outcome.d.ts +115 -0
  105. package/build/http/outcome.js +325 -0
  106. package/build/http/outcome.js.map +1 -0
  107. package/build/http/probes.d.ts +83 -0
  108. package/build/http/probes.js +232 -0
  109. package/build/http/probes.js.map +1 -0
  110. package/build/http/transport.d.ts +165 -0
  111. package/build/http/transport.js +518 -0
  112. package/build/http/transport.js.map +1 -0
  113. package/build/http-client/client.d.ts +121 -0
  114. package/build/http-client/client.js +330 -0
  115. package/build/http-client/client.js.map +1 -0
  116. package/build/http-client/connection.d.ts +78 -0
  117. package/build/http-client/connection.js +54 -0
  118. package/build/http-client/connection.js.map +1 -0
  119. package/build/http-client/machine.d.ts +14 -0
  120. package/build/http-client/machine.js +109 -0
  121. package/build/http-client/machine.js.map +1 -0
  122. package/build/http-client/meets.d.ts +11 -0
  123. package/build/http-client/meets.js +74 -0
  124. package/build/http-client/meets.js.map +1 -0
  125. package/build/http-client/session.d.ts +87 -0
  126. package/build/http-client/session.js +99 -0
  127. package/build/http-client/session.js.map +1 -0
  128. package/build/http-client/starting.d.ts +25 -0
  129. package/build/http-client/starting.js +65 -0
  130. package/build/http-client/starting.js.map +1 -0
  131. package/build/http-client.d.ts +9 -330
  132. package/build/http-client.js +8 -707
  133. package/build/http.d.ts +5 -353
  134. package/build/http.js +3 -1086
  135. package/build/identity.d.ts +16 -0
  136. package/build/identity.js +44 -0
  137. package/build/identity.js.map +1 -0
  138. package/build/index.d.ts +35 -412
  139. package/build/index.js +25 -3278
  140. package/build/knobs.d.ts +1 -0
  141. package/build/{knobs-BJcz6m01.js → knobs.js} +2 -34
  142. package/build/knobs.js.map +1 -0
  143. package/build/latch.d.ts +29 -0
  144. package/build/latch.js +122 -0
  145. package/build/latch.js.map +1 -0
  146. package/build/{rules-HyFCBStQ.d.ts → law.d.ts} +19 -37
  147. package/build/law.js +149 -0
  148. package/build/law.js.map +1 -0
  149. package/build/lifecycle.d.ts +144 -0
  150. package/build/lifecycle.js +411 -0
  151. package/build/lifecycle.js.map +1 -0
  152. package/build/limiter/machine.d.ts +40 -0
  153. package/build/limiter/machine.js +279 -0
  154. package/build/limiter/machine.js.map +1 -0
  155. package/build/{policy-CBcK2XGg.d.ts → limiter/policy.d.ts} +28 -29
  156. package/build/{policy-UYALkqVq.js → limiter/policy.js} +10 -9
  157. package/build/limiter/policy.js.map +1 -0
  158. package/build/{limiter-BNKnwzeY.d.ts → limiter/quota.d.ts} +32 -74
  159. package/build/{limiter-DrM0PQV9.js → limiter/quota.js} +3 -418
  160. package/build/limiter/quota.js.map +1 -0
  161. package/build/{store-DFwFKrsB.d.ts → limiter/store.d.ts} +10 -11
  162. package/build/limiter/store.js +145 -0
  163. package/build/limiter/store.js.map +1 -0
  164. package/build/limiter.d.ts +4 -3
  165. package/build/limiter.js +4 -2
  166. package/build/lint.d.ts +152 -0
  167. package/build/lint.js +576 -0
  168. package/build/lint.js.map +1 -0
  169. package/build/logger.d.ts +71 -0
  170. package/build/logger.js +183 -0
  171. package/build/logger.js.map +1 -0
  172. package/build/machines.d.ts +41 -0
  173. package/build/machines.js +451 -0
  174. package/build/machines.js.map +1 -0
  175. package/build/metrics.d.ts +53 -0
  176. package/build/metrics.js +111 -0
  177. package/build/metrics.js.map +1 -0
  178. package/build/mongodb/client.d.ts +131 -0
  179. package/build/mongodb/client.js +388 -0
  180. package/build/mongodb/client.js.map +1 -0
  181. package/build/mongodb/connection.d.ts +112 -0
  182. package/build/mongodb/connection.js +12 -0
  183. package/build/mongodb/connection.js.map +1 -0
  184. package/build/mongodb/machine.d.ts +14 -0
  185. package/build/mongodb/machine.js +122 -0
  186. package/build/mongodb/machine.js.map +1 -0
  187. package/build/mongodb/meets.d.ts +15 -0
  188. package/build/mongodb/meets.js +103 -0
  189. package/build/mongodb/meets.js.map +1 -0
  190. package/build/mongodb/starting.d.ts +29 -0
  191. package/build/mongodb/starting.js +77 -0
  192. package/build/mongodb/starting.js.map +1 -0
  193. package/build/mongodb.d.ts +8 -296
  194. package/build/mongodb.js +7 -682
  195. package/build/operation.d.ts +27 -0
  196. package/build/{operation-DF73RZOI.js → operation.js} +2 -2
  197. package/build/operation.js.map +1 -0
  198. package/build/otel.d.ts +2 -2
  199. package/build/pg/client.d.ts +96 -0
  200. package/build/pg/client.js +277 -0
  201. package/build/pg/client.js.map +1 -0
  202. package/build/pg/connection.d.ts +75 -0
  203. package/build/pg/connection.js +11 -0
  204. package/build/pg/connection.js.map +1 -0
  205. package/build/pg/machine.d.ts +13 -0
  206. package/build/pg/machine.js +111 -0
  207. package/build/pg/machine.js.map +1 -0
  208. package/build/pg/meets.d.ts +14 -0
  209. package/build/pg/meets.js +83 -0
  210. package/build/pg/meets.js.map +1 -0
  211. package/build/pg/starting.d.ts +26 -0
  212. package/build/pg/starting.js +62 -0
  213. package/build/pg/starting.js.map +1 -0
  214. package/build/pg.d.ts +8 -219
  215. package/build/pg.js +7 -525
  216. package/build/poller.d.ts +9 -0
  217. package/build/poller.js +154 -0
  218. package/build/poller.js.map +1 -0
  219. package/build/process.d.ts +114 -0
  220. package/build/process.js +170 -0
  221. package/build/process.js.map +1 -0
  222. package/build/profiles.d.ts +68 -0
  223. package/build/profiles.js +120 -0
  224. package/build/profiles.js.map +1 -0
  225. package/build/{queue-DXdw1vLO.d.ts → queue.d.ts} +16 -29
  226. package/build/queue.js +279 -0
  227. package/build/queue.js.map +1 -0
  228. package/build/rabbitmq/client.d.ts +144 -0
  229. package/build/rabbitmq/client.js +426 -0
  230. package/build/rabbitmq/client.js.map +1 -0
  231. package/build/rabbitmq/connection.d.ts +91 -0
  232. package/build/rabbitmq/connection.js +15 -0
  233. package/build/rabbitmq/connection.js.map +1 -0
  234. package/build/rabbitmq/machine.d.ts +13 -0
  235. package/build/rabbitmq/machine.js +159 -0
  236. package/build/rabbitmq/machine.js.map +1 -0
  237. package/build/rabbitmq/meets.d.ts +12 -0
  238. package/build/rabbitmq/meets.js +96 -0
  239. package/build/rabbitmq/meets.js.map +1 -0
  240. package/build/rabbitmq/starting.d.ts +27 -0
  241. package/build/rabbitmq/starting.js +67 -0
  242. package/build/rabbitmq/starting.js.map +1 -0
  243. package/build/rabbitmq.d.ts +8 -283
  244. package/build/rabbitmq.js +7 -742
  245. package/build/redis/client.d.ts +86 -0
  246. package/build/redis/client.js +285 -0
  247. package/build/redis/client.js.map +1 -0
  248. package/build/redis/connection.d.ts +57 -0
  249. package/build/redis/connection.js +11 -0
  250. package/build/redis/connection.js.map +1 -0
  251. package/build/redis/limiter.d.ts +43 -0
  252. package/build/redis/limiter.js +164 -0
  253. package/build/redis/limiter.js.map +1 -0
  254. package/build/redis/machine.d.ts +12 -0
  255. package/build/redis/machine.js +144 -0
  256. package/build/redis/machine.js.map +1 -0
  257. package/build/redis/meets.d.ts +11 -0
  258. package/build/redis/meets.js +75 -0
  259. package/build/redis/meets.js.map +1 -0
  260. package/build/redis/operations.d.ts +5 -0
  261. package/build/redis/operations.js +114 -0
  262. package/build/redis/operations.js.map +1 -0
  263. package/build/redis.d.ts +10 -207
  264. package/build/redis.js +9 -771
  265. package/build/requests.d.ts +66 -0
  266. package/build/requests.js +279 -0
  267. package/build/requests.js.map +1 -0
  268. package/build/retrier.d.ts +62 -0
  269. package/build/{retrier-Dnjhvxol.js → retrier.js} +2 -2
  270. package/build/retrier.js.map +1 -0
  271. package/build/routes.d.ts +35 -0
  272. package/build/routes.js +77 -0
  273. package/build/routes.js.map +1 -0
  274. package/build/rules.d.ts +19 -2
  275. package/build/stops.d.ts +51 -0
  276. package/build/stops.js +86 -0
  277. package/build/stops.js.map +1 -0
  278. package/build/testing.d.ts +8 -270
  279. package/build/testing.js +4 -1027
  280. package/build/testing.js.map +1 -1
  281. package/build/timing.d.ts +63 -0
  282. package/build/{timing-DZl36uXs.js → timing.js} +2 -2
  283. package/build/timing.js.map +1 -0
  284. package/build/units.d.ts +17 -0
  285. package/build/units.js +236 -0
  286. package/build/units.js.map +1 -0
  287. package/build/watchdog.d.ts +38 -0
  288. package/build/watchdog.js +57 -0
  289. package/build/watchdog.js.map +1 -0
  290. package/build/websocket/client.d.ts +136 -0
  291. package/build/websocket/client.js +422 -0
  292. package/build/websocket/client.js.map +1 -0
  293. package/build/websocket/connection.d.ts +83 -0
  294. package/build/websocket/connection.js +57 -0
  295. package/build/websocket/connection.js.map +1 -0
  296. package/build/websocket/machine.d.ts +12 -0
  297. package/build/websocket/machine.js +155 -0
  298. package/build/websocket/machine.js.map +1 -0
  299. package/build/websocket/meets.d.ts +14 -0
  300. package/build/websocket/meets.js +92 -0
  301. package/build/websocket/meets.js.map +1 -0
  302. package/build/websocket/starting.d.ts +25 -0
  303. package/build/websocket/starting.js +69 -0
  304. package/build/websocket/starting.js.map +1 -0
  305. package/build/websocket.d.ts +9 -264
  306. package/build/websocket.js +8 -774
  307. package/build/worker.d.ts +92 -0
  308. package/build/worker.js +153 -0
  309. package/build/worker.js.map +1 -0
  310. package/package.json +5 -4
  311. package/build/backoff-QTRGfkEK.d.ts +0 -45
  312. package/build/clock-DpVgpY4h.d.ts +0 -2
  313. package/build/grpc.js.map +0 -1
  314. package/build/host.js.map +0 -1
  315. package/build/http-client.js.map +0 -1
  316. package/build/http.js.map +0 -1
  317. package/build/index.js.map +0 -1
  318. package/build/knobs-BJcz6m01.js.map +0 -1
  319. package/build/lifecycle-D0J-zwZF.d.ts +0 -374
  320. package/build/limiter-DrM0PQV9.js.map +0 -1
  321. package/build/logger-B6drv9w0.d.ts +0 -488
  322. package/build/machine-BhVeHr_m.js.map +0 -1
  323. package/build/mongodb.js.map +0 -1
  324. package/build/operation-DF73RZOI.js.map +0 -1
  325. package/build/pg.js.map +0 -1
  326. package/build/policy-UYALkqVq.js.map +0 -1
  327. package/build/process-7R9M29wN.js +0 -437
  328. package/build/process-7R9M29wN.js.map +0 -1
  329. package/build/rabbitmq.js.map +0 -1
  330. package/build/redis.js.map +0 -1
  331. package/build/requests-DD2ALv0_.js +0 -583
  332. package/build/requests-DD2ALv0_.js.map +0 -1
  333. package/build/retrier-Dnjhvxol.js.map +0 -1
  334. package/build/timing-DZl36uXs.js.map +0 -1
  335. package/build/websocket.js.map +0 -1
package/build/pg.js CHANGED
@@ -1,526 +1,8 @@
1
- import { B as zeroProblems, C as notSent, E as poolProblems, F as timeProblems, G as immediate, J as keepaliveSilent, K as keepaliveAnswered, L as wholeProblems, M as sendWithReissue, N as socketEnds, P as stepChecks, R as withinBound, S as ms, T as orZero, U as WHAT, V as MANIFESTATIONS, W as firstDetector, X as unwatched, Y as kernelGiveUp, _ as inFlight, a as helloDial, b as message, d as clientProblems, f as clientRecorder, h as finiteMs, j as roundTripCheck, l as attachClient, m as dialCheck, n as closeTimer, p as deadlineProblem, q as keepaliveReset, r as transition, s as STARTING_POLL, t as clientKnob, u as clientBounds, w as operationTable, y as localStateCheck } from "./machine-BhVeHr_m.js";
2
- import { realClock } from "./clock.js";
3
- // src/pg/connection.ts
4
- /**
5
- * This factory makes a `pg.Pool`. It loads pg only when a client connects, thus a service that does
6
- * not use PostgreSQL does not need pg. The specifier is a value, thus the package needs no types of pg.
7
- */
8
- const nodePostgres = async (options) => {
9
- return new (await (import("pg"))).default.Pool(options);
10
- };
11
- // src/pg/meets.ts
12
- /**
13
- * This function gives the manifestations that a pg client can meet, and the detectors of each, from
14
- * its knobs. The cells of S1 measured each one on node-postgres 8.23.1:
15
- * - A reset of a query gives ECONNRESET in 0.6 ms, and a FIN to an idle client gives EOF in 18 µs.
16
- * - A frozen server and a black hole have no detector in 60 s at the defaults: pg has no timer on a query and no keepalive.
17
- * - `query_timeout` ends a query in a black hole on time (9.503 s for 9.5 s).
18
- * - `statement_timeout` ends a slow statement on the server with ERROR 57014 (10.002 s for 10 s).
19
- *
20
- * pg has no watchdog and does not test an idle client. In place of a watchdog, the pool ends an
21
- * idle client at its idle timer. A later checkout then opens a new connection, which `connect_bound` bounds.
22
- */
23
- const pgMeetings = (name, knobs) => {
24
- const query = {
25
- by: "request_timeout",
26
- within: knobs.request_timeout,
27
- situation: "in flight",
28
- effect: `the query fails as indeterminate, and ${name} gives the client back with an error, thus the pool ends it`
29
- };
30
- const idleExpiry = {
31
- by: "pool_idle_expiry",
32
- within: knobs.pool_idle_expiry,
33
- situation: "idle",
34
- effect: "the pool ends the idle client, thus no checkout gets the dead connection"
35
- };
36
- const giveUp = kernelGiveUp(knobs.retrans_giveup_time, "the query fails with its error");
37
- const silent = (manifestation) => ({
38
- manifestation,
39
- what: WHAT[manifestation],
40
- detectors: [
41
- query,
42
- giveUp,
43
- idleExpiry,
44
- keepaliveSilent(knobs.keepalive_idle)
45
- ]
46
- });
47
- return [
48
- {
49
- manifestation: "rst",
50
- what: WHAT.rst,
51
- detectors: [
52
- immediate("in flight", "the query fails with ECONNRESET, and each new connect is refused"),
53
- idleExpiry,
54
- keepaliveReset(knobs.keepalive_idle)
55
- ]
56
- },
57
- {
58
- manifestation: "fin",
59
- what: WHAT.fin,
60
- detectors: [immediate("in flight", "the query fails: 'Connection terminated unexpectedly'"), immediate("idle", "the pool removes the idle client, and emits error")]
61
- },
62
- silent("blackhole"),
63
- silent("freeze-host"),
64
- {
65
- manifestation: "freeze-process",
66
- what: WHAT["freeze-process"],
67
- detectors: [
68
- query,
69
- idleExpiry,
70
- keepaliveAnswered()
71
- ]
72
- },
73
- {
74
- manifestation: "app-stall",
75
- what: "the server stops its replies for a time, for example behind a lock, and its kernel stays up",
76
- detectors: [
77
- {
78
- by: "statement_timeout",
79
- within: knobs.statement_timeout,
80
- situation: "in flight",
81
- effect: "the server ends the statement with ERROR 57014, a certain outcome, and the client stays"
82
- },
83
- query,
84
- idleExpiry,
85
- keepaliveAnswered()
86
- ]
87
- }
88
- ];
89
- };
90
- // src/pg/client.ts
91
- /** This function gives the problems of a spec: the knobs that pg cannot use. */
92
- const specProblems = (name, spec) => {
93
- const { knobs } = spec;
94
- return [
95
- ...timeProblems(name, knobs),
96
- ...zeroProblems(name, "pg", knobs, {
97
- bounded: [
98
- "connect_bound",
99
- "request_timeout",
100
- "close_bound"
101
- ],
102
- open: [
103
- "statement_timeout",
104
- "pool_idle_expiry",
105
- "keepalive_idle",
106
- "max_connection_age"
107
- ]
108
- }),
109
- ...wholeProblems(name, knobs, {
110
- millis: [
111
- "connect_bound",
112
- "request_timeout",
113
- "statement_timeout",
114
- "pool_idle_expiry"
115
- ],
116
- seconds: ["keepalive_idle", "max_connection_age"]
117
- }),
118
- ...poolProblems(name, spec.pool)
119
- ];
120
- };
121
- /** This function gives the options of `pg.Pool` from a spec. Each timer of pg comes from the declaration. */
122
- const poolOptions = (name, spec) => {
123
- const { knobs } = spec;
124
- return {
125
- host: spec.host,
126
- port: spec.port,
127
- ...spec.database === void 0 ? {} : { database: spec.database },
128
- ...spec.user === void 0 ? {} : { user: spec.user },
129
- ...spec.password === void 0 ? {} : { password: spec.password },
130
- application_name: name,
131
- max: spec.pool.max,
132
- min: spec.pool.min,
133
- connectionTimeoutMillis: ms(knobs.connect_bound),
134
- query_timeout: ms(knobs.request_timeout),
135
- statement_timeout: finiteMs(knobs.statement_timeout),
136
- idleTimeoutMillis: orZero(knobs.pool_idle_expiry),
137
- maxLifetimeSeconds: orZero(knobs.max_connection_age) / 1e3,
138
- keepAlive: knobs.keepalive_idle.value !== "infinite",
139
- keepAliveInitialDelayMillis: orZero(knobs.keepalive_idle),
140
- allowExitOnIdle: true
141
- };
142
- };
143
- /**
144
- * This dial function opens a TCP connection, sends an SSLRequest, and waits for its one byte. The
145
- * server answers it before the authentication, thus the dial needs no password and opens no backend.
146
- */
147
- const sslRequestDial = helloDial(Uint8Array.of(0, 0, 0, 8, 4, 210, 22, 47), (received) => {
148
- const answer = String.fromCharCode(received[0] ?? 0);
149
- return answer === "S" || answer === "N" ? null : /* @__PURE__ */ new Error(`the answer to the SSLRequest was ${JSON.stringify(answer)}`);
150
- });
151
- /** A database error is an ErrorResponse of the server: pg gives its severity and its SQLSTATE code. */
152
- const replyOf = (error) => {
153
- if (typeof error !== "object" || error === null) return null;
154
- const { severity, code } = error;
155
- return typeof severity === "string" && typeof code === "string" && /^[0-9A-Z]{5}$/.test(code) ? {
156
- severity,
157
- code
158
- } : null;
159
- };
160
- /**
161
- * This function makes a PostgreSQL client, a dependency of a service over a `pg.Pool`.
162
- * - It sets each timer of pg from the declaration. The driver sends nothing again by itself, and the client
163
- * sends again only an idempotent operation of its table.
164
- * - It tells the outcome of a query in three values. An ERROR of the server is certain. A FATAL
165
- * ends the session, thus its outcome is not certain.
166
- * - It gives back a client whose query has no certain outcome with an error. The pool then ends it,
167
- * thus an orphaned query does not hold the next checkout.
168
- * - It listens to each error of the pool and of a client that it holds. In Node, an error with no
169
- * listener throws, and library code does not end the process (A5).
170
- * - It records the process id of the backend of each connection (D1).
171
- *
172
- * The outcomes, the second try, the queries in flight, the bounded close and the checks come from
173
- * the core of the clients. This entry gives what pg does in its own way.
174
- */
175
- const pgClient = (name, spec, options = {}) => {
176
- const problems = specProblems(name, spec);
177
- if (problems.length > 0) throw new Error(problems.join("; "));
178
- const { knobs } = spec;
179
- const clock = options.clock ?? realClock;
180
- const create = options.create ?? nodePostgres;
181
- const dial = options.dial ?? sslRequestDial;
182
- const operations = operationTable({}, spec.operations);
183
- const requestTimeout = ms(knobs.request_timeout);
184
- let pool = null;
185
- let status = "none";
186
- const joins = [];
187
- const open = /* @__PURE__ */ new Set();
188
- const flight = inFlight();
189
- const emit = clientRecorder(options.record, clock, name);
190
- const record = (on, to = status, detail) => {
191
- const from = status;
192
- status = to;
193
- emit(from, to, on, detail);
194
- };
195
- const closing = () => status === "closing" || status === "end";
196
- const onConnect = (client) => {
197
- open.add(client);
198
- const generation = joins.length + 1;
199
- const pid = client.processID;
200
- joins.push({
201
- generation,
202
- at: clock.now(),
203
- backendPid: typeof pid === "number" ? pid : null,
204
- ...socketEnds(client.connection?.stream)
205
- });
206
- record("join", closing() ? status : "ready", `generation ${generation}, backend ${pid ?? "unknown"}`);
207
- };
208
- const onRemove = (client) => {
209
- open.delete(client);
210
- record("client.removed");
211
- };
212
- const onPoolError = (error) => {
213
- if (status !== "end") record("client.error", status, message(error));
214
- };
215
- /** This function records a failed checkout: a full pool at its bound, or a connect that failed. */
216
- const checkoutFailed = (error) => {
217
- const text = message(error);
218
- if (/timeout exceeded when trying to connect/i.test(text)) record("checkout.timeout", status, text);
219
- else record("connect.failed", closing() ? status : "failing", text);
220
- };
221
- /** This function checks out a client, or gives the reason why the query cannot leave the client. */
222
- const checkout = (signal) => {
223
- const p = pool;
224
- if (p === null) return Promise.resolve(notSent(`${name} has not connected`));
225
- if (closing()) return Promise.resolve(notSent(`${name} is ${status}: the query did not leave the client`));
226
- if (signal?.aborted) return Promise.resolve(notSent("the caller aborted the query before it left the client"));
227
- return new Promise((resolve) => {
228
- let done = false;
229
- const aborted = () => {
230
- done = true;
231
- resolve(notSent("the caller abandoned the query before it left the client"));
232
- };
233
- signal?.addEventListener("abort", aborted, { once: true });
234
- p.connect().then((client) => {
235
- signal?.removeEventListener("abort", aborted);
236
- if (done) client.release();
237
- else resolve(client);
238
- }, (error) => {
239
- signal?.removeEventListener("abort", aborted);
240
- checkoutFailed(error);
241
- if (!done) resolve(notSent(message(error)));
242
- });
243
- });
244
- };
245
- /** This function makes one try of a query: a checkout, the query, and the release. */
246
- const attempt = async (text, values, signal) => {
247
- const got = await checkout(signal);
248
- if (!("release" in got)) return got;
249
- const client = got;
250
- const onClientError = (error) => record("client.error", status, message(error));
251
- client.on("error", onClientError);
252
- return flight.track(signal, "the caller abandoned the query after it was sent", (settle) => {
253
- client.query(text, values).then((result) => settle({
254
- outcome: "ok",
255
- value: {
256
- rows: result.rows,
257
- rowCount: result.rowCount
258
- }
259
- }), (error) => {
260
- const reply = replyOf(error);
261
- const said = reply === null ? message(error) : `${reply.code} ${message(error)}`;
262
- if (reply !== null && reply.severity === "ERROR") settle({
263
- outcome: "error",
264
- error: said,
265
- reply: true
266
- });
267
- else settle({
268
- outcome: "indeterminate",
269
- error: said
270
- }, error instanceof Error ? error : new Error(said));
271
- });
272
- }, (_value, cause) => {
273
- client.off("error", onClientError);
274
- client.release(cause);
275
- });
276
- };
277
- const query = (operation, text, values, sendOptions = {}) => sendWithReissue({
278
- clock,
279
- bound: requestTimeout,
280
- operation: operations[operation],
281
- options: sendOptions,
282
- attempt: (signal) => attempt(text, values, signal),
283
- onReissue: () => record("command.reissue", status, operation)
284
- });
285
- const handle = {
286
- async connect(signal) {
287
- record("status.connecting", "connecting");
288
- const p = await create(poolOptions(name, spec));
289
- pool = p;
290
- p.on("connect", onConnect);
291
- p.on("remove", onRemove);
292
- p.on("error", onPoolError);
293
- if (signal.aborted) throw new Error(`${name}: the service stopped before the connect`);
294
- const first = await checkout(signal);
295
- if ("release" in first) first.release();
296
- },
297
- async close() {
298
- const p = pool;
299
- if (p === null || closing()) return;
300
- record("close.request", "closing");
301
- if (!await withinBound(clock, ms(knobs.close_bound), p.end())) {
302
- record("close.bound");
303
- for (const client of [...open]) client.connection?.stream?.destroy?.();
304
- record("close.destroy", status, `${open.size} sockets`);
305
- }
306
- flight.settleAll("the client closed with the query in flight");
307
- p.off("connect", onConnect);
308
- p.off("remove", onRemove);
309
- record("status.end", "end");
310
- }
311
- };
312
- const declaration = {
313
- system: spec.system,
314
- knobs: {
315
- connect_bound: knobs.connect_bound,
316
- request_timeout: knobs.request_timeout,
317
- statement_timeout: knobs.statement_timeout,
318
- pool_idle_expiry: knobs.pool_idle_expiry,
319
- keepalive_idle: knobs.keepalive_idle,
320
- idle_refresh_interval: knobs.keepalive_idle,
321
- max_connection_age: knobs.max_connection_age,
322
- close_bound: knobs.close_bound,
323
- retrans_giveup_time: knobs.retrans_giveup_time
324
- },
325
- operations
326
- };
327
- const checks = (checkOptions) => stepChecks(name, checkOptions, {
328
- "local-state": localStateCheck(() => status === "ready"),
329
- checkout: {
330
- ownConnection: true,
331
- run: async (signal) => {
332
- const got = await checkout(signal);
333
- if (!("release" in got)) return "error";
334
- got.release();
335
- return "ok";
336
- }
337
- },
338
- "round-trip": roundTripCheck((sendOptions) => query("check", "SELECT 1", [], sendOptions)),
339
- dial: dialCheck(dial, spec.host, spec.port)
340
- });
341
- return {
342
- name,
343
- declaration,
344
- handle,
345
- meets: pgMeetings(name, knobs),
346
- get status() {
347
- return status;
348
- },
349
- joins,
350
- query,
351
- checks,
352
- attach: (svc, checkOptions) => attachClient(svc, name, declaration, handle, checks(checkOptions)),
353
- problems(description) {
354
- const deadline = deadlineProblem(name, knobs.request_timeout, knobs.statement_timeout, "statement");
355
- return [...clientProblems(name, description, clientBounds(knobs)), ...deadline === null ? [] : [deadline]];
356
- }
357
- };
358
- };
359
- // src/pg/machine.ts
360
- /** The states where the pool exists and serves. */
361
- const SERVING = [
362
- "connecting",
363
- "ready",
364
- "failing"
365
- ];
366
- const OPEN = [...SERVING, "closing"];
367
- /**
368
- * The pg client of the service as a `mark1russell7.machine/1` document. It is the pool as the
369
- * wrapper sees it, not one connection: the document `node-postgres` of `@mark1russell7/machines`
370
- * gives each client of the pool. The state is the outcome of the last connect:
371
- * - `ready`: the last connect of the pool succeeded.
372
- * - `failing`: the last connect failed. pg does not connect again by itself. The next checkout tries again.
373
- *
374
- * An idle client that fails and a client that the pool removes do not change the state. The pool
375
- * removed that client, and the next checkout opens a new connection. A test records the transitions
376
- * of the client and replays them against this document.
377
- */
378
- const PG_CLIENT_MACHINE = {
379
- format: "mark1russell7.machine/1",
380
- id: "service.machine.pg-client",
381
- title: "The PostgreSQL client of the service, over a pool of node-postgres 8",
382
- layer: "driver",
383
- version: "0.1.0",
384
- sources: [{
385
- ref: "packages/service/src/pg (the client implements this document)",
386
- rung: "implemented"
387
- }],
388
- states: {
389
- none: {
390
- initial: true,
391
- note: "the client exists, and its pool does not"
392
- },
393
- connecting: { note: "the first checkout of the startup opens the first connection" },
394
- ready: { note: "the last connect succeeded. A dead idle client stays hidden until the pool ends it or a query meets it" },
395
- failing: { note: "the last connect failed. A checkout of a full pool that times out does not change the state" },
396
- closing: { note: "the end of the pool: each idle client sends Terminate, and each held client goes at its release" },
397
- end: {
398
- final: true,
399
- note: "the service closed the client"
400
- }
401
- },
402
- events: {
403
- "status.connecting": { kind: "local" },
404
- join: {
405
- kind: "rx",
406
- note: "the first ReadyForQuery of a new connection, with the process id of its backend (D1)"
407
- },
408
- "connect.failed": {
409
- kind: "local",
410
- note: "a connect failed: its error, a close, a FATAL of the server, or connect_bound"
411
- },
412
- "checkout.timeout": {
413
- kind: "timer",
414
- note: "a checkout of a full pool waited connect_bound"
415
- },
416
- "client.error": {
417
- kind: "rx",
418
- note: "an error of an idle client, or of a client that the service holds"
419
- },
420
- "client.removed": {
421
- kind: "local",
422
- note: "the socket of a client of the pool closed"
423
- },
424
- "command.reissue": { kind: "local" },
425
- "close.request": { kind: "local" },
426
- "close.bound": { kind: "timer" },
427
- "close.destroy": {
428
- kind: "local",
429
- note: "the bound destroys each socket that a silent server does not close"
430
- },
431
- "status.end": { kind: "local" }
432
- },
433
- knobs: {
434
- connect_bound: clientKnob("connectionTimeoutMillis"),
435
- close_bound: clientKnob("close.bound")
436
- },
437
- timers: {
438
- connect_timeout: {
439
- duration: {
440
- knob: "connect_bound",
441
- unit: "s"
442
- },
443
- arms: ["enter:connecting"],
444
- cancels: ["join", "connect.failed"],
445
- fires: "connect.failed",
446
- note: "pg-pool destroys the socket of the attempt: 'Connection terminated due to connection timeout'"
447
- },
448
- close: closeTimer()
449
- },
450
- transitions: [
451
- transition("connect", "none", "status.connecting", "connecting"),
452
- transition("join", [...SERVING], "join", "ready"),
453
- transition("connect-failed", [...SERVING], "connect.failed", "failing", { timer: "connect_timeout" }),
454
- transition("join-closing", "closing", "join", "closing"),
455
- transition("connect-failed-closing", "closing", "connect.failed", "closing"),
456
- ...OPEN.map((state) => transition(`checkout-timeout-${state}`, state, "checkout.timeout", state)),
457
- ...OPEN.map((state) => transition(`client-error-${state}`, state, "client.error", state)),
458
- ...OPEN.map((state) => transition(`removed-${state}`, state, "client.removed", state)),
459
- ...OPEN.map((state) => transition(`reissue-${state}`, state, "command.reissue", state)),
460
- transition("close", [...SERVING], "close.request", "closing"),
461
- transition("close-bound", "closing", "close.bound", "closing", { timer: "close" }),
462
- transition("close-destroy", "closing", "close.destroy", "closing"),
463
- transition("end", "closing", "status.end", "end")
464
- ]
465
- };
466
- // src/pg/starting.ts
467
- /**
468
- * This poll is the poll of the checks that `PG_STARTING_KNOBS` fits. It is `STARTING_POLL` of the
469
- * core: each 5 s with a jitter of 20 %, and a timeout of 4 s (B6, O12). The poll does not depend on the driver.
470
- */
471
- const PG_STARTING_POLL = STARTING_POLL;
472
- /**
473
- * These knobs are a declaration to start from, from what the cells of S1 measured on node-postgres
474
- * 8.23.1 against PostgreSQL 17.11. The runtime has no defaults: a service gives these knobs, or its
475
- * own. Each value holds the borders that the cells measured:
476
- * - `connect_bound` 5 s: at the defaults pg has no bound, and the kernel gives up on the SYNs only
477
- * after 131 s. The timer of the pool fired 7 ms late at most. Thus 5 s is in a startup budget of 30 s (O15).
478
- * - `request_timeout` 9.5 s: the cells ended a query in a black hole at 9.503 s. That is in the slowest poll of 10 s (O13).
479
- * - `statement_timeout` 9 s: below `request_timeout`, thus the server ends a slow statement first,
480
- * with ERROR 57014 (O07). The cells measured that end 2 ms after the deadline.
481
- * - `pool_idle_expiry` 10 s: the idle timer of pg-pool at its defaults, measured at 10.004 s. It is
482
- * below a router that forgets an idle flow at 12 s (O03), and below a `client_idle_timeout` of PgBouncer at 12 s (O01).
483
- * - `keepalive_idle` 30 s: at the defaults pg has no keepalive. This value refreshes a held idle
484
- * connection through the NAT forms of the cells (240 s and 350 s).
485
- * - `max_connection_age` "infinite": no cell measured an age, thus the declaration gives none.
486
- * - `close_bound` 2 s: behind a black hole the end of the pool waits with no bound. The cells saw
487
- * the pool emit `remove` only when the socket closes.
488
- * - `retrans_giveup_time` 924.6 s: the give-up of Linux at `tcp_retries2 = 15`. The cells measured 3.120 s at `tcp_retries2 = 3`.
489
- */
490
- const PG_STARTING_KNOBS = {
491
- connect_bound: {
492
- value: 5,
493
- unit: "s"
494
- },
495
- request_timeout: {
496
- value: 9.5,
497
- unit: "s"
498
- },
499
- statement_timeout: {
500
- value: 9,
501
- unit: "s"
502
- },
503
- pool_idle_expiry: {
504
- value: 10,
505
- unit: "s"
506
- },
507
- keepalive_idle: {
508
- value: 30,
509
- unit: "s"
510
- },
511
- max_connection_age: {
512
- value: "infinite",
513
- unit: "s"
514
- },
515
- close_bound: {
516
- value: 2,
517
- unit: "s"
518
- },
519
- retrans_giveup_time: {
520
- value: 924.6,
521
- unit: "s"
522
- }
523
- };
1
+ import { STARTING_POLL } from "./client/core.js";
2
+ import { MANIFESTATIONS, firstDetector, unwatched } from "./client/meets.js";
3
+ import { pgClient, poolOptions, specProblems, sslRequestDial } from "./pg/client.js";
4
+ import { nodePostgres } from "./pg/connection.js";
5
+ import { PG_CLIENT_MACHINE } from "./pg/machine.js";
6
+ import { pgMeetings } from "./pg/meets.js";
7
+ import { PG_STARTING_KNOBS, PG_STARTING_POLL } from "./pg/starting.js";
524
8
  export { MANIFESTATIONS, PG_CLIENT_MACHINE, PG_STARTING_KNOBS, PG_STARTING_POLL, STARTING_POLL, firstDetector, nodePostgres, pgClient, pgMeetings, poolOptions, specProblems, sslRequestDial, unwatched };
525
-
526
- //# sourceMappingURL=pg.js.map
@@ -0,0 +1,9 @@
1
+ import "./clock.js";
2
+ import { Outcome } from "./latch.js";
3
+ import "./timing.js";
4
+ /**
5
+ * A check function measures one step of a connection. It ends with nothing or `"ok"` for a pass,
6
+ * or with `"error"` or `"indeterminate"`. A throw is an error with a flag. The runtime aborts the
7
+ * signal at the timeout of the poll.
8
+ */
9
+ export type CheckFn = (signal: AbortSignal) => Outcome | void | Promise<Outcome | void>;
@@ -0,0 +1,154 @@
1
+ import { valueIn } from "@mark1russell7/quantity";
2
+ // src/poller.ts
3
+ const message = (error) => error instanceof Error ? error.message : String(error);
4
+ /**
5
+ * This function makes a poller. The loop is on a fixed delay from the start of each poll. Thus the
6
+ * next poll starts one interval after the current poll starts, with the jitter (±20 % is 0.2).
7
+ * The timing algebra of the declaration uses this model.
8
+ *
9
+ * Each poll makes a new measurement. While a use of the check is pending, the poller does not
10
+ * start the check again. Such a poll fails at once with the same cause, and the sequence of
11
+ * failures continues.
12
+ */
13
+ const poller = (options) => {
14
+ const { clock, poll, run } = options;
15
+ const interval = valueIn(poll.interval, "ms");
16
+ const timeout = valueIn(poll.timeout, "ms");
17
+ let last = null;
18
+ let next = null;
19
+ let stopped = false;
20
+ let looping = false;
21
+ let pending = null;
22
+ const active = /* @__PURE__ */ new Set();
23
+ /** This function gives the name of the state, as the machine document of the poller names it. */
24
+ const named = () => stopped ? "stopped" : pending !== null ? pending.late ? "hung" : "calling" : looping ? "waiting" : "idle";
25
+ const step = (on, change) => {
26
+ const from = named();
27
+ change();
28
+ options.trace?.(from, named(), on);
29
+ };
30
+ const result = (outcome, started, fields = {}) => ({
31
+ outcome,
32
+ at: clock.now(),
33
+ took: clock.now() - started,
34
+ cause: fields.cause ?? null,
35
+ timedOut: fields.timedOut ?? false,
36
+ thrown: fields.thrown ?? false,
37
+ forced: fields.forced ?? false
38
+ });
39
+ const once = () => {
40
+ const started = clock.now();
41
+ if (options.forced()) return Promise.resolve(result("error", started, {
42
+ cause: "the force-fail list names it",
43
+ forced: true
44
+ }));
45
+ if (pending !== null) {
46
+ const cause = `its call of ${Math.round(started - pending.since)} ms ago is still pending`;
47
+ return Promise.resolve(result("error", started, { cause }));
48
+ }
49
+ const controller = new AbortController();
50
+ return new Promise((resolve) => {
51
+ let done = false;
52
+ const finish = (value) => {
53
+ if (done) return;
54
+ done = true;
55
+ active.delete(abandon);
56
+ cancel();
57
+ resolve(value);
58
+ };
59
+ const abandon = () => {
60
+ controller.abort();
61
+ finish(result("error", started, { cause: "the runtime stopped" }));
62
+ };
63
+ active.add(abandon);
64
+ const call = {
65
+ since: started,
66
+ late: false
67
+ };
68
+ pending = call;
69
+ const cancel = clock.after(timeout, () => {
70
+ controller.abort();
71
+ step("timeout", () => {
72
+ if (pending === call) call.late = true;
73
+ });
74
+ finish(result("error", started, {
75
+ cause: `no answer in ${timeout} ms`,
76
+ timedOut: true
77
+ }));
78
+ });
79
+ const settled = () => {
80
+ if (pending !== call) return;
81
+ step(call.late ? "late-answer" : "answer", () => {
82
+ pending = null;
83
+ });
84
+ };
85
+ let answer;
86
+ try {
87
+ answer = Promise.resolve(run(controller.signal));
88
+ } catch (error) {
89
+ answer = Promise.reject(error);
90
+ }
91
+ answer.then((value) => {
92
+ settled();
93
+ const outcome = value ?? "ok";
94
+ finish(result(outcome, started, outcome === "ok" ? {} : { cause: `the check gave ${outcome}` }));
95
+ }, (error) => {
96
+ settled();
97
+ finish(result("error", started, {
98
+ cause: message(error),
99
+ thrown: true
100
+ }));
101
+ });
102
+ });
103
+ };
104
+ const handle = (value) => {
105
+ last = value;
106
+ if (!stopped) options.record(value);
107
+ return value;
108
+ };
109
+ const jittered = () => interval * (1 + poll.jitter * (2 * options.random() - 1));
110
+ const tick = () => {
111
+ next = null;
112
+ if (stopped) return;
113
+ let polled;
114
+ step("tick", () => {
115
+ next = clock.after(jittered(), tick);
116
+ if (options.unrefLoop === true) next.unref?.();
117
+ polled = once();
118
+ });
119
+ polled.then(handle);
120
+ };
121
+ return {
122
+ get last() {
123
+ return last;
124
+ },
125
+ pollNow: () => {
126
+ let polled;
127
+ step("sweep", () => {
128
+ polled = once();
129
+ });
130
+ return polled.then(handle);
131
+ },
132
+ start(delay) {
133
+ if (stopped || next !== null) return;
134
+ step("start", () => {
135
+ looping = true;
136
+ next = clock.after(Math.max(0, delay), tick);
137
+ if (options.unrefLoop === true) next.unref?.();
138
+ });
139
+ },
140
+ jittered,
141
+ stop() {
142
+ if (stopped) return;
143
+ step("stop", () => {
144
+ stopped = true;
145
+ next?.();
146
+ next = null;
147
+ });
148
+ for (const abandon of [...active]) abandon();
149
+ }
150
+ };
151
+ };
152
+ export { poller };
153
+
154
+ //# sourceMappingURL=poller.js.map