@distilled.cloud/core 1.0.0-rc.2 → 1.0.0-rc.21
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/LICENSE +201 -0
- package/README.md +33 -0
- package/lib/_bundle/aws.js +2 -0
- package/lib/_bundle/aws.js.map +1 -0
- package/lib/_bundle/base.js +5 -0
- package/lib/_bundle/base.js.map +1 -0
- package/lib/_bundle/graphql.js +3 -0
- package/lib/_bundle/graphql.js.map +1 -0
- package/lib/_bundle/grpc.js +3 -0
- package/lib/_bundle/grpc.js.map +1 -0
- package/lib/_bundle/rest.js +2 -0
- package/lib/_bundle/rest.js.map +1 -0
- package/lib/_bundle/xml.js +4 -0
- package/lib/_bundle/xml.js.map +1 -0
- package/lib/annotations.d.ts +17 -0
- package/lib/annotations.d.ts.map +1 -0
- package/lib/annotations.js +2 -0
- package/lib/annotations.js.map +1 -0
- package/lib/api.d.ts +220 -51
- package/lib/api.d.ts.map +1 -1
- package/lib/api.js +1 -178
- package/lib/category.d.ts +19 -16
- package/lib/category.d.ts.map +1 -1
- package/lib/category.js +1 -280
- package/lib/chunks.d.ts +3 -0
- package/lib/chunks.d.ts.map +1 -0
- package/lib/chunks.js +1 -0
- package/lib/config.d.ts +81 -0
- package/lib/config.d.ts.map +1 -0
- package/lib/config.js +1 -0
- package/lib/crypto.d.ts +76 -0
- package/lib/crypto.d.ts.map +1 -0
- package/lib/crypto.js +1 -0
- package/lib/error-category.d.ts +1 -1
- package/lib/error-category.d.ts.map +1 -1
- package/lib/error-category.js +1 -46
- package/lib/errors.d.ts +99 -74
- package/lib/errors.d.ts.map +1 -1
- package/lib/errors.js +1 -186
- package/lib/graphql.d.ts +420 -0
- package/lib/graphql.d.ts.map +1 -0
- package/lib/graphql.js +1 -0
- package/lib/graphql.types.d.ts +11 -0
- package/lib/graphql.types.d.ts.map +1 -0
- package/lib/graphql.types.js +1 -0
- package/lib/grpc-transport-cloudflare-http2.d.ts +7 -0
- package/lib/grpc-transport-cloudflare-http2.d.ts.map +1 -0
- package/lib/grpc-transport-cloudflare-http2.js +6 -0
- package/lib/grpc-transport-cloudflare-http2.js.map +1 -0
- package/lib/grpc-transport-cloudflare-sockets.d.ts +7 -0
- package/lib/grpc-transport-cloudflare-sockets.d.ts.map +1 -0
- package/lib/grpc-transport-cloudflare-sockets.js +5 -0
- package/lib/grpc-transport-cloudflare-sockets.js.map +1 -0
- package/lib/grpc-transport-node-http2.d.ts +19 -0
- package/lib/grpc-transport-node-http2.d.ts.map +1 -0
- package/lib/grpc-transport-node-http2.js +2 -0
- package/lib/grpc-transport-node-http2.js.map +1 -0
- package/lib/grpc-transport.d.ts +111 -0
- package/lib/grpc-transport.d.ts.map +1 -0
- package/lib/grpc-transport.js +1 -0
- package/lib/hpack.d.ts +34 -0
- package/lib/hpack.d.ts.map +1 -0
- package/lib/hpack.js +2 -0
- package/lib/hpack.js.map +1 -0
- package/lib/index.d.ts +32 -0
- package/lib/index.d.ts.map +1 -0
- package/lib/index.js +17 -0
- package/lib/max-message-bytes.d.ts +42 -0
- package/lib/max-message-bytes.d.ts.map +1 -0
- package/lib/max-message-bytes.js +2 -0
- package/lib/max-message-bytes.js.map +1 -0
- package/lib/ndjson.d.ts +3 -0
- package/lib/ndjson.d.ts.map +1 -0
- package/lib/ndjson.js +2 -0
- package/lib/ndjson.js.map +1 -0
- package/lib/pagination.d.ts +105 -25
- package/lib/pagination.d.ts.map +1 -1
- package/lib/pagination.js +1 -182
- package/lib/plain-values.d.ts +21 -0
- package/lib/plain-values.d.ts.map +1 -0
- package/lib/plain-values.js +2 -0
- package/lib/plain-values.js.map +1 -0
- package/lib/protobuf.d.ts +85 -0
- package/lib/protobuf.d.ts.map +1 -0
- package/lib/protobuf.js +1 -0
- package/lib/protocol-grpc.d.ts +123 -0
- package/lib/protocol-grpc.d.ts.map +1 -0
- package/lib/protocol-grpc.js +1 -0
- package/lib/protocol-http.d.ts +206 -25
- package/lib/protocol-http.d.ts.map +1 -1
- package/lib/protocol-http.js +1 -554
- package/lib/protocol-rest.d.ts +258 -64
- package/lib/protocol-rest.d.ts.map +1 -1
- package/lib/protocol-rest.js +1 -242
- package/lib/protocol.d.ts +113 -0
- package/lib/protocol.d.ts.map +1 -0
- package/lib/protocol.js +0 -0
- package/lib/query.d.ts +29 -0
- package/lib/query.d.ts.map +1 -0
- package/lib/query.js +1 -0
- package/lib/response-validation-strict.d.ts +48 -0
- package/lib/response-validation-strict.d.ts.map +1 -0
- package/lib/response-validation-strict.js +1 -0
- package/lib/response-validation.d.ts +86 -0
- package/lib/response-validation.d.ts.map +1 -0
- package/lib/response-validation.js +1 -0
- package/lib/retry-after.d.ts +4 -4
- package/lib/retry-after.d.ts.map +1 -1
- package/lib/retry-after.js +1 -112
- package/lib/retry-state.d.ts +26 -0
- package/lib/retry-state.d.ts.map +1 -0
- package/lib/retry-state.js +2 -0
- package/lib/retry-state.js.map +1 -0
- package/lib/retry.d.ts +141 -42
- package/lib/retry.d.ts.map +1 -1
- package/lib/retry.js +1 -210
- package/lib/schema-lite-impl.d.ts +179 -0
- package/lib/schema-lite-impl.d.ts.map +1 -0
- package/lib/schema-lite-impl.js +1 -0
- package/lib/schema-lite-typed.d.ts +41 -0
- package/lib/schema-lite-typed.d.ts.map +1 -0
- package/lib/schema-lite-typed.js +1 -0
- package/lib/schema-lite.d.ts +41 -0
- package/lib/schema-lite.d.ts.map +1 -0
- package/lib/schema-lite.js +1 -0
- package/lib/schema.d.ts +21 -7
- package/lib/schema.d.ts.map +1 -1
- package/lib/schema.js +1 -48
- package/lib/sdk-error.d.ts +31 -0
- package/lib/sdk-error.d.ts.map +1 -0
- package/lib/sdk-error.js +1 -0
- package/lib/send.d.ts +37 -0
- package/lib/send.d.ts.map +1 -0
- package/lib/send.js +2 -0
- package/lib/send.js.map +1 -0
- package/lib/sensitive.d.ts +28 -0
- package/lib/sensitive.d.ts.map +1 -0
- package/lib/sensitive.js +2 -0
- package/lib/sensitive.js.map +1 -0
- package/lib/telemetry-debug.d.ts +6 -0
- package/lib/telemetry-debug.d.ts.map +1 -0
- package/lib/telemetry-debug.js +2 -0
- package/lib/telemetry-debug.js.map +1 -0
- package/lib/telemetry-hooks.d.ts +32 -0
- package/lib/telemetry-hooks.d.ts.map +1 -0
- package/lib/telemetry-hooks.js +2 -0
- package/lib/telemetry-hooks.js.map +1 -0
- package/lib/telemetry-host-rules.d.ts +23 -0
- package/lib/telemetry-host-rules.d.ts.map +1 -0
- package/lib/telemetry-host-rules.js +2 -0
- package/lib/telemetry-host-rules.js.map +1 -0
- package/lib/telemetry-lambda-context.d.ts +17 -0
- package/lib/telemetry-lambda-context.d.ts.map +1 -0
- package/lib/telemetry-lambda-context.js +2 -0
- package/lib/telemetry-lambda-context.js.map +1 -0
- package/lib/telemetry-message-context.d.ts +52 -0
- package/lib/telemetry-message-context.d.ts.map +1 -0
- package/lib/telemetry-message-context.js +2 -0
- package/lib/telemetry-message-context.js.map +1 -0
- package/lib/telemetry-metrics.d.ts +6 -0
- package/lib/telemetry-metrics.d.ts.map +1 -0
- package/lib/telemetry-metrics.js +2 -0
- package/lib/telemetry-metrics.js.map +1 -0
- package/lib/telemetry-name-template.d.ts +13 -0
- package/lib/telemetry-name-template.d.ts.map +1 -0
- package/lib/telemetry-name-template.js +2 -0
- package/lib/telemetry-name-template.js.map +1 -0
- package/lib/telemetry-stream.d.ts +11 -0
- package/lib/telemetry-stream.d.ts.map +1 -0
- package/lib/telemetry-stream.js +2 -0
- package/lib/telemetry-stream.js.map +1 -0
- package/lib/telemetry-xray.d.ts +28 -0
- package/lib/telemetry-xray.d.ts.map +1 -0
- package/lib/telemetry-xray.js +2 -0
- package/lib/telemetry-xray.js.map +1 -0
- package/lib/telemetry.d.ts +317 -0
- package/lib/telemetry.d.ts.map +1 -0
- package/lib/telemetry.js +3 -0
- package/lib/telemetry.js.map +1 -0
- package/lib/token-cache.d.ts +31 -0
- package/lib/token-cache.d.ts.map +1 -0
- package/lib/token-cache.js +2 -0
- package/lib/token-cache.js.map +1 -0
- package/lib/trait.d.ts +94 -20
- package/lib/trait.d.ts.map +1 -1
- package/lib/trait.js +1 -107
- package/lib/unknown-error.d.ts +18 -0
- package/lib/unknown-error.d.ts.map +1 -0
- package/lib/unknown-error.js +1 -0
- package/lib/xml.d.ts +44 -0
- package/lib/xml.d.ts.map +1 -0
- package/lib/xml.js +1 -0
- package/package.json +13 -13
- package/src/annotations.ts +53 -0
- package/src/api.ts +1597 -234
- package/src/category.ts +97 -134
- package/src/chunks.ts +30 -0
- package/src/config.ts +261 -0
- package/src/crypto.ts +202 -0
- package/src/error-category.ts +16 -29
- package/src/errors.ts +99 -77
- package/src/graphql.ts +1817 -0
- package/src/graphql.types.ts +120 -0
- package/src/grpc-transport-cloudflare-http2.ts +569 -0
- package/src/grpc-transport-cloudflare-sockets.ts +368 -0
- package/src/grpc-transport-node-http2.ts +307 -0
- package/src/grpc-transport.ts +287 -0
- package/src/hpack.ts +331 -0
- package/src/index.ts +31 -0
- package/src/max-message-bytes.ts +58 -0
- package/src/ndjson.ts +55 -0
- package/src/pagination.ts +379 -94
- package/src/plain-values.ts +70 -0
- package/src/protobuf.ts +1367 -0
- package/src/protocol-grpc.ts +811 -0
- package/src/protocol-http.ts +1045 -371
- package/src/protocol-rest.ts +908 -255
- package/src/protocol.ts +150 -0
- package/src/query.ts +39 -0
- package/src/response-validation-strict.ts +333 -0
- package/src/response-validation.ts +161 -0
- package/src/retry-after.ts +8 -14
- package/src/retry-state.ts +105 -0
- package/src/retry.ts +260 -145
- package/src/schema-lite-impl.ts +621 -0
- package/src/schema-lite-typed.ts +44 -0
- package/src/schema-lite.ts +45 -0
- package/src/schema.ts +259 -15
- package/src/sdk-error.ts +37 -0
- package/src/send.ts +156 -0
- package/src/sensitive.ts +188 -0
- package/src/telemetry-debug.ts +179 -0
- package/src/telemetry-hooks.ts +33 -0
- package/src/telemetry-host-rules.ts +39 -0
- package/src/telemetry-lambda-context.ts +46 -0
- package/src/telemetry-message-context.ts +170 -0
- package/src/telemetry-metrics.ts +69 -0
- package/src/telemetry-name-template.ts +26 -0
- package/src/telemetry-stream.ts +100 -0
- package/src/telemetry-xray.ts +66 -0
- package/src/telemetry.ts +800 -0
- package/src/token-cache.ts +117 -0
- package/src/trait.ts +139 -67
- package/src/unknown-error.ts +38 -0
- package/src/xml.ts +346 -0
- package/lib/api.js.map +0 -1
- package/lib/category.js.map +0 -1
- package/lib/codegen/cli.d.ts +0 -29
- package/lib/codegen/cli.d.ts.map +0 -1
- package/lib/codegen/cli.js +0 -165
- package/lib/codegen/cli.js.map +0 -1
- package/lib/codegen/emit.d.ts +0 -129
- package/lib/codegen/emit.d.ts.map +0 -1
- package/lib/codegen/emit.js +0 -105
- package/lib/codegen/emit.js.map +0 -1
- package/lib/codegen/format.d.ts +0 -23
- package/lib/codegen/format.d.ts.map +0 -1
- package/lib/codegen/format.js +0 -28
- package/lib/codegen/format.js.map +0 -1
- package/lib/codegen/generator.d.ts +0 -334
- package/lib/codegen/generator.d.ts.map +0 -1
- package/lib/codegen/generator.js +0 -691
- package/lib/codegen/generator.js.map +0 -1
- package/lib/codegen/graph.d.ts +0 -36
- package/lib/codegen/graph.d.ts.map +0 -1
- package/lib/codegen/graph.js +0 -136
- package/lib/codegen/graph.js.map +0 -1
- package/lib/codegen/members.d.ts +0 -25
- package/lib/codegen/members.d.ts.map +0 -1
- package/lib/codegen/members.js +0 -55
- package/lib/codegen/members.js.map +0 -1
- package/lib/codegen/naming.d.ts +0 -29
- package/lib/codegen/naming.d.ts.map +0 -1
- package/lib/codegen/naming.js +0 -74
- package/lib/codegen/naming.js.map +0 -1
- package/lib/codegen/openapi-cli.d.ts +0 -38
- package/lib/codegen/openapi-cli.d.ts.map +0 -1
- package/lib/codegen/openapi-cli.js +0 -107
- package/lib/codegen/openapi-cli.js.map +0 -1
- package/lib/codegen/openapi.d.ts +0 -115
- package/lib/codegen/openapi.d.ts.map +0 -1
- package/lib/codegen/openapi.js +0 -1220
- package/lib/codegen/openapi.js.map +0 -1
- package/lib/codegen/operations.d.ts +0 -24
- package/lib/codegen/operations.d.ts.map +0 -1
- package/lib/codegen/operations.js +0 -56
- package/lib/codegen/operations.js.map +0 -1
- package/lib/codegen/pagination.d.ts +0 -39
- package/lib/codegen/pagination.d.ts.map +0 -1
- package/lib/codegen/pagination.js +0 -33
- package/lib/codegen/pagination.js.map +0 -1
- package/lib/codegen/prelude.d.ts +0 -15
- package/lib/codegen/prelude.d.ts.map +0 -1
- package/lib/codegen/prelude.js +0 -60
- package/lib/codegen/prelude.js.map +0 -1
- package/lib/error-category.js.map +0 -1
- package/lib/errors.js.map +0 -1
- package/lib/json-patch.d.ts +0 -44
- package/lib/json-patch.d.ts.map +0 -1
- package/lib/json-patch.js +0 -174
- package/lib/json-patch.js.map +0 -1
- package/lib/pagination.js.map +0 -1
- package/lib/protocol-http.js.map +0 -1
- package/lib/protocol-rest.js.map +0 -1
- package/lib/retry-after.js.map +0 -1
- package/lib/retry.js.map +0 -1
- package/lib/schema.js.map +0 -1
- package/lib/trait.js.map +0 -1
- package/src/codegen/cli.ts +0 -268
- package/src/codegen/emit.ts +0 -207
- package/src/codegen/format.ts +0 -47
- package/src/codegen/generator.ts +0 -1153
- package/src/codegen/graph.ts +0 -151
- package/src/codegen/members.ts +0 -71
- package/src/codegen/naming.ts +0 -86
- package/src/codegen/openapi-cli.ts +0 -166
- package/src/codegen/openapi.ts +0 -1450
- package/src/codegen/operations.ts +0 -76
- package/src/codegen/pagination.ts +0 -71
- package/src/codegen/prelude.ts +0 -70
- package/src/json-patch.ts +0 -222
package/src/retry-after.ts
CHANGED
|
@@ -8,7 +8,7 @@
|
|
|
8
8
|
*
|
|
9
9
|
* Each SDK's `matchError` calls these helpers and attaches the resulting
|
|
10
10
|
* `Duration` to retryable errors via the optional `retryAfter` field, where
|
|
11
|
-
* the retry policy
|
|
11
|
+
* the retry policy waits at least that long (RFC 9110).
|
|
12
12
|
*
|
|
13
13
|
* Services that use bespoke headers or body fields are expected to do their
|
|
14
14
|
* own parsing in the SDK's `matchError`, optionally falling back to these
|
|
@@ -18,7 +18,7 @@ import * as Duration from "effect/Duration";
|
|
|
18
18
|
import { RETRYABLE_HTTP_STATUSES } from "./errors.ts";
|
|
19
19
|
|
|
20
20
|
/**
|
|
21
|
-
* Header bag
|
|
21
|
+
* Header bag -- case-insensitive lookup is the caller's responsibility.
|
|
22
22
|
*
|
|
23
23
|
* The helpers in this module read lowercase keys (`retry-after`, `ratelimit`)
|
|
24
24
|
* because most HTTP clients normalize headers to lowercase. If your client
|
|
@@ -34,9 +34,7 @@ export type Headers = Record<string, string | undefined> | undefined;
|
|
|
34
34
|
*
|
|
35
35
|
* Returns `undefined` when the header is missing or unparseable.
|
|
36
36
|
*/
|
|
37
|
-
export const parseRetryAfter = (
|
|
38
|
-
headers: Headers,
|
|
39
|
-
): Duration.Duration | undefined => {
|
|
37
|
+
export const parseRetryAfter = (headers: Headers): Duration.Duration | undefined => {
|
|
40
38
|
const raw = headers?.["retry-after"];
|
|
41
39
|
if (!raw) return undefined;
|
|
42
40
|
const trimmed = raw.trim();
|
|
@@ -74,12 +72,10 @@ export const parseRetryAfter = (
|
|
|
74
72
|
* `RateLimit: r=0, t=30` (compact form)
|
|
75
73
|
*
|
|
76
74
|
* Returns the `t` (seconds until reset) value as a Duration, but only when
|
|
77
|
-
* `r` (remaining quota) is present and equals `0`
|
|
75
|
+
* `r` (remaining quota) is present and equals `0` -- i.e., we're actually
|
|
78
76
|
* rate-limited right now. Returns `undefined` otherwise.
|
|
79
77
|
*/
|
|
80
|
-
export const parseRatelimit = (
|
|
81
|
-
headers: Headers,
|
|
82
|
-
): Duration.Duration | undefined => {
|
|
78
|
+
export const parseRatelimit = (headers: Headers): Duration.Duration | undefined => {
|
|
83
79
|
const raw = headers?.["ratelimit"];
|
|
84
80
|
if (!raw) return undefined;
|
|
85
81
|
|
|
@@ -99,7 +95,7 @@ export const parseRatelimit = (
|
|
|
99
95
|
else if (key === "t" || key === "reset") reset = num;
|
|
100
96
|
}
|
|
101
97
|
|
|
102
|
-
if (remaining !==
|
|
98
|
+
if (remaining !== 0) return undefined;
|
|
103
99
|
if (reset === undefined || reset < 0) return undefined;
|
|
104
100
|
return Duration.seconds(reset);
|
|
105
101
|
};
|
|
@@ -108,9 +104,7 @@ export const parseRatelimit = (
|
|
|
108
104
|
* Convenience: try `Retry-After` first, then `RateLimit`. Returns the first
|
|
109
105
|
* successful parse, or `undefined` if neither header yields a usable value.
|
|
110
106
|
*/
|
|
111
|
-
export const parseServerRetryHint = (
|
|
112
|
-
headers: Headers,
|
|
113
|
-
): Duration.Duration | undefined =>
|
|
107
|
+
export const parseServerRetryHint = (headers: Headers): Duration.Duration | undefined =>
|
|
114
108
|
parseRetryAfter(headers) ?? parseRatelimit(headers);
|
|
115
109
|
|
|
116
110
|
/**
|
|
@@ -118,7 +112,7 @@ export const parseServerRetryHint = (
|
|
|
118
112
|
* unless `status` is one whose error class actually declares a `retryAfter`
|
|
119
113
|
* field (see `RETRYABLE_HTTP_STATUSES`). Use this in `matchError` when you
|
|
120
114
|
* dispatch off a generic `HTTP_STATUS_MAP` lookup that includes both
|
|
121
|
-
* retryable (429/5xx/423) and non-retryable (4xx) classes
|
|
115
|
+
* retryable (429/5xx/423) and non-retryable (4xx) classes -- it prevents
|
|
122
116
|
* stale `retryAfter` properties from being attached to errors like
|
|
123
117
|
* `BadRequest` or `NotFound` (which would otherwise leak into JSON output).
|
|
124
118
|
*/
|
|
@@ -0,0 +1,105 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* What the retry layer learns from the wire, kept outside the error values:
|
|
3
|
+
*
|
|
4
|
+
* - the host of the last request each fiber sent, which keys the retry
|
|
5
|
+
* budget ({@link Budget}), and the bounded server hint its response
|
|
6
|
+
* carried (`x-amz-retry-after` / `x-distilled-retry-after`);
|
|
7
|
+
* - whether that request's body could be read only once, so a retry
|
|
8
|
+
* would send it empty;
|
|
9
|
+
* - a count of successful responses per host, which refills the budget.
|
|
10
|
+
*
|
|
11
|
+
* `send` (`./send.ts`) writes them for every protocol that sends through it.
|
|
12
|
+
* The module imports nothing heavy: every bundle that sends a request loads
|
|
13
|
+
* it.
|
|
14
|
+
*/
|
|
15
|
+
import * as Fiber from "effect/Fiber";
|
|
16
|
+
|
|
17
|
+
interface LastRequest {
|
|
18
|
+
readonly host: string;
|
|
19
|
+
/** The body can be read only once: the call must not be sent again. */
|
|
20
|
+
readonly oneShot: boolean;
|
|
21
|
+
/** Bounded server hint of the response, in milliseconds. */
|
|
22
|
+
hintMs: number | undefined;
|
|
23
|
+
}
|
|
24
|
+
|
|
25
|
+
const lastByFiber = /*@__PURE__*/ new WeakMap<object, LastRequest>();
|
|
26
|
+
const successesByHost = /*@__PURE__*/ new Map<string, number>();
|
|
27
|
+
|
|
28
|
+
/** The host of `url` (no port); `""` when it has none. */
|
|
29
|
+
export const hostOf = (url: string): string => {
|
|
30
|
+
const schemeEnd = url.indexOf("://");
|
|
31
|
+
if (schemeEnd === -1) return "";
|
|
32
|
+
const start = schemeEnd + 3;
|
|
33
|
+
let end = start;
|
|
34
|
+
while (end < url.length && !"/?#".includes(url[end]!)) end++;
|
|
35
|
+
const authority = url.slice(url.lastIndexOf("@", end) + 1 || start, end);
|
|
36
|
+
const colon = authority.lastIndexOf(":");
|
|
37
|
+
return colon !== -1 && !authority.endsWith("]") ? authority.slice(0, colon) : authority;
|
|
38
|
+
};
|
|
39
|
+
|
|
40
|
+
/** A whole, non-negative number of milliseconds; anything else is no hint. */
|
|
41
|
+
const parseMs = (value: string | undefined): number | undefined => {
|
|
42
|
+
if (value === undefined) return undefined;
|
|
43
|
+
const trimmed = value.trim();
|
|
44
|
+
if (!/^\d+$/.test(trimmed)) return undefined;
|
|
45
|
+
const ms = Number(trimmed);
|
|
46
|
+
return Number.isSafeInteger(ms) ? ms : undefined;
|
|
47
|
+
};
|
|
48
|
+
|
|
49
|
+
/**
|
|
50
|
+
* The bounded server hint of a response: `x-amz-retry-after` or
|
|
51
|
+
* `x-distilled-retry-after` in milliseconds, the larger when both are set.
|
|
52
|
+
*/
|
|
53
|
+
export const boundedHintOf = (
|
|
54
|
+
headers: Readonly<Record<string, string | undefined>>,
|
|
55
|
+
): number | undefined => {
|
|
56
|
+
const amz = parseMs(headers["x-amz-retry-after"]);
|
|
57
|
+
const distilled = parseMs(headers["x-distilled-retry-after"]);
|
|
58
|
+
if (amz === undefined) return distilled;
|
|
59
|
+
return distilled === undefined ? amz : Math.max(amz, distilled);
|
|
60
|
+
};
|
|
61
|
+
|
|
62
|
+
/**
|
|
63
|
+
* Record that the current fiber is sending a request to `url`; `oneShot`
|
|
64
|
+
* when its body can be read only once.
|
|
65
|
+
*/
|
|
66
|
+
export const recordRequest = (url: string, oneShot = false): void => {
|
|
67
|
+
const fiber = Fiber.getCurrent();
|
|
68
|
+
if (fiber !== undefined) {
|
|
69
|
+
lastByFiber.set(fiber, { host: hostOf(url), oneShot, hintMs: undefined });
|
|
70
|
+
}
|
|
71
|
+
};
|
|
72
|
+
|
|
73
|
+
/** Record the response to the current fiber's last request. */
|
|
74
|
+
export const recordResponse = (
|
|
75
|
+
status: number,
|
|
76
|
+
headers: Readonly<Record<string, string | undefined>>,
|
|
77
|
+
): void => {
|
|
78
|
+
const fiber = Fiber.getCurrent();
|
|
79
|
+
const last = fiber === undefined ? undefined : lastByFiber.get(fiber);
|
|
80
|
+
if (last === undefined) return;
|
|
81
|
+
if (status < 300) {
|
|
82
|
+
successesByHost.set(last.host, (successesByHost.get(last.host) ?? 0) + 1);
|
|
83
|
+
} else {
|
|
84
|
+
last.hintMs = boundedHintOf(headers);
|
|
85
|
+
}
|
|
86
|
+
};
|
|
87
|
+
|
|
88
|
+
/**
|
|
89
|
+
* The current fiber's last request, removed so that a later failure that
|
|
90
|
+
* sent nothing cannot reuse its hint.
|
|
91
|
+
*/
|
|
92
|
+
export const takeLastRequest = (): {
|
|
93
|
+
readonly host: string;
|
|
94
|
+
readonly oneShot: boolean;
|
|
95
|
+
readonly hintMs?: number;
|
|
96
|
+
} => {
|
|
97
|
+
const fiber = Fiber.getCurrent();
|
|
98
|
+
const last = fiber === undefined ? undefined : lastByFiber.get(fiber);
|
|
99
|
+
if (last === undefined) return { host: "", oneShot: false };
|
|
100
|
+
lastByFiber.delete(fiber!);
|
|
101
|
+
return last.hintMs === undefined ? { host: last.host, oneShot: last.oneShot } : last;
|
|
102
|
+
};
|
|
103
|
+
|
|
104
|
+
/** Successful responses from `host` so far in this process. */
|
|
105
|
+
export const successesOf = (host: string): number => successesByHost.get(host) ?? 0;
|
package/src/retry.ts
CHANGED
|
@@ -7,16 +7,15 @@
|
|
|
7
7
|
* so that callers can install a blanket retry policy at the layer level
|
|
8
8
|
* for that SDK without wrapping every call with `Effect.retry`.
|
|
9
9
|
*/
|
|
10
|
-
import * as Config from "effect/Config";
|
|
11
10
|
import * as Context from "effect/Context";
|
|
12
11
|
import * as Duration from "effect/Duration";
|
|
13
12
|
import * as Effect from "effect/Effect";
|
|
14
|
-
import { pipe } from "effect/Function";
|
|
15
13
|
import * as Layer from "effect/Layer";
|
|
16
14
|
import * as Option from "effect/Option";
|
|
17
15
|
import * as Ref from "effect/Ref";
|
|
18
16
|
import * as Schedule from "effect/Schedule";
|
|
19
17
|
import { isThrottling, isTransientError } from "./category.ts";
|
|
18
|
+
import { successesOf } from "./retry-state.ts";
|
|
20
19
|
|
|
21
20
|
// ============================================================================
|
|
22
21
|
// Retry Policy Types
|
|
@@ -25,6 +24,140 @@ import { isThrottling, isTransientError } from "./category.ts";
|
|
|
25
24
|
/**
|
|
26
25
|
* Retry policy options that match the Effect.retry contract.
|
|
27
26
|
*/
|
|
27
|
+
/**
|
|
28
|
+
* Capped exponential backoff, as plain data: the built-in policies use this
|
|
29
|
+
* instead of an Effect `Schedule`, so bundles do not carry the Schedule
|
|
30
|
+
* machinery unless a caller supplies a schedule of their own.
|
|
31
|
+
*
|
|
32
|
+
* Delay before retry `n` (0-based), `t`:
|
|
33
|
+
*
|
|
34
|
+
* - with `fullJitter`: `random(0, 1) × min(capMs, base × factor^n)`, where
|
|
35
|
+
* `base` is `throttlingBaseMs` for throttling errors and `baseMs` otherwise;
|
|
36
|
+
* - without it: `min(baseMs × factor^n, capMs)`, at least `minThrottleMs` for
|
|
37
|
+
* throttling errors, plus up to `jitterMs` of random jitter.
|
|
38
|
+
*
|
|
39
|
+
* With `serverHint`, the server can lengthen `t`:
|
|
40
|
+
*
|
|
41
|
+
* - a bounded hint (`x-amz-retry-after` or `x-distilled-retry-after`, in
|
|
42
|
+
* milliseconds) is clamped to `[t, t + 5s]` and used as is;
|
|
43
|
+
* - otherwise `error.retryAfter` (`Retry-After` / `RateLimit`) is a minimum,
|
|
44
|
+
* as RFC 9110 defines it; a hint over the cap ({@link ServerRetryHintCapMs})
|
|
45
|
+
* stops retrying, because retrying earlier than the server asked fails again.
|
|
46
|
+
*
|
|
47
|
+
* With a `budget`, each retry withdraws from it and stops when it is empty.
|
|
48
|
+
*/
|
|
49
|
+
export interface Backoff {
|
|
50
|
+
readonly baseMs: number;
|
|
51
|
+
readonly factor: number;
|
|
52
|
+
readonly capMs: number;
|
|
53
|
+
/** Stop after this many retries; unlimited when absent. */
|
|
54
|
+
readonly maxRetries?: number;
|
|
55
|
+
readonly jitterMs?: number;
|
|
56
|
+
readonly minThrottleMs?: number;
|
|
57
|
+
readonly serverHint?: boolean;
|
|
58
|
+
/** Base delay for throttling errors under `fullJitter`; `baseMs` when absent. */
|
|
59
|
+
readonly throttlingBaseMs?: number;
|
|
60
|
+
readonly fullJitter?: boolean;
|
|
61
|
+
readonly budget?: Budget;
|
|
62
|
+
/**
|
|
63
|
+
* When the budget cannot pay for a retry, wait the delay before failing
|
|
64
|
+
* anyway, so a polling loop around the call cannot spin (AWS Retry
|
|
65
|
+
* Behavior 2.1 for long-polling operations such as SQS `ReceiveMessage`).
|
|
66
|
+
*/
|
|
67
|
+
readonly longPoll?: boolean;
|
|
68
|
+
}
|
|
69
|
+
|
|
70
|
+
/** How far a bounded server hint may lengthen the computed delay. */
|
|
71
|
+
export const BOUNDED_HINT_MAX_EXTRA_MS = 5_000;
|
|
72
|
+
|
|
73
|
+
// ============================================================================
|
|
74
|
+
// Retry Budget
|
|
75
|
+
// ============================================================================
|
|
76
|
+
|
|
77
|
+
/**
|
|
78
|
+
* A retry budget: one token bucket per key (the request's host). A retry
|
|
79
|
+
* withdraws its cost; a successful response from that host adds one token,
|
|
80
|
+
* up to `max`; a retry that succeeds gets its cost back. When a bucket
|
|
81
|
+
* cannot pay, the call fails instead of retrying, so a failing dependency
|
|
82
|
+
* sees about `1 / cost` extra load instead of `maxRetries` times its load.
|
|
83
|
+
*
|
|
84
|
+
* The bucket lives as long as the budget value. A policy defined at module
|
|
85
|
+
* scope shares one budget across calls (and across Lambda invocations); a
|
|
86
|
+
* policy built per call starts a fresh one each time.
|
|
87
|
+
*/
|
|
88
|
+
export interface Budget {
|
|
89
|
+
/** Withdraw a retry's cost from `key`'s bucket; the cost, or `undefined` when it cannot pay. */
|
|
90
|
+
readonly withdraw: (key: string, throttling: boolean) => number | undefined;
|
|
91
|
+
/** Give back what a retry that succeeded withdrew. */
|
|
92
|
+
readonly refund: (key: string, cost: number) => void;
|
|
93
|
+
/** Tokens left in `key`'s bucket. */
|
|
94
|
+
readonly tokens: (key: string) => number;
|
|
95
|
+
/** Return every bucket to its initial tokens. */
|
|
96
|
+
readonly reset: () => void;
|
|
97
|
+
}
|
|
98
|
+
|
|
99
|
+
export interface BudgetOptions {
|
|
100
|
+
/** Tokens a new bucket starts with. Default 100: 7 transient or 20 throttling retries. */
|
|
101
|
+
readonly initial?: number;
|
|
102
|
+
/** Most tokens a bucket holds. Default 500. */
|
|
103
|
+
readonly max?: number;
|
|
104
|
+
/** Cost of retrying a non-throttling error. Default 14. */
|
|
105
|
+
readonly transientCost?: number;
|
|
106
|
+
/** Cost of retrying a throttling error. Default 5. */
|
|
107
|
+
readonly throttlingCost?: number;
|
|
108
|
+
}
|
|
109
|
+
|
|
110
|
+
/**
|
|
111
|
+
* The AWS SDKs' retry quota (Retry Behavior 2.1: 500 tokens, a transient
|
|
112
|
+
* retry costs 14, a throttling retry 5, a success adds 1), except that a
|
|
113
|
+
* bucket starts with 100 tokens instead of full: many short-lived clients,
|
|
114
|
+
* each starting full, would together retry a failing service at nearly
|
|
115
|
+
* `maxRetries` times its load.
|
|
116
|
+
*/
|
|
117
|
+
export const makeBudget = (options: BudgetOptions = {}): Budget => {
|
|
118
|
+
const initial = options.initial ?? 100;
|
|
119
|
+
const max = options.max ?? 500;
|
|
120
|
+
const transientCost = options.transientCost ?? 14;
|
|
121
|
+
const throttlingCost = options.throttlingCost ?? 5;
|
|
122
|
+
const buckets = new Map<string, { tokens: number; seen: number }>();
|
|
123
|
+
const bucket = (key: string) => {
|
|
124
|
+
const seen = successesOf(key);
|
|
125
|
+
let b = buckets.get(key);
|
|
126
|
+
if (b === undefined) {
|
|
127
|
+
b = { tokens: initial, seen };
|
|
128
|
+
buckets.set(key, b);
|
|
129
|
+
} else if (seen !== b.seen) {
|
|
130
|
+
// Successes since the last look, one token each.
|
|
131
|
+
b.tokens = Math.min(max, b.tokens + (seen - b.seen));
|
|
132
|
+
b.seen = seen;
|
|
133
|
+
}
|
|
134
|
+
return b;
|
|
135
|
+
};
|
|
136
|
+
return {
|
|
137
|
+
withdraw: (key, throttling) => {
|
|
138
|
+
const b = bucket(key);
|
|
139
|
+
const cost = throttling ? throttlingCost : transientCost;
|
|
140
|
+
if (b.tokens < cost) return undefined;
|
|
141
|
+
b.tokens -= cost;
|
|
142
|
+
return cost;
|
|
143
|
+
},
|
|
144
|
+
refund: (key, cost) => {
|
|
145
|
+
const b = bucket(key);
|
|
146
|
+
// The success itself already added a token.
|
|
147
|
+
b.tokens = Math.min(max, b.tokens + cost - 1);
|
|
148
|
+
},
|
|
149
|
+
tokens: (key) => bucket(key).tokens,
|
|
150
|
+
reset: () => buckets.clear(),
|
|
151
|
+
};
|
|
152
|
+
};
|
|
153
|
+
|
|
154
|
+
/**
|
|
155
|
+
* The budget of the default policy, shared by every SDK in this process.
|
|
156
|
+
* Its buckets are keyed by host, so one failing service does not use up the
|
|
157
|
+
* retries of another.
|
|
158
|
+
*/
|
|
159
|
+
export const defaultBudget: Budget = /*@__PURE__*/ makeBudget();
|
|
160
|
+
|
|
28
161
|
export interface Options {
|
|
29
162
|
/**
|
|
30
163
|
* Predicate to determine if an error should trigger a retry.
|
|
@@ -34,6 +167,8 @@ export interface Options {
|
|
|
34
167
|
* The schedule to use for retrying.
|
|
35
168
|
*/
|
|
36
169
|
readonly schedule?: Schedule.Schedule<unknown>;
|
|
170
|
+
/** Used when no `schedule` is given. */
|
|
171
|
+
readonly backoff?: Backoff;
|
|
37
172
|
}
|
|
38
173
|
|
|
39
174
|
/**
|
|
@@ -63,8 +198,7 @@ export type Policy = Options | Factory;
|
|
|
63
198
|
* export class Retry extends makeRetryService("PlanetScaleRetry") {}
|
|
64
199
|
* ```
|
|
65
200
|
*/
|
|
66
|
-
export const makeRetryService = (name: string) =>
|
|
67
|
-
Context.Service<any, Policy>()(name);
|
|
201
|
+
export const makeRetryService = (name: string) => Context.Service<any, Policy>()(name);
|
|
68
202
|
|
|
69
203
|
// ============================================================================
|
|
70
204
|
// Retry Schedule Utilities
|
|
@@ -74,7 +208,7 @@ export const makeRetryService = (name: string) =>
|
|
|
74
208
|
* Custom jittered schedule helper.
|
|
75
209
|
* Adds random jitter between 0-50ms to avoid thundering herd.
|
|
76
210
|
*/
|
|
77
|
-
export const jittered = Schedule.addDelay(() =>
|
|
211
|
+
export const jittered = /*@__PURE__*/ Schedule.addDelay(() =>
|
|
78
212
|
Effect.succeed(Duration.millis(Math.random() * 50)),
|
|
79
213
|
);
|
|
80
214
|
|
|
@@ -111,7 +245,7 @@ const ENV_SERVER_RETRY_HINT_CAP_MS = "DISTILLED_SERVER_RETRY_HINT_CAP_MS";
|
|
|
111
245
|
* )
|
|
112
246
|
* ```
|
|
113
247
|
*/
|
|
114
|
-
export const ServerRetryHintCapMs = Context.Service<number>(
|
|
248
|
+
export const ServerRetryHintCapMs = /*@__PURE__*/ Context.Service<number>(
|
|
115
249
|
"@distilled.cloud/core/ServerRetryHintCapMs",
|
|
116
250
|
);
|
|
117
251
|
|
|
@@ -121,43 +255,58 @@ export const ServerRetryHintCapMs = Context.Service<number>(
|
|
|
121
255
|
export const serverRetryHintCapLayer = (capMs: number) =>
|
|
122
256
|
Layer.succeed(ServerRetryHintCapMs, capMs);
|
|
123
257
|
|
|
124
|
-
const serverRetryHintCapMsConfig: Config.Config<number> = Config.string(
|
|
125
|
-
ENV_SERVER_RETRY_HINT_CAP_MS,
|
|
126
|
-
).pipe(
|
|
127
|
-
Config.map((raw) => {
|
|
128
|
-
if (raw === "") return DEFAULT_SERVER_RETRY_HINT_CAP_MS;
|
|
129
|
-
const n = Number(raw);
|
|
130
|
-
if (!Number.isFinite(n) || n < 0) return DEFAULT_SERVER_RETRY_HINT_CAP_MS;
|
|
131
|
-
return Math.trunc(n);
|
|
132
|
-
}),
|
|
133
|
-
Config.withDefault(DEFAULT_SERVER_RETRY_HINT_CAP_MS),
|
|
134
|
-
);
|
|
135
|
-
|
|
136
258
|
/**
|
|
137
259
|
* Effective cap when neither {@link ServerRetryHintCapMs} nor
|
|
138
260
|
* `DISTILLED_SERVER_RETRY_HINT_CAP_MS` is set: {@link DEFAULT_SERVER_RETRY_HINT_CAP_MS}.
|
|
139
261
|
*
|
|
140
|
-
* Reads the `DISTILLED_SERVER_RETRY_HINT_CAP_MS` environment variable
|
|
141
|
-
*
|
|
142
|
-
*
|
|
262
|
+
* Reads the `DISTILLED_SERVER_RETRY_HINT_CAP_MS` environment variable (must
|
|
263
|
+
* be a non-negative finite number; invalid values fall back to the default).
|
|
264
|
+
* It reads `process.env` directly: Effect's `Config` would add about 5 KB
|
|
265
|
+
* gzip to every bundle for this one optional knob. Use
|
|
266
|
+
* {@link ServerRetryHintCapMs} to set the cap from code.
|
|
143
267
|
*/
|
|
144
268
|
export const readServerRetryHintCapMsFromEnv = (): number =>
|
|
145
|
-
|
|
146
|
-
|
|
147
|
-
|
|
148
|
-
|
|
269
|
+
parseCapMs(
|
|
270
|
+
(globalThis as { process?: { env?: Record<string, string | undefined> } }).process?.env?.[
|
|
271
|
+
ENV_SERVER_RETRY_HINT_CAP_MS
|
|
272
|
+
],
|
|
149
273
|
);
|
|
150
274
|
|
|
151
|
-
const
|
|
275
|
+
const parseCapMs = (raw: string | undefined): number => {
|
|
276
|
+
if (raw === undefined || raw === "") return DEFAULT_SERVER_RETRY_HINT_CAP_MS;
|
|
277
|
+
const n = Number(raw);
|
|
278
|
+
return Number.isFinite(n) && n >= 0 ? Math.trunc(n) : DEFAULT_SERVER_RETRY_HINT_CAP_MS;
|
|
279
|
+
};
|
|
280
|
+
|
|
281
|
+
/**
|
|
282
|
+
* The `ConfigProvider` the caller provided, looked up by its context key.
|
|
283
|
+
* Importing `effect/Config` / `effect/ConfigProvider` to read one optional
|
|
284
|
+
* env var would add about 5 KB gzip to every bundle; a provider object
|
|
285
|
+
* carries its own `load`, so neither module is needed.
|
|
286
|
+
*/
|
|
287
|
+
const CONFIG_PROVIDER_KEY = "effect/ConfigProvider";
|
|
288
|
+
interface ProvidedConfigProvider {
|
|
289
|
+
readonly load: (
|
|
290
|
+
path: ReadonlyArray<string>,
|
|
291
|
+
) => Effect.Effect<{ readonly _tag: string; readonly value?: string } | undefined, unknown>;
|
|
292
|
+
}
|
|
293
|
+
|
|
294
|
+
export const resolveServerRetryHintCapMs = (): Effect.Effect<number, never, never> =>
|
|
152
295
|
Effect.gen(function* () {
|
|
153
296
|
const fromLayer = yield* Effect.serviceOption(ServerRetryHintCapMs);
|
|
154
297
|
if (Option.isSome(fromLayer)) {
|
|
155
298
|
const n = fromLayer.value;
|
|
156
299
|
if (Number.isFinite(n) && n >= 0) return Math.trunc(n);
|
|
157
300
|
}
|
|
158
|
-
|
|
159
|
-
|
|
160
|
-
|
|
301
|
+
const provider = (yield* Effect.context<never>()).mapUnsafe.get(CONFIG_PROVIDER_KEY) as
|
|
302
|
+
| ProvidedConfigProvider
|
|
303
|
+
| undefined;
|
|
304
|
+
if (!provider) return readServerRetryHintCapMsFromEnv();
|
|
305
|
+
// An unset or unreadable cap is the default cap.
|
|
306
|
+
const node = yield* provider
|
|
307
|
+
.load([ENV_SERVER_RETRY_HINT_CAP_MS])
|
|
308
|
+
.pipe(Effect.orElseSucceed(() => undefined));
|
|
309
|
+
return parseCapMs(node?._tag === "Value" ? node.value : undefined);
|
|
161
310
|
});
|
|
162
311
|
|
|
163
312
|
/**
|
|
@@ -167,142 +316,108 @@ const resolveServerRetryHintCapMs = (): Effect.Effect<number, never, never> =>
|
|
|
167
316
|
*
|
|
168
317
|
* Returns `undefined` when the error doesn't carry a hint.
|
|
169
318
|
*/
|
|
170
|
-
const serverHintMillis = (
|
|
171
|
-
error
|
|
172
|
-
capMs: number,
|
|
173
|
-
): number | undefined => {
|
|
174
|
-
const hint = (error as { retryAfter?: unknown } | null | undefined)
|
|
175
|
-
?.retryAfter;
|
|
319
|
+
export const serverHintMillis = (error: unknown, capMs: number): number | undefined => {
|
|
320
|
+
const hint = (error as { retryAfter?: unknown } | null | undefined)?.retryAfter;
|
|
176
321
|
if (!Duration.isDuration(hint)) return undefined;
|
|
177
322
|
return Math.min(Duration.toMillis(hint), capMs);
|
|
178
323
|
};
|
|
179
324
|
|
|
180
|
-
/**
|
|
181
|
-
* Schedule modifier that honors `error.retryAfter` (set by the SDK's
|
|
182
|
-
* `matchError` from `Retry-After` / `RateLimit` headers) with precedence
|
|
183
|
-
* over the input delay. Falls back to the original delay when no hint is
|
|
184
|
-
* present.
|
|
185
|
-
*/
|
|
186
|
-
const honorServerHint = (
|
|
187
|
-
lastError: Ref.Ref<unknown>,
|
|
188
|
-
baseline?: (duration: Duration.Duration, error: unknown) => Duration.Duration,
|
|
189
|
-
) =>
|
|
190
|
-
Schedule.modifyDelay(
|
|
191
|
-
Effect.fnUntraced(function* ({ duration }) {
|
|
192
|
-
const capMs = yield* resolveServerRetryHintCapMs();
|
|
193
|
-
const error = yield* Ref.get(lastError);
|
|
194
|
-
const hint = serverHintMillis(error, capMs);
|
|
195
|
-
if (hint !== undefined) return hint;
|
|
196
|
-
const adjusted = baseline ? baseline(duration, error) : duration;
|
|
197
|
-
return Duration.toMillis(adjusted);
|
|
198
|
-
}),
|
|
199
|
-
);
|
|
200
|
-
|
|
201
325
|
// ============================================================================
|
|
202
326
|
// Default Retry Policies
|
|
203
327
|
// ============================================================================
|
|
204
328
|
|
|
205
329
|
/**
|
|
206
|
-
*
|
|
330
|
+
* The policy used when the context provides none, tuned for calls at
|
|
331
|
+
* runtime (a request handler, a Lambda) after AWS's Retry Behavior 2.1:
|
|
207
332
|
*
|
|
208
|
-
*
|
|
209
|
-
* -
|
|
210
|
-
* -
|
|
211
|
-
*
|
|
212
|
-
*
|
|
213
|
-
* -
|
|
214
|
-
* capped at 5s per delay
|
|
215
|
-
* - Ensures at least 500ms delay for throttling errors
|
|
216
|
-
* - Limits to 8 retry attempts
|
|
217
|
-
* - Applies jitter to avoid thundering herd
|
|
333
|
+
* - retries only errors marked retryable (see `isTransientError`);
|
|
334
|
+
* - at most 2 retries (3 attempts);
|
|
335
|
+
* - full jitter: `random(0, 1) × min(20s, base × 2^n)`, base 50ms, or 1s
|
|
336
|
+
* for throttling errors;
|
|
337
|
+
* - honors server hints (see {@link Backoff});
|
|
338
|
+
* - draws from {@link defaultBudget}.
|
|
218
339
|
*
|
|
219
|
-
*
|
|
220
|
-
*
|
|
221
|
-
* high request concurrency routinely outlast a short 5-attempt/3s policy
|
|
222
|
-
* while a genuinely broken credential still fails within seconds.
|
|
340
|
+
* Deploy tools that should wait out longer outages install their own
|
|
341
|
+
* policy, for example {@link transientFactory}.
|
|
223
342
|
*/
|
|
224
|
-
export const
|
|
343
|
+
export const defaultOptions: Options = {
|
|
225
344
|
while: (error) => isTransientError(error),
|
|
226
|
-
|
|
227
|
-
|
|
228
|
-
|
|
229
|
-
|
|
230
|
-
|
|
231
|
-
|
|
232
|
-
|
|
233
|
-
|
|
234
|
-
|
|
235
|
-
|
|
236
|
-
|
|
237
|
-
|
|
238
|
-
|
|
239
|
-
}),
|
|
240
|
-
jittered,
|
|
241
|
-
),
|
|
242
|
-
Schedule.recurs(8),
|
|
243
|
-
]),
|
|
244
|
-
});
|
|
345
|
+
backoff: {
|
|
346
|
+
baseMs: 50,
|
|
347
|
+
throttlingBaseMs: 1000,
|
|
348
|
+
factor: 2,
|
|
349
|
+
capMs: 20_000,
|
|
350
|
+
maxRetries: 2,
|
|
351
|
+
fullJitter: true,
|
|
352
|
+
serverHint: true,
|
|
353
|
+
budget: defaultBudget,
|
|
354
|
+
},
|
|
355
|
+
};
|
|
356
|
+
|
|
357
|
+
export const makeDefault: Factory = () => defaultOptions;
|
|
245
358
|
|
|
246
359
|
/**
|
|
247
|
-
*
|
|
248
|
-
*
|
|
360
|
+
* A model's `distilled.retry#policy` trait: numbers that replace the default
|
|
361
|
+
* policy's for a service or an operation. What retries still comes from the
|
|
362
|
+
* error's marks.
|
|
249
363
|
*/
|
|
250
|
-
export
|
|
364
|
+
export interface RetryOverride {
|
|
365
|
+
/** Attempts in total, the first included. */
|
|
366
|
+
readonly maxAttempts?: number;
|
|
367
|
+
readonly baseMs?: number;
|
|
368
|
+
readonly throttlingBaseMs?: number;
|
|
369
|
+
readonly capMs?: number;
|
|
370
|
+
/** Multiplier between delays (2 by default). */
|
|
371
|
+
readonly factor?: number;
|
|
372
|
+
readonly longPoll?: boolean;
|
|
373
|
+
}
|
|
374
|
+
|
|
375
|
+
const overridden = new WeakMap<RetryOverride, Options>();
|
|
376
|
+
|
|
377
|
+
/** {@link defaultOptions} with `override`'s numbers, memoized per override object. */
|
|
378
|
+
export const defaultWith = (override: RetryOverride): Options => {
|
|
379
|
+
const cached = overridden.get(override);
|
|
380
|
+
if (cached !== undefined) return cached;
|
|
381
|
+
const backoff = defaultOptions.backoff!;
|
|
382
|
+
const result: Options = {
|
|
383
|
+
...defaultOptions,
|
|
384
|
+
backoff: {
|
|
385
|
+
...backoff,
|
|
386
|
+
...(override.maxAttempts !== undefined ? { maxRetries: override.maxAttempts - 1 } : {}),
|
|
387
|
+
...(override.baseMs !== undefined ? { baseMs: override.baseMs } : {}),
|
|
388
|
+
...(override.throttlingBaseMs !== undefined
|
|
389
|
+
? { throttlingBaseMs: override.throttlingBaseMs }
|
|
390
|
+
: {}),
|
|
391
|
+
...(override.capMs !== undefined ? { capMs: override.capMs } : {}),
|
|
392
|
+
...(override.factor !== undefined ? { factor: override.factor } : {}),
|
|
393
|
+
...(override.longPoll !== undefined ? { longPoll: override.longPoll } : {}),
|
|
394
|
+
},
|
|
395
|
+
};
|
|
396
|
+
overridden.set(override, result);
|
|
397
|
+
return result;
|
|
398
|
+
};
|
|
399
|
+
|
|
400
|
+
const slowBackoff: Backoff = {
|
|
401
|
+
baseMs: 1000,
|
|
402
|
+
factor: 2,
|
|
403
|
+
capMs: 5000,
|
|
404
|
+
jitterMs: 50,
|
|
405
|
+
serverHint: true,
|
|
406
|
+
};
|
|
407
|
+
const slowBackoffNoHint: Backoff = { baseMs: 1000, factor: 2, capMs: 5000, jitterMs: 50 };
|
|
408
|
+
|
|
409
|
+
export const throttlingFactory: Factory = () => throttlingFactoryOptions;
|
|
410
|
+
const throttlingFactoryOptions: Options = {
|
|
251
411
|
while: (error) => isThrottling(error),
|
|
252
|
-
|
|
253
|
-
|
|
254
|
-
capped(Duration.seconds(5)),
|
|
255
|
-
honorServerHint(lastError),
|
|
256
|
-
jittered,
|
|
257
|
-
),
|
|
258
|
-
});
|
|
412
|
+
backoff: slowBackoff,
|
|
413
|
+
};
|
|
259
414
|
|
|
260
|
-
/**
|
|
261
|
-
* Retry options that retries all throttling errors indefinitely.
|
|
262
|
-
*
|
|
263
|
-
* Note: prefer {@link throttlingFactory} when you want server-hint honoring.
|
|
264
|
-
* This static `Options` form does not have access to `lastError` and so
|
|
265
|
-
* cannot read `error.retryAfter`.
|
|
266
|
-
*/
|
|
267
415
|
export const throttlingOptions: Options = {
|
|
268
416
|
while: (error) => isThrottling(error),
|
|
269
|
-
|
|
270
|
-
Schedule.exponential(1000, 2),
|
|
271
|
-
capped(Duration.seconds(5)),
|
|
272
|
-
jittered,
|
|
273
|
-
),
|
|
417
|
+
backoff: slowBackoffNoHint,
|
|
274
418
|
};
|
|
275
419
|
|
|
276
|
-
|
|
277
|
-
|
|
278
|
-
* so callers using `<SDK>.Retry.transient` get correct backoff.
|
|
279
|
-
*/
|
|
280
|
-
export const transientFactory: Factory = (lastError) => ({
|
|
281
|
-
while: isTransientError,
|
|
282
|
-
schedule: pipe(
|
|
283
|
-
Schedule.exponential(1000, 2),
|
|
284
|
-
capped(Duration.seconds(5)),
|
|
285
|
-
honorServerHint(lastError),
|
|
286
|
-
jittered,
|
|
287
|
-
),
|
|
288
|
-
});
|
|
420
|
+
export const transientFactory: Factory = () => transientFactoryOptions;
|
|
421
|
+
const transientFactoryOptions: Options = { while: isTransientError, backoff: slowBackoff };
|
|
289
422
|
|
|
290
|
-
|
|
291
|
-
* Retry options that retries all transient errors indefinitely.
|
|
292
|
-
*
|
|
293
|
-
* This includes:
|
|
294
|
-
* 1. Throttling errors
|
|
295
|
-
* 2. Server errors
|
|
296
|
-
* 3. Network errors
|
|
297
|
-
* 4. Locked errors (423)
|
|
298
|
-
*
|
|
299
|
-
* Note: prefer {@link transientFactory} when you want server-hint honoring.
|
|
300
|
-
*/
|
|
301
|
-
export const transientOptions: Options = {
|
|
302
|
-
while: isTransientError,
|
|
303
|
-
schedule: pipe(
|
|
304
|
-
Schedule.exponential(1000, 2),
|
|
305
|
-
capped(Duration.seconds(5)),
|
|
306
|
-
jittered,
|
|
307
|
-
),
|
|
308
|
-
};
|
|
423
|
+
export const transientOptions: Options = { while: isTransientError, backoff: slowBackoffNoHint };
|