@metamask-previews/controller-utils 11.4.5-preview-4c4eb52a → 11.5.0-preview-0d0d258d
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/CHANGELOG.md +13 -2
- package/dist/create-service-policy.cjs +38 -42
- package/dist/create-service-policy.cjs.map +1 -1
- package/dist/create-service-policy.d.cts +83 -19
- package/dist/create-service-policy.d.cts.map +1 -1
- package/dist/create-service-policy.d.mts +83 -19
- package/dist/create-service-policy.d.mts.map +1 -1
- package/dist/create-service-policy.mjs +35 -42
- package/dist/create-service-policy.mjs.map +1 -1
- package/dist/index.cjs +9 -1
- package/dist/index.cjs.map +1 -1
- package/dist/index.d.cts +2 -1
- package/dist/index.d.cts.map +1 -1
- package/dist/index.d.mts +2 -1
- package/dist/index.d.mts.map +1 -1
- package/dist/index.mjs +1 -1
- package/dist/index.mjs.map +1 -1
- package/package.json +2 -2
package/CHANGELOG.md
CHANGED
@@ -7,9 +7,19 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
|
|
7
7
|
|
8
8
|
## [Unreleased]
|
9
9
|
|
10
|
+
## [11.5.0]
|
11
|
+
|
10
12
|
### Added
|
11
13
|
|
12
|
-
- Add `createServicePolicy`
|
14
|
+
- Add utility function `createServicePolicy` for reducing boilerplate for service classes ([#5141](https://github.com/MetaMask/core/pull/5141), [#5154](https://github.com/MetaMask/core/pull/5154), [#5143](https://github.com/MetaMask/core/pull/5143), [#5149](https://github.com/MetaMask/core/pull/5149), [#5188](https://github.com/MetaMask/core/pull/5188), [#5192](https://github.com/MetaMask/core/pull/5192), [#5225](https://github.com/MetaMask/core/pull/5225))
|
15
|
+
- Export constants `DEFAULT_CIRCUIT_BREAK_DURATION`, `DEFAULT_DEGRADED_THRESHOLD`, `DEFAULT_MAX_CONSECUTIVE_FAILURES`, and `DEFAULT_MAX_RETRIES`
|
16
|
+
- Export types `ServicePolicy` and `CreateServicePolicyOptions`
|
17
|
+
- Re-export `BrokenCircuitError`, `CircuitState`, `handleAll`, and `handleWhen` from `cockatiel`
|
18
|
+
- Export `CockatielEvent` type, an alias of the `Event` type from `cockatiel`
|
19
|
+
|
20
|
+
### Changed
|
21
|
+
|
22
|
+
- Bump `@metamask/utils` from `^11.0.1` to `^11.1.0` ([#5223](https://github.com/MetaMask/core/pull/5223))
|
13
23
|
|
14
24
|
## [11.4.5]
|
15
25
|
|
@@ -448,7 +458,8 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
|
|
448
458
|
|
449
459
|
All changes listed after this point were applied to this package following the monorepo conversion.
|
450
460
|
|
451
|
-
[Unreleased]: https://github.com/MetaMask/core/compare/@metamask/controller-utils@11.
|
461
|
+
[Unreleased]: https://github.com/MetaMask/core/compare/@metamask/controller-utils@11.5.0...HEAD
|
462
|
+
[11.5.0]: https://github.com/MetaMask/core/compare/@metamask/controller-utils@11.4.5...@metamask/controller-utils@11.5.0
|
452
463
|
[11.4.5]: https://github.com/MetaMask/core/compare/@metamask/controller-utils@11.4.4...@metamask/controller-utils@11.4.5
|
453
464
|
[11.4.4]: https://github.com/MetaMask/core/compare/@metamask/controller-utils@11.4.3...@metamask/controller-utils@11.4.4
|
454
465
|
[11.4.3]: https://github.com/MetaMask/core/compare/@metamask/controller-utils@11.4.2...@metamask/controller-utils@11.4.3
|
@@ -1,7 +1,11 @@
|
|
1
1
|
"use strict";
|
2
2
|
Object.defineProperty(exports, "__esModule", { value: true });
|
3
|
-
exports.createServicePolicy = exports.DEFAULT_DEGRADED_THRESHOLD = exports.DEFAULT_CIRCUIT_BREAK_DURATION = exports.DEFAULT_MAX_CONSECUTIVE_FAILURES = exports.DEFAULT_MAX_RETRIES = void 0;
|
3
|
+
exports.createServicePolicy = exports.DEFAULT_DEGRADED_THRESHOLD = exports.DEFAULT_CIRCUIT_BREAK_DURATION = exports.DEFAULT_MAX_CONSECUTIVE_FAILURES = exports.DEFAULT_MAX_RETRIES = exports.handleWhen = exports.handleAll = exports.BrokenCircuitError = exports.CircuitState = void 0;
|
4
4
|
const cockatiel_1 = require("cockatiel");
|
5
|
+
Object.defineProperty(exports, "BrokenCircuitError", { enumerable: true, get: function () { return cockatiel_1.BrokenCircuitError; } });
|
6
|
+
Object.defineProperty(exports, "CircuitState", { enumerable: true, get: function () { return cockatiel_1.CircuitState; } });
|
7
|
+
Object.defineProperty(exports, "handleAll", { enumerable: true, get: function () { return cockatiel_1.handleAll; } });
|
8
|
+
Object.defineProperty(exports, "handleWhen", { enumerable: true, get: function () { return cockatiel_1.handleWhen; } });
|
5
9
|
/**
|
6
10
|
* The maximum number of times that a failing service should be re-run before
|
7
11
|
* giving up.
|
@@ -9,7 +13,9 @@ const cockatiel_1 = require("cockatiel");
|
|
9
13
|
exports.DEFAULT_MAX_RETRIES = 3;
|
10
14
|
/**
|
11
15
|
* The maximum number of times that the service is allowed to fail before
|
12
|
-
* pausing further retries.
|
16
|
+
* pausing further retries. This is set to a value such that if given a
|
17
|
+
* service that continually fails, the policy needs to be executed 3 times
|
18
|
+
* before further retries are paused.
|
13
19
|
*/
|
14
20
|
exports.DEFAULT_MAX_CONSECUTIVE_FAILURES = (1 + exports.DEFAULT_MAX_RETRIES) * 3;
|
15
21
|
/**
|
@@ -38,6 +44,12 @@ exports.DEFAULT_DEGRADED_THRESHOLD = 5000;
|
|
38
44
|
* there for more.
|
39
45
|
*
|
40
46
|
* @param options - The options to this function.
|
47
|
+
* @param options.maxRetries - The maximum number of times that a failing
|
48
|
+
* service should be re-invoked before giving up. Defaults to 3.
|
49
|
+
* @param options.retryFilterPolicy - The policy used to control when the
|
50
|
+
* service should be retried based on either the result of the servce or an
|
51
|
+
* error that it throws. For instance, you could use this to retry only certain
|
52
|
+
* errors. See `handleWhen` and friends from Cockatiel for more.
|
41
53
|
* @param options.maxConsecutiveFailures - The maximum number of times that the
|
42
54
|
* service is allowed to fail before pausing further retries. Defaults to 12.
|
43
55
|
* @param options.circuitBreakDuration - The length of time (in milliseconds) to
|
@@ -46,14 +58,6 @@ exports.DEFAULT_DEGRADED_THRESHOLD = 5000;
|
|
46
58
|
* @param options.degradedThreshold - The length of time (in milliseconds) that
|
47
59
|
* governs when the service is regarded as degraded (affecting when `onDegraded`
|
48
60
|
* is called). Defaults to 5 seconds.
|
49
|
-
* @param options.onBreak - A function which is called when the service fails
|
50
|
-
* too many times in a row (specifically, more than `maxConsecutiveFailures`).
|
51
|
-
* @param options.onDegraded - A function which is called when the service
|
52
|
-
* succeeds before `maxConsecutiveFailures` is reached, but takes more time than
|
53
|
-
* the `degradedThreshold` to run.
|
54
|
-
* @param options.onRetry - A function which will be called the moment the
|
55
|
-
* policy kicks off a timer to re-run the function passed to the policy. This is
|
56
|
-
* primarily useful in tests where we are mocking timers.
|
57
61
|
* @returns The service policy.
|
58
62
|
* @example
|
59
63
|
* This function is designed to be used in the context of a service class like
|
@@ -62,6 +66,10 @@ exports.DEFAULT_DEGRADED_THRESHOLD = 5000;
|
|
62
66
|
* class Service {
|
63
67
|
* constructor() {
|
64
68
|
* this.#policy = createServicePolicy({
|
69
|
+
* maxRetries: 3,
|
70
|
+
* retryFilterPolicy: handleWhen((error) => {
|
71
|
+
* return error.message.includes('oops');
|
72
|
+
* }),
|
65
73
|
* maxConsecutiveFailures: 3,
|
66
74
|
* circuitBreakDuration: 5000,
|
67
75
|
* degradedThreshold: 2000,
|
@@ -83,21 +91,16 @@ exports.DEFAULT_DEGRADED_THRESHOLD = 5000;
|
|
83
91
|
* }
|
84
92
|
* ```
|
85
93
|
*/
|
86
|
-
function createServicePolicy({ maxConsecutiveFailures = exports.DEFAULT_MAX_CONSECUTIVE_FAILURES, circuitBreakDuration = exports.DEFAULT_CIRCUIT_BREAK_DURATION, degradedThreshold = exports.DEFAULT_DEGRADED_THRESHOLD,
|
87
|
-
|
88
|
-
}, onDegraded = () => {
|
89
|
-
// do nothing
|
90
|
-
}, onRetry = () => {
|
91
|
-
// do nothing
|
92
|
-
}, } = {}) {
|
93
|
-
const retryPolicy = (0, cockatiel_1.retry)(cockatiel_1.handleAll, {
|
94
|
+
function createServicePolicy({ maxRetries = exports.DEFAULT_MAX_RETRIES, retryFilterPolicy = cockatiel_1.handleAll, maxConsecutiveFailures = exports.DEFAULT_MAX_CONSECUTIVE_FAILURES, circuitBreakDuration = exports.DEFAULT_CIRCUIT_BREAK_DURATION, degradedThreshold = exports.DEFAULT_DEGRADED_THRESHOLD, } = {}) {
|
95
|
+
const retryPolicy = (0, cockatiel_1.retry)(retryFilterPolicy, {
|
94
96
|
// Note that although the option here is called "max attempts", it's really
|
95
97
|
// maximum number of *retries* (attempts past the initial attempt).
|
96
|
-
maxAttempts:
|
98
|
+
maxAttempts: maxRetries,
|
97
99
|
// Retries of the service will be executed following ever increasing delays,
|
98
100
|
// determined by a backoff formula.
|
99
101
|
backoff: new cockatiel_1.ExponentialBackoff(),
|
100
102
|
});
|
103
|
+
const onRetry = retryPolicy.onRetry.bind(retryPolicy);
|
101
104
|
const circuitBreakerPolicy = (0, cockatiel_1.circuitBreaker)(cockatiel_1.handleAll, {
|
102
105
|
// While the circuit is open, any additional invocations of the service
|
103
106
|
// passed to the policy (either via automatic retries or by manually
|
@@ -108,38 +111,31 @@ function createServicePolicy({ maxConsecutiveFailures = exports.DEFAULT_MAX_CONS
|
|
108
111
|
halfOpenAfter: circuitBreakDuration,
|
109
112
|
breaker: new cockatiel_1.ConsecutiveBreaker(maxConsecutiveFailures),
|
110
113
|
});
|
111
|
-
|
112
|
-
|
113
|
-
// Combined with the retry policy, this can happen if:
|
114
|
-
// - `maxConsecutiveFailures` < the default max retries (3) and the policy is
|
115
|
-
// executed once
|
116
|
-
// - `maxConsecutiveFailures` >= the default max retries (3) but the policy is
|
117
|
-
// executed multiple times, enough for the total number of retries to exceed
|
118
|
-
// `maxConsecutiveFailures`
|
119
|
-
circuitBreakerPolicy.onBreak(onBreak);
|
120
|
-
// The `onRetryPolicy` callback will be called each time the service is
|
121
|
-
// invoked (including retries).
|
122
|
-
retryPolicy.onRetry(onRetry);
|
114
|
+
const onBreak = circuitBreakerPolicy.onBreak.bind(circuitBreakerPolicy);
|
115
|
+
const onDegradedEventEmitter = new cockatiel_1.EventEmitter();
|
123
116
|
retryPolicy.onGiveUp(() => {
|
124
117
|
if (circuitBreakerPolicy.state === cockatiel_1.CircuitState.Closed) {
|
125
|
-
|
126
|
-
// exceeded and the maximum number of consecutive failures has not been
|
127
|
-
// reached yet (whether the policy is called once or multiple times).
|
128
|
-
onDegraded();
|
118
|
+
onDegradedEventEmitter.emit();
|
129
119
|
}
|
130
120
|
});
|
131
121
|
retryPolicy.onSuccess(({ duration }) => {
|
132
122
|
if (circuitBreakerPolicy.state === cockatiel_1.CircuitState.Closed &&
|
133
123
|
duration > degradedThreshold) {
|
134
|
-
|
135
|
-
// throw, but the time it takes for the service to run exceeds the
|
136
|
-
// `degradedThreshold`.
|
137
|
-
onDegraded();
|
124
|
+
onDegradedEventEmitter.emit();
|
138
125
|
}
|
139
126
|
});
|
140
|
-
|
141
|
-
// the
|
142
|
-
|
127
|
+
const onDegraded = onDegradedEventEmitter.addListener;
|
128
|
+
// Every time the retry policy makes an attempt, it executes the circuit
|
129
|
+
// breaker policy, which executes the service.
|
130
|
+
const policy = (0, cockatiel_1.wrap)(retryPolicy, circuitBreakerPolicy);
|
131
|
+
return {
|
132
|
+
...policy,
|
133
|
+
circuitBreakerPolicy,
|
134
|
+
retryPolicy,
|
135
|
+
onBreak,
|
136
|
+
onDegraded,
|
137
|
+
onRetry,
|
138
|
+
};
|
143
139
|
}
|
144
140
|
exports.createServicePolicy = createServicePolicy;
|
145
141
|
//# sourceMappingURL=create-service-policy.cjs.map
|
@@ -1 +1 @@
|
|
1
|
-
{"version":3,"file":"create-service-policy.cjs","sourceRoot":"","sources":["../src/create-service-policy.ts"],"names":[],"mappings":";;;AAAA,yCAQmB;AAKnB;;;GAGG;AACU,QAAA,mBAAmB,GAAG,CAAC,CAAC;AAErC;;;GAGG;AACU,QAAA,gCAAgC,GAAG,CAAC,CAAC,GAAG,2BAAmB,CAAC,GAAG,CAAC,CAAC;AAE9E;;;GAGG;AACU,QAAA,8BAA8B,GAAG,EAAE,GAAG,EAAE,GAAG,IAAI,CAAC;AAE7D;;;GAGG;AACU,QAAA,0BAA0B,GAAG,IAAK,CAAC;AAEhD;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;GA4DG;AACH,SAAgB,mBAAmB,CAAC,EAClC,sBAAsB,GAAG,wCAAgC,EACzD,oBAAoB,GAAG,sCAA8B,EACrD,iBAAiB,GAAG,kCAA0B,EAC9C,OAAO,GAAG,GAAG,EAAE;IACb,aAAa;AACf,CAAC,EACD,UAAU,GAAG,GAAG,EAAE;IAChB,aAAa;AACf,CAAC,EACD,OAAO,GAAG,GAAG,EAAE;IACb,aAAa;AACf,CAAC,MAQC,EAAE;IACJ,MAAM,WAAW,GAAG,IAAA,iBAAK,EAAC,qBAAS,EAAE;QACnC,2EAA2E;QAC3E,mEAAmE;QACnE,WAAW,EAAE,2BAAmB;QAChC,4EAA4E;QAC5E,mCAAmC;QACnC,OAAO,EAAE,IAAI,8BAAkB,EAAE;KAClC,CAAC,CAAC;IAEH,MAAM,oBAAoB,GAAG,IAAA,0BAAc,EAAC,qBAAS,EAAE;QACrD,uEAAuE;QACvE,oEAAoE;QACpE,wEAAwE;QACxE,4EAA4E;QAC5E,qEAAqE;QACrE,qDAAqD;QACrD,aAAa,EAAE,oBAAoB;QACnC,OAAO,EAAE,IAAI,8BAAkB,CAAC,sBAAsB,CAAC;KACxD,CAAC,CAAC;IAEH,2EAA2E;IAC3E,2EAA2E;IAC3E,sDAAsD;IACtD,6EAA6E;IAC7E,gBAAgB;IAChB,8EAA8E;IAC9E,8EAA8E;IAC9E,6BAA6B;IAC7B,oBAAoB,CAAC,OAAO,CAAC,OAAO,CAAC,CAAC;IAEtC,uEAAuE;IACvE,+BAA+B;IAC/B,WAAW,CAAC,OAAO,CAAC,OAAO,CAAC,CAAC;IAE7B,WAAW,CAAC,QAAQ,CAAC,GAAG,EAAE;QACxB,IAAI,oBAAoB,CAAC,KAAK,KAAK,wBAAY,CAAC,MAAM,EAAE;YACtD,uEAAuE;YACvE,uEAAuE;YACvE,qEAAqE;YACrE,UAAU,EAAE,CAAC;SACd;IACH,CAAC,CAAC,CAAC;IACH,WAAW,CAAC,SAAS,CAAC,CAAC,EAAE,QAAQ,EAAE,EAAE,EAAE;QACrC,IACE,oBAAoB,CAAC,KAAK,KAAK,wBAAY,CAAC,MAAM;YAClD,QAAQ,GAAG,iBAAiB,EAC5B;YACA,wEAAwE;YACxE,kEAAkE;YAClE,uBAAuB;YACvB,UAAU,EAAE,CAAC;SACd;IACH,CAAC,CAAC,CAAC;IAEH,4EAA4E;IAC5E,eAAe;IACf,OAAO,IAAA,gBAAI,EAAC,WAAW,EAAE,oBAAoB,CAAC,CAAC;AACjD,CAAC;AA9ED,kDA8EC","sourcesContent":["import {\n circuitBreaker,\n ConsecutiveBreaker,\n ExponentialBackoff,\n handleAll,\n retry,\n wrap,\n CircuitState,\n} from 'cockatiel';\nimport type { IPolicy } from 'cockatiel';\n\nexport type { IPolicy as IServicePolicy };\n\n/**\n * The maximum number of times that a failing service should be re-run before\n * giving up.\n */\nexport const DEFAULT_MAX_RETRIES = 3;\n\n/**\n * The maximum number of times that the service is allowed to fail before\n * pausing further retries.\n */\nexport const DEFAULT_MAX_CONSECUTIVE_FAILURES = (1 + DEFAULT_MAX_RETRIES) * 3;\n\n/**\n * The default length of time (in milliseconds) to temporarily pause retries of\n * the service after enough consecutive failures.\n */\nexport const DEFAULT_CIRCUIT_BREAK_DURATION = 30 * 60 * 1000;\n\n/**\n * The default length of time (in milliseconds) that governs when the service is\n * regarded as degraded (affecting when `onDegraded` is called).\n */\nexport const DEFAULT_DEGRADED_THRESHOLD = 5_000;\n\n/**\n * Constructs an object exposing an `execute` method which, given a function —\n * hereafter called the \"service\" — will retry that service with ever increasing\n * delays until it succeeds. If the policy detects too many consecutive\n * failures, it will block further retries until a designated time period has\n * passed; this particular behavior is primarily designed for services that wrap\n * API calls so as not to make needless HTTP requests when the API is down and\n * to be able to recover when the API comes back up. In addition, hooks allow\n * for responding to certain events, one of which can be used to detect when an\n * HTTP request is performing slowly.\n *\n * Internally, this function makes use of the retry and circuit breaker policies\n * from the [Cockatiel](https://www.npmjs.com/package/cockatiel) library; see\n * there for more.\n *\n * @param options - The options to this function.\n * @param options.maxConsecutiveFailures - The maximum number of times that the\n * service is allowed to fail before pausing further retries. Defaults to 12.\n * @param options.circuitBreakDuration - The length of time (in milliseconds) to\n * pause retries of the action after the number of failures reaches\n * `maxConsecutiveFailures`.\n * @param options.degradedThreshold - The length of time (in milliseconds) that\n * governs when the service is regarded as degraded (affecting when `onDegraded`\n * is called). Defaults to 5 seconds.\n * @param options.onBreak - A function which is called when the service fails\n * too many times in a row (specifically, more than `maxConsecutiveFailures`).\n * @param options.onDegraded - A function which is called when the service\n * succeeds before `maxConsecutiveFailures` is reached, but takes more time than\n * the `degradedThreshold` to run.\n * @param options.onRetry - A function which will be called the moment the\n * policy kicks off a timer to re-run the function passed to the policy. This is\n * primarily useful in tests where we are mocking timers.\n * @returns The service policy.\n * @example\n * This function is designed to be used in the context of a service class like\n * this:\n * ``` ts\n * class Service {\n * constructor() {\n * this.#policy = createServicePolicy({\n * maxConsecutiveFailures: 3,\n * circuitBreakDuration: 5000,\n * degradedThreshold: 2000,\n * onBreak: () => {\n * console.log('Circuit broke');\n * },\n * onDegraded: () => {\n * console.log('Service is degraded');\n * },\n * });\n * }\n *\n * async fetch() {\n * return await this.#policy.execute(async () => {\n * const response = await fetch('https://some/url');\n * return await response.json();\n * });\n * }\n * }\n * ```\n */\nexport function createServicePolicy({\n maxConsecutiveFailures = DEFAULT_MAX_CONSECUTIVE_FAILURES,\n circuitBreakDuration = DEFAULT_CIRCUIT_BREAK_DURATION,\n degradedThreshold = DEFAULT_DEGRADED_THRESHOLD,\n onBreak = () => {\n // do nothing\n },\n onDegraded = () => {\n // do nothing\n },\n onRetry = () => {\n // do nothing\n },\n}: {\n maxConsecutiveFailures?: number;\n circuitBreakDuration?: number;\n degradedThreshold?: number;\n onBreak?: () => void;\n onDegraded?: () => void;\n onRetry?: () => void;\n} = {}): IPolicy {\n const retryPolicy = retry(handleAll, {\n // Note that although the option here is called \"max attempts\", it's really\n // maximum number of *retries* (attempts past the initial attempt).\n maxAttempts: DEFAULT_MAX_RETRIES,\n // Retries of the service will be executed following ever increasing delays,\n // determined by a backoff formula.\n backoff: new ExponentialBackoff(),\n });\n\n const circuitBreakerPolicy = circuitBreaker(handleAll, {\n // While the circuit is open, any additional invocations of the service\n // passed to the policy (either via automatic retries or by manually\n // executing the policy again) will result in a BrokenCircuitError. This\n // will remain the case until `circuitBreakDuration` passes, after which the\n // service will be allowed to run again. If the service succeeds, the\n // circuit will close, otherwise it will remain open.\n halfOpenAfter: circuitBreakDuration,\n breaker: new ConsecutiveBreaker(maxConsecutiveFailures),\n });\n\n // The `onBreak` callback will be called if the service consistently throws\n // for as many times as exceeds the maximum consecutive number of failures.\n // Combined with the retry policy, this can happen if:\n // - `maxConsecutiveFailures` < the default max retries (3) and the policy is\n // executed once\n // - `maxConsecutiveFailures` >= the default max retries (3) but the policy is\n // executed multiple times, enough for the total number of retries to exceed\n // `maxConsecutiveFailures`\n circuitBreakerPolicy.onBreak(onBreak);\n\n // The `onRetryPolicy` callback will be called each time the service is\n // invoked (including retries).\n retryPolicy.onRetry(onRetry);\n\n retryPolicy.onGiveUp(() => {\n if (circuitBreakerPolicy.state === CircuitState.Closed) {\n // The `onDegraded` callback will be called if the number of retries is\n // exceeded and the maximum number of consecutive failures has not been\n // reached yet (whether the policy is called once or multiple times).\n onDegraded();\n }\n });\n retryPolicy.onSuccess(({ duration }) => {\n if (\n circuitBreakerPolicy.state === CircuitState.Closed &&\n duration > degradedThreshold\n ) {\n // The `onDegraded` callback will also be called if the service does not\n // throw, but the time it takes for the service to run exceeds the\n // `degradedThreshold`.\n onDegraded();\n }\n });\n\n // The retry policy really retries the circuit breaker policy, which invokes\n // the service.\n return wrap(retryPolicy, circuitBreakerPolicy);\n}\n"]}
|
1
|
+
{"version":3,"file":"create-service-policy.cjs","sourceRoot":"","sources":["../src/create-service-policy.ts"],"names":[],"mappings":";;;AAAA,yCAWmB;AASI,mGAnBrB,8BAAkB,OAmBqB;AAAhC,6FAlBP,wBAAY,OAkBO;AAAsB,0FAbzC,qBAAS,OAayC;AAAE,2FAZpD,sBAAU,OAYoD;AAuEhE;;;GAGG;AACU,QAAA,mBAAmB,GAAG,CAAC,CAAC;AAErC;;;;;GAKG;AACU,QAAA,gCAAgC,GAAG,CAAC,CAAC,GAAG,2BAAmB,CAAC,GAAG,CAAC,CAAC;AAE9E;;;GAGG;AACU,QAAA,8BAA8B,GAAG,EAAE,GAAG,EAAE,GAAG,IAAI,CAAC;AAE7D;;;GAGG;AACU,QAAA,0BAA0B,GAAG,IAAK,CAAC;AAEhD;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;GA8DG;AACH,SAAgB,mBAAmB,CAAC,EAClC,UAAU,GAAG,2BAAmB,EAChC,iBAAiB,GAAG,qBAAS,EAC7B,sBAAsB,GAAG,wCAAgC,EACzD,oBAAoB,GAAG,sCAA8B,EACrD,iBAAiB,GAAG,kCAA0B,MAChB,EAAE;IAChC,MAAM,WAAW,GAAG,IAAA,iBAAK,EAAC,iBAAiB,EAAE;QAC3C,2EAA2E;QAC3E,mEAAmE;QACnE,WAAW,EAAE,UAAU;QACvB,4EAA4E;QAC5E,mCAAmC;QACnC,OAAO,EAAE,IAAI,8BAAkB,EAAE;KAClC,CAAC,CAAC;IACH,MAAM,OAAO,GAAG,WAAW,CAAC,OAAO,CAAC,IAAI,CAAC,WAAW,CAAC,CAAC;IAEtD,MAAM,oBAAoB,GAAG,IAAA,0BAAc,EAAC,qBAAS,EAAE;QACrD,uEAAuE;QACvE,oEAAoE;QACpE,wEAAwE;QACxE,4EAA4E;QAC5E,qEAAqE;QACrE,qDAAqD;QACrD,aAAa,EAAE,oBAAoB;QACnC,OAAO,EAAE,IAAI,8BAAkB,CAAC,sBAAsB,CAAC;KACxD,CAAC,CAAC;IACH,MAAM,OAAO,GAAG,oBAAoB,CAAC,OAAO,CAAC,IAAI,CAAC,oBAAoB,CAAC,CAAC;IAExE,MAAM,sBAAsB,GAAG,IAAI,wBAAqB,EAAQ,CAAC;IACjE,WAAW,CAAC,QAAQ,CAAC,GAAG,EAAE;QACxB,IAAI,oBAAoB,CAAC,KAAK,KAAK,wBAAY,CAAC,MAAM,EAAE;YACtD,sBAAsB,CAAC,IAAI,EAAE,CAAC;SAC/B;IACH,CAAC,CAAC,CAAC;IACH,WAAW,CAAC,SAAS,CAAC,CAAC,EAAE,QAAQ,EAAE,EAAE,EAAE;QACrC,IACE,oBAAoB,CAAC,KAAK,KAAK,wBAAY,CAAC,MAAM;YAClD,QAAQ,GAAG,iBAAiB,EAC5B;YACA,sBAAsB,CAAC,IAAI,EAAE,CAAC;SAC/B;IACH,CAAC,CAAC,CAAC;IACH,MAAM,UAAU,GAAG,sBAAsB,CAAC,WAAW,CAAC;IAEtD,wEAAwE;IACxE,8CAA8C;IAC9C,MAAM,MAAM,GAAG,IAAA,gBAAI,EAAC,WAAW,EAAE,oBAAoB,CAAC,CAAC;IAEvD,OAAO;QACL,GAAG,MAAM;QACT,oBAAoB;QACpB,WAAW;QACX,OAAO;QACP,UAAU;QACV,OAAO;KACR,CAAC;AACJ,CAAC;AAzDD,kDAyDC","sourcesContent":["import {\n BrokenCircuitError,\n CircuitState,\n EventEmitter as CockatielEventEmitter,\n ConsecutiveBreaker,\n ExponentialBackoff,\n circuitBreaker,\n handleAll,\n handleWhen,\n retry,\n wrap,\n} from 'cockatiel';\nimport type {\n CircuitBreakerPolicy,\n Event as CockatielEvent,\n IPolicy,\n Policy,\n RetryPolicy,\n} from 'cockatiel';\n\nexport { CircuitState, BrokenCircuitError, handleAll, handleWhen };\n\nexport type { CockatielEvent };\n\n/**\n * The options for `createServicePolicy`.\n */\nexport type CreateServicePolicyOptions = {\n /**\n * The length of time (in milliseconds) to pause retries of the action after\n * the number of failures reaches `maxConsecutiveFailures`.\n */\n circuitBreakDuration?: number;\n /**\n * The length of time (in milliseconds) that governs when the service is\n * regarded as degraded (affecting when `onDegraded` is called).\n */\n degradedThreshold?: number;\n /**\n * The maximum number of times that the service is allowed to fail before\n * pausing further retries.\n */\n maxConsecutiveFailures?: number;\n /**\n * The maximum number of times that a failing service should be re-invoked\n * before giving up.\n */\n maxRetries?: number;\n /**\n * The policy used to control when the service should be retried based on\n * either the result of the service or an error that it throws. For instance,\n * you could use this to retry only certain errors. See `handleWhen` and\n * friends from Cockatiel for more.\n */\n retryFilterPolicy?: Policy;\n};\n\n/**\n * The service policy object.\n */\nexport type ServicePolicy = IPolicy & {\n /**\n * The Cockatiel circuit breaker policy that the service policy uses\n * internally.\n */\n circuitBreakerPolicy: CircuitBreakerPolicy;\n /**\n * The Cockatiel retry policy that the service policy uses internally.\n */\n retryPolicy: RetryPolicy;\n /**\n * A function which is called when the number of times that the service fails\n * in a row meets the set maximum number of consecutive failures.\n */\n onBreak: CircuitBreakerPolicy['onBreak'];\n /**\n * A function which is called in two circumstances: 1) when the service\n * succeeds before the maximum number of consecutive failures is reached, but\n * takes more time than the `degradedThreshold` to run, or 2) if the service\n * never succeeds before the retry policy gives up and before the maximum\n * number of consecutive failures has been reached.\n */\n onDegraded: CockatielEvent<void>;\n /**\n * A function which will be called by the retry policy each time the service\n * fails and the policy kicks off a timer to re-run the service. This is\n * primarily useful in tests where we are mocking timers.\n */\n onRetry: RetryPolicy['onRetry'];\n};\n\n/**\n * The maximum number of times that a failing service should be re-run before\n * giving up.\n */\nexport const DEFAULT_MAX_RETRIES = 3;\n\n/**\n * The maximum number of times that the service is allowed to fail before\n * pausing further retries. This is set to a value such that if given a\n * service that continually fails, the policy needs to be executed 3 times\n * before further retries are paused.\n */\nexport const DEFAULT_MAX_CONSECUTIVE_FAILURES = (1 + DEFAULT_MAX_RETRIES) * 3;\n\n/**\n * The default length of time (in milliseconds) to temporarily pause retries of\n * the service after enough consecutive failures.\n */\nexport const DEFAULT_CIRCUIT_BREAK_DURATION = 30 * 60 * 1000;\n\n/**\n * The default length of time (in milliseconds) that governs when the service is\n * regarded as degraded (affecting when `onDegraded` is called).\n */\nexport const DEFAULT_DEGRADED_THRESHOLD = 5_000;\n\n/**\n * Constructs an object exposing an `execute` method which, given a function —\n * hereafter called the \"service\" — will retry that service with ever increasing\n * delays until it succeeds. If the policy detects too many consecutive\n * failures, it will block further retries until a designated time period has\n * passed; this particular behavior is primarily designed for services that wrap\n * API calls so as not to make needless HTTP requests when the API is down and\n * to be able to recover when the API comes back up. In addition, hooks allow\n * for responding to certain events, one of which can be used to detect when an\n * HTTP request is performing slowly.\n *\n * Internally, this function makes use of the retry and circuit breaker policies\n * from the [Cockatiel](https://www.npmjs.com/package/cockatiel) library; see\n * there for more.\n *\n * @param options - The options to this function.\n * @param options.maxRetries - The maximum number of times that a failing\n * service should be re-invoked before giving up. Defaults to 3.\n * @param options.retryFilterPolicy - The policy used to control when the\n * service should be retried based on either the result of the servce or an\n * error that it throws. For instance, you could use this to retry only certain\n * errors. See `handleWhen` and friends from Cockatiel for more.\n * @param options.maxConsecutiveFailures - The maximum number of times that the\n * service is allowed to fail before pausing further retries. Defaults to 12.\n * @param options.circuitBreakDuration - The length of time (in milliseconds) to\n * pause retries of the action after the number of failures reaches\n * `maxConsecutiveFailures`.\n * @param options.degradedThreshold - The length of time (in milliseconds) that\n * governs when the service is regarded as degraded (affecting when `onDegraded`\n * is called). Defaults to 5 seconds.\n * @returns The service policy.\n * @example\n * This function is designed to be used in the context of a service class like\n * this:\n * ``` ts\n * class Service {\n * constructor() {\n * this.#policy = createServicePolicy({\n * maxRetries: 3,\n * retryFilterPolicy: handleWhen((error) => {\n * return error.message.includes('oops');\n * }),\n * maxConsecutiveFailures: 3,\n * circuitBreakDuration: 5000,\n * degradedThreshold: 2000,\n * onBreak: () => {\n * console.log('Circuit broke');\n * },\n * onDegraded: () => {\n * console.log('Service is degraded');\n * },\n * });\n * }\n *\n * async fetch() {\n * return await this.#policy.execute(async () => {\n * const response = await fetch('https://some/url');\n * return await response.json();\n * });\n * }\n * }\n * ```\n */\nexport function createServicePolicy({\n maxRetries = DEFAULT_MAX_RETRIES,\n retryFilterPolicy = handleAll,\n maxConsecutiveFailures = DEFAULT_MAX_CONSECUTIVE_FAILURES,\n circuitBreakDuration = DEFAULT_CIRCUIT_BREAK_DURATION,\n degradedThreshold = DEFAULT_DEGRADED_THRESHOLD,\n}: CreateServicePolicyOptions = {}): ServicePolicy {\n const retryPolicy = retry(retryFilterPolicy, {\n // Note that although the option here is called \"max attempts\", it's really\n // maximum number of *retries* (attempts past the initial attempt).\n maxAttempts: maxRetries,\n // Retries of the service will be executed following ever increasing delays,\n // determined by a backoff formula.\n backoff: new ExponentialBackoff(),\n });\n const onRetry = retryPolicy.onRetry.bind(retryPolicy);\n\n const circuitBreakerPolicy = circuitBreaker(handleAll, {\n // While the circuit is open, any additional invocations of the service\n // passed to the policy (either via automatic retries or by manually\n // executing the policy again) will result in a BrokenCircuitError. This\n // will remain the case until `circuitBreakDuration` passes, after which the\n // service will be allowed to run again. If the service succeeds, the\n // circuit will close, otherwise it will remain open.\n halfOpenAfter: circuitBreakDuration,\n breaker: new ConsecutiveBreaker(maxConsecutiveFailures),\n });\n const onBreak = circuitBreakerPolicy.onBreak.bind(circuitBreakerPolicy);\n\n const onDegradedEventEmitter = new CockatielEventEmitter<void>();\n retryPolicy.onGiveUp(() => {\n if (circuitBreakerPolicy.state === CircuitState.Closed) {\n onDegradedEventEmitter.emit();\n }\n });\n retryPolicy.onSuccess(({ duration }) => {\n if (\n circuitBreakerPolicy.state === CircuitState.Closed &&\n duration > degradedThreshold\n ) {\n onDegradedEventEmitter.emit();\n }\n });\n const onDegraded = onDegradedEventEmitter.addListener;\n\n // Every time the retry policy makes an attempt, it executes the circuit\n // breaker policy, which executes the service.\n const policy = wrap(retryPolicy, circuitBreakerPolicy);\n\n return {\n ...policy,\n circuitBreakerPolicy,\n retryPolicy,\n onBreak,\n onDegraded,\n onRetry,\n };\n}\n"]}
|
@@ -1,5 +1,72 @@
|
|
1
|
-
import
|
2
|
-
|
1
|
+
import { BrokenCircuitError, CircuitState, handleAll, handleWhen } from "cockatiel";
|
2
|
+
import type { CircuitBreakerPolicy, Event as CockatielEvent, IPolicy, Policy, RetryPolicy } from "cockatiel";
|
3
|
+
export { CircuitState, BrokenCircuitError, handleAll, handleWhen };
|
4
|
+
export type { CockatielEvent };
|
5
|
+
/**
|
6
|
+
* The options for `createServicePolicy`.
|
7
|
+
*/
|
8
|
+
export type CreateServicePolicyOptions = {
|
9
|
+
/**
|
10
|
+
* The length of time (in milliseconds) to pause retries of the action after
|
11
|
+
* the number of failures reaches `maxConsecutiveFailures`.
|
12
|
+
*/
|
13
|
+
circuitBreakDuration?: number;
|
14
|
+
/**
|
15
|
+
* The length of time (in milliseconds) that governs when the service is
|
16
|
+
* regarded as degraded (affecting when `onDegraded` is called).
|
17
|
+
*/
|
18
|
+
degradedThreshold?: number;
|
19
|
+
/**
|
20
|
+
* The maximum number of times that the service is allowed to fail before
|
21
|
+
* pausing further retries.
|
22
|
+
*/
|
23
|
+
maxConsecutiveFailures?: number;
|
24
|
+
/**
|
25
|
+
* The maximum number of times that a failing service should be re-invoked
|
26
|
+
* before giving up.
|
27
|
+
*/
|
28
|
+
maxRetries?: number;
|
29
|
+
/**
|
30
|
+
* The policy used to control when the service should be retried based on
|
31
|
+
* either the result of the service or an error that it throws. For instance,
|
32
|
+
* you could use this to retry only certain errors. See `handleWhen` and
|
33
|
+
* friends from Cockatiel for more.
|
34
|
+
*/
|
35
|
+
retryFilterPolicy?: Policy;
|
36
|
+
};
|
37
|
+
/**
|
38
|
+
* The service policy object.
|
39
|
+
*/
|
40
|
+
export type ServicePolicy = IPolicy & {
|
41
|
+
/**
|
42
|
+
* The Cockatiel circuit breaker policy that the service policy uses
|
43
|
+
* internally.
|
44
|
+
*/
|
45
|
+
circuitBreakerPolicy: CircuitBreakerPolicy;
|
46
|
+
/**
|
47
|
+
* The Cockatiel retry policy that the service policy uses internally.
|
48
|
+
*/
|
49
|
+
retryPolicy: RetryPolicy;
|
50
|
+
/**
|
51
|
+
* A function which is called when the number of times that the service fails
|
52
|
+
* in a row meets the set maximum number of consecutive failures.
|
53
|
+
*/
|
54
|
+
onBreak: CircuitBreakerPolicy['onBreak'];
|
55
|
+
/**
|
56
|
+
* A function which is called in two circumstances: 1) when the service
|
57
|
+
* succeeds before the maximum number of consecutive failures is reached, but
|
58
|
+
* takes more time than the `degradedThreshold` to run, or 2) if the service
|
59
|
+
* never succeeds before the retry policy gives up and before the maximum
|
60
|
+
* number of consecutive failures has been reached.
|
61
|
+
*/
|
62
|
+
onDegraded: CockatielEvent<void>;
|
63
|
+
/**
|
64
|
+
* A function which will be called by the retry policy each time the service
|
65
|
+
* fails and the policy kicks off a timer to re-run the service. This is
|
66
|
+
* primarily useful in tests where we are mocking timers.
|
67
|
+
*/
|
68
|
+
onRetry: RetryPolicy['onRetry'];
|
69
|
+
};
|
3
70
|
/**
|
4
71
|
* The maximum number of times that a failing service should be re-run before
|
5
72
|
* giving up.
|
@@ -7,7 +74,9 @@ export type { IPolicy as IServicePolicy };
|
|
7
74
|
export declare const DEFAULT_MAX_RETRIES = 3;
|
8
75
|
/**
|
9
76
|
* The maximum number of times that the service is allowed to fail before
|
10
|
-
* pausing further retries.
|
77
|
+
* pausing further retries. This is set to a value such that if given a
|
78
|
+
* service that continually fails, the policy needs to be executed 3 times
|
79
|
+
* before further retries are paused.
|
11
80
|
*/
|
12
81
|
export declare const DEFAULT_MAX_CONSECUTIVE_FAILURES: number;
|
13
82
|
/**
|
@@ -36,6 +105,12 @@ export declare const DEFAULT_DEGRADED_THRESHOLD = 5000;
|
|
36
105
|
* there for more.
|
37
106
|
*
|
38
107
|
* @param options - The options to this function.
|
108
|
+
* @param options.maxRetries - The maximum number of times that a failing
|
109
|
+
* service should be re-invoked before giving up. Defaults to 3.
|
110
|
+
* @param options.retryFilterPolicy - The policy used to control when the
|
111
|
+
* service should be retried based on either the result of the servce or an
|
112
|
+
* error that it throws. For instance, you could use this to retry only certain
|
113
|
+
* errors. See `handleWhen` and friends from Cockatiel for more.
|
39
114
|
* @param options.maxConsecutiveFailures - The maximum number of times that the
|
40
115
|
* service is allowed to fail before pausing further retries. Defaults to 12.
|
41
116
|
* @param options.circuitBreakDuration - The length of time (in milliseconds) to
|
@@ -44,14 +119,6 @@ export declare const DEFAULT_DEGRADED_THRESHOLD = 5000;
|
|
44
119
|
* @param options.degradedThreshold - The length of time (in milliseconds) that
|
45
120
|
* governs when the service is regarded as degraded (affecting when `onDegraded`
|
46
121
|
* is called). Defaults to 5 seconds.
|
47
|
-
* @param options.onBreak - A function which is called when the service fails
|
48
|
-
* too many times in a row (specifically, more than `maxConsecutiveFailures`).
|
49
|
-
* @param options.onDegraded - A function which is called when the service
|
50
|
-
* succeeds before `maxConsecutiveFailures` is reached, but takes more time than
|
51
|
-
* the `degradedThreshold` to run.
|
52
|
-
* @param options.onRetry - A function which will be called the moment the
|
53
|
-
* policy kicks off a timer to re-run the function passed to the policy. This is
|
54
|
-
* primarily useful in tests where we are mocking timers.
|
55
122
|
* @returns The service policy.
|
56
123
|
* @example
|
57
124
|
* This function is designed to be used in the context of a service class like
|
@@ -60,6 +127,10 @@ export declare const DEFAULT_DEGRADED_THRESHOLD = 5000;
|
|
60
127
|
* class Service {
|
61
128
|
* constructor() {
|
62
129
|
* this.#policy = createServicePolicy({
|
130
|
+
* maxRetries: 3,
|
131
|
+
* retryFilterPolicy: handleWhen((error) => {
|
132
|
+
* return error.message.includes('oops');
|
133
|
+
* }),
|
63
134
|
* maxConsecutiveFailures: 3,
|
64
135
|
* circuitBreakDuration: 5000,
|
65
136
|
* degradedThreshold: 2000,
|
@@ -81,12 +152,5 @@ export declare const DEFAULT_DEGRADED_THRESHOLD = 5000;
|
|
81
152
|
* }
|
82
153
|
* ```
|
83
154
|
*/
|
84
|
-
export declare function createServicePolicy({
|
85
|
-
maxConsecutiveFailures?: number;
|
86
|
-
circuitBreakDuration?: number;
|
87
|
-
degradedThreshold?: number;
|
88
|
-
onBreak?: () => void;
|
89
|
-
onDegraded?: () => void;
|
90
|
-
onRetry?: () => void;
|
91
|
-
}): IPolicy;
|
155
|
+
export declare function createServicePolicy({ maxRetries, retryFilterPolicy, maxConsecutiveFailures, circuitBreakDuration, degradedThreshold, }?: CreateServicePolicyOptions): ServicePolicy;
|
92
156
|
//# sourceMappingURL=create-service-policy.d.cts.map
|
@@ -1 +1 @@
|
|
1
|
-
{"version":3,"file":"create-service-policy.d.cts","sourceRoot":"","sources":["../src/create-service-policy.ts"],"names":[],"mappings":"
|
1
|
+
{"version":3,"file":"create-service-policy.d.cts","sourceRoot":"","sources":["../src/create-service-policy.ts"],"names":[],"mappings":"AAAA,OAAO,EACL,kBAAkB,EAClB,YAAY,EAKZ,SAAS,EACT,UAAU,EAGX,kBAAkB;AACnB,OAAO,KAAK,EACV,oBAAoB,EACpB,KAAK,IAAI,cAAc,EACvB,OAAO,EACP,MAAM,EACN,WAAW,EACZ,kBAAkB;AAEnB,OAAO,EAAE,YAAY,EAAE,kBAAkB,EAAE,SAAS,EAAE,UAAU,EAAE,CAAC;AAEnE,YAAY,EAAE,cAAc,EAAE,CAAC;AAE/B;;GAEG;AACH,MAAM,MAAM,0BAA0B,GAAG;IACvC;;;OAGG;IACH,oBAAoB,CAAC,EAAE,MAAM,CAAC;IAC9B;;;OAGG;IACH,iBAAiB,CAAC,EAAE,MAAM,CAAC;IAC3B;;;OAGG;IACH,sBAAsB,CAAC,EAAE,MAAM,CAAC;IAChC;;;OAGG;IACH,UAAU,CAAC,EAAE,MAAM,CAAC;IACpB;;;;;OAKG;IACH,iBAAiB,CAAC,EAAE,MAAM,CAAC;CAC5B,CAAC;AAEF;;GAEG;AACH,MAAM,MAAM,aAAa,GAAG,OAAO,GAAG;IACpC;;;OAGG;IACH,oBAAoB,EAAE,oBAAoB,CAAC;IAC3C;;OAEG;IACH,WAAW,EAAE,WAAW,CAAC;IACzB;;;OAGG;IACH,OAAO,EAAE,oBAAoB,CAAC,SAAS,CAAC,CAAC;IACzC;;;;;;OAMG;IACH,UAAU,EAAE,cAAc,CAAC,IAAI,CAAC,CAAC;IACjC;;;;OAIG;IACH,OAAO,EAAE,WAAW,CAAC,SAAS,CAAC,CAAC;CACjC,CAAC;AAEF;;;GAGG;AACH,eAAO,MAAM,mBAAmB,IAAI,CAAC;AAErC;;;;;GAKG;AACH,eAAO,MAAM,gCAAgC,QAAgC,CAAC;AAE9E;;;GAGG;AACH,eAAO,MAAM,8BAA8B,QAAiB,CAAC;AAE7D;;;GAGG;AACH,eAAO,MAAM,0BAA0B,OAAQ,CAAC;AAEhD;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;GA8DG;AACH,wBAAgB,mBAAmB,CAAC,EAClC,UAAgC,EAChC,iBAA6B,EAC7B,sBAAyD,EACzD,oBAAqD,EACrD,iBAA8C,GAC/C,GAAE,0BAA+B,GAAG,aAAa,CAmDjD"}
|
@@ -1,5 +1,72 @@
|
|
1
|
-
import
|
2
|
-
|
1
|
+
import { BrokenCircuitError, CircuitState, handleAll, handleWhen } from "cockatiel";
|
2
|
+
import type { CircuitBreakerPolicy, Event as CockatielEvent, IPolicy, Policy, RetryPolicy } from "cockatiel";
|
3
|
+
export { CircuitState, BrokenCircuitError, handleAll, handleWhen };
|
4
|
+
export type { CockatielEvent };
|
5
|
+
/**
|
6
|
+
* The options for `createServicePolicy`.
|
7
|
+
*/
|
8
|
+
export type CreateServicePolicyOptions = {
|
9
|
+
/**
|
10
|
+
* The length of time (in milliseconds) to pause retries of the action after
|
11
|
+
* the number of failures reaches `maxConsecutiveFailures`.
|
12
|
+
*/
|
13
|
+
circuitBreakDuration?: number;
|
14
|
+
/**
|
15
|
+
* The length of time (in milliseconds) that governs when the service is
|
16
|
+
* regarded as degraded (affecting when `onDegraded` is called).
|
17
|
+
*/
|
18
|
+
degradedThreshold?: number;
|
19
|
+
/**
|
20
|
+
* The maximum number of times that the service is allowed to fail before
|
21
|
+
* pausing further retries.
|
22
|
+
*/
|
23
|
+
maxConsecutiveFailures?: number;
|
24
|
+
/**
|
25
|
+
* The maximum number of times that a failing service should be re-invoked
|
26
|
+
* before giving up.
|
27
|
+
*/
|
28
|
+
maxRetries?: number;
|
29
|
+
/**
|
30
|
+
* The policy used to control when the service should be retried based on
|
31
|
+
* either the result of the service or an error that it throws. For instance,
|
32
|
+
* you could use this to retry only certain errors. See `handleWhen` and
|
33
|
+
* friends from Cockatiel for more.
|
34
|
+
*/
|
35
|
+
retryFilterPolicy?: Policy;
|
36
|
+
};
|
37
|
+
/**
|
38
|
+
* The service policy object.
|
39
|
+
*/
|
40
|
+
export type ServicePolicy = IPolicy & {
|
41
|
+
/**
|
42
|
+
* The Cockatiel circuit breaker policy that the service policy uses
|
43
|
+
* internally.
|
44
|
+
*/
|
45
|
+
circuitBreakerPolicy: CircuitBreakerPolicy;
|
46
|
+
/**
|
47
|
+
* The Cockatiel retry policy that the service policy uses internally.
|
48
|
+
*/
|
49
|
+
retryPolicy: RetryPolicy;
|
50
|
+
/**
|
51
|
+
* A function which is called when the number of times that the service fails
|
52
|
+
* in a row meets the set maximum number of consecutive failures.
|
53
|
+
*/
|
54
|
+
onBreak: CircuitBreakerPolicy['onBreak'];
|
55
|
+
/**
|
56
|
+
* A function which is called in two circumstances: 1) when the service
|
57
|
+
* succeeds before the maximum number of consecutive failures is reached, but
|
58
|
+
* takes more time than the `degradedThreshold` to run, or 2) if the service
|
59
|
+
* never succeeds before the retry policy gives up and before the maximum
|
60
|
+
* number of consecutive failures has been reached.
|
61
|
+
*/
|
62
|
+
onDegraded: CockatielEvent<void>;
|
63
|
+
/**
|
64
|
+
* A function which will be called by the retry policy each time the service
|
65
|
+
* fails and the policy kicks off a timer to re-run the service. This is
|
66
|
+
* primarily useful in tests where we are mocking timers.
|
67
|
+
*/
|
68
|
+
onRetry: RetryPolicy['onRetry'];
|
69
|
+
};
|
3
70
|
/**
|
4
71
|
* The maximum number of times that a failing service should be re-run before
|
5
72
|
* giving up.
|
@@ -7,7 +74,9 @@ export type { IPolicy as IServicePolicy };
|
|
7
74
|
export declare const DEFAULT_MAX_RETRIES = 3;
|
8
75
|
/**
|
9
76
|
* The maximum number of times that the service is allowed to fail before
|
10
|
-
* pausing further retries.
|
77
|
+
* pausing further retries. This is set to a value such that if given a
|
78
|
+
* service that continually fails, the policy needs to be executed 3 times
|
79
|
+
* before further retries are paused.
|
11
80
|
*/
|
12
81
|
export declare const DEFAULT_MAX_CONSECUTIVE_FAILURES: number;
|
13
82
|
/**
|
@@ -36,6 +105,12 @@ export declare const DEFAULT_DEGRADED_THRESHOLD = 5000;
|
|
36
105
|
* there for more.
|
37
106
|
*
|
38
107
|
* @param options - The options to this function.
|
108
|
+
* @param options.maxRetries - The maximum number of times that a failing
|
109
|
+
* service should be re-invoked before giving up. Defaults to 3.
|
110
|
+
* @param options.retryFilterPolicy - The policy used to control when the
|
111
|
+
* service should be retried based on either the result of the servce or an
|
112
|
+
* error that it throws. For instance, you could use this to retry only certain
|
113
|
+
* errors. See `handleWhen` and friends from Cockatiel for more.
|
39
114
|
* @param options.maxConsecutiveFailures - The maximum number of times that the
|
40
115
|
* service is allowed to fail before pausing further retries. Defaults to 12.
|
41
116
|
* @param options.circuitBreakDuration - The length of time (in milliseconds) to
|
@@ -44,14 +119,6 @@ export declare const DEFAULT_DEGRADED_THRESHOLD = 5000;
|
|
44
119
|
* @param options.degradedThreshold - The length of time (in milliseconds) that
|
45
120
|
* governs when the service is regarded as degraded (affecting when `onDegraded`
|
46
121
|
* is called). Defaults to 5 seconds.
|
47
|
-
* @param options.onBreak - A function which is called when the service fails
|
48
|
-
* too many times in a row (specifically, more than `maxConsecutiveFailures`).
|
49
|
-
* @param options.onDegraded - A function which is called when the service
|
50
|
-
* succeeds before `maxConsecutiveFailures` is reached, but takes more time than
|
51
|
-
* the `degradedThreshold` to run.
|
52
|
-
* @param options.onRetry - A function which will be called the moment the
|
53
|
-
* policy kicks off a timer to re-run the function passed to the policy. This is
|
54
|
-
* primarily useful in tests where we are mocking timers.
|
55
122
|
* @returns The service policy.
|
56
123
|
* @example
|
57
124
|
* This function is designed to be used in the context of a service class like
|
@@ -60,6 +127,10 @@ export declare const DEFAULT_DEGRADED_THRESHOLD = 5000;
|
|
60
127
|
* class Service {
|
61
128
|
* constructor() {
|
62
129
|
* this.#policy = createServicePolicy({
|
130
|
+
* maxRetries: 3,
|
131
|
+
* retryFilterPolicy: handleWhen((error) => {
|
132
|
+
* return error.message.includes('oops');
|
133
|
+
* }),
|
63
134
|
* maxConsecutiveFailures: 3,
|
64
135
|
* circuitBreakDuration: 5000,
|
65
136
|
* degradedThreshold: 2000,
|
@@ -81,12 +152,5 @@ export declare const DEFAULT_DEGRADED_THRESHOLD = 5000;
|
|
81
152
|
* }
|
82
153
|
* ```
|
83
154
|
*/
|
84
|
-
export declare function createServicePolicy({
|
85
|
-
maxConsecutiveFailures?: number;
|
86
|
-
circuitBreakDuration?: number;
|
87
|
-
degradedThreshold?: number;
|
88
|
-
onBreak?: () => void;
|
89
|
-
onDegraded?: () => void;
|
90
|
-
onRetry?: () => void;
|
91
|
-
}): IPolicy;
|
155
|
+
export declare function createServicePolicy({ maxRetries, retryFilterPolicy, maxConsecutiveFailures, circuitBreakDuration, degradedThreshold, }?: CreateServicePolicyOptions): ServicePolicy;
|
92
156
|
//# sourceMappingURL=create-service-policy.d.mts.map
|
@@ -1 +1 @@
|
|
1
|
-
{"version":3,"file":"create-service-policy.d.mts","sourceRoot":"","sources":["../src/create-service-policy.ts"],"names":[],"mappings":"
|
1
|
+
{"version":3,"file":"create-service-policy.d.mts","sourceRoot":"","sources":["../src/create-service-policy.ts"],"names":[],"mappings":"AAAA,OAAO,EACL,kBAAkB,EAClB,YAAY,EAKZ,SAAS,EACT,UAAU,EAGX,kBAAkB;AACnB,OAAO,KAAK,EACV,oBAAoB,EACpB,KAAK,IAAI,cAAc,EACvB,OAAO,EACP,MAAM,EACN,WAAW,EACZ,kBAAkB;AAEnB,OAAO,EAAE,YAAY,EAAE,kBAAkB,EAAE,SAAS,EAAE,UAAU,EAAE,CAAC;AAEnE,YAAY,EAAE,cAAc,EAAE,CAAC;AAE/B;;GAEG;AACH,MAAM,MAAM,0BAA0B,GAAG;IACvC;;;OAGG;IACH,oBAAoB,CAAC,EAAE,MAAM,CAAC;IAC9B;;;OAGG;IACH,iBAAiB,CAAC,EAAE,MAAM,CAAC;IAC3B;;;OAGG;IACH,sBAAsB,CAAC,EAAE,MAAM,CAAC;IAChC;;;OAGG;IACH,UAAU,CAAC,EAAE,MAAM,CAAC;IACpB;;;;;OAKG;IACH,iBAAiB,CAAC,EAAE,MAAM,CAAC;CAC5B,CAAC;AAEF;;GAEG;AACH,MAAM,MAAM,aAAa,GAAG,OAAO,GAAG;IACpC;;;OAGG;IACH,oBAAoB,EAAE,oBAAoB,CAAC;IAC3C;;OAEG;IACH,WAAW,EAAE,WAAW,CAAC;IACzB;;;OAGG;IACH,OAAO,EAAE,oBAAoB,CAAC,SAAS,CAAC,CAAC;IACzC;;;;;;OAMG;IACH,UAAU,EAAE,cAAc,CAAC,IAAI,CAAC,CAAC;IACjC;;;;OAIG;IACH,OAAO,EAAE,WAAW,CAAC,SAAS,CAAC,CAAC;CACjC,CAAC;AAEF;;;GAGG;AACH,eAAO,MAAM,mBAAmB,IAAI,CAAC;AAErC;;;;;GAKG;AACH,eAAO,MAAM,gCAAgC,QAAgC,CAAC;AAE9E;;;GAGG;AACH,eAAO,MAAM,8BAA8B,QAAiB,CAAC;AAE7D;;;GAGG;AACH,eAAO,MAAM,0BAA0B,OAAQ,CAAC;AAEhD;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;GA8DG;AACH,wBAAgB,mBAAmB,CAAC,EAClC,UAAgC,EAChC,iBAA6B,EAC7B,sBAAyD,EACzD,oBAAqD,EACrD,iBAA8C,GAC/C,GAAE,0BAA+B,GAAG,aAAa,CAmDjD"}
|
@@ -1,4 +1,5 @@
|
|
1
|
-
import {
|
1
|
+
import { BrokenCircuitError, CircuitState, EventEmitter as CockatielEventEmitter, ConsecutiveBreaker, ExponentialBackoff, circuitBreaker, handleAll, handleWhen, retry, wrap } from "cockatiel";
|
2
|
+
export { CircuitState, BrokenCircuitError, handleAll, handleWhen };
|
2
3
|
/**
|
3
4
|
* The maximum number of times that a failing service should be re-run before
|
4
5
|
* giving up.
|
@@ -6,7 +7,9 @@ import { circuitBreaker, ConsecutiveBreaker, ExponentialBackoff, handleAll, retr
|
|
6
7
|
export const DEFAULT_MAX_RETRIES = 3;
|
7
8
|
/**
|
8
9
|
* The maximum number of times that the service is allowed to fail before
|
9
|
-
* pausing further retries.
|
10
|
+
* pausing further retries. This is set to a value such that if given a
|
11
|
+
* service that continually fails, the policy needs to be executed 3 times
|
12
|
+
* before further retries are paused.
|
10
13
|
*/
|
11
14
|
export const DEFAULT_MAX_CONSECUTIVE_FAILURES = (1 + DEFAULT_MAX_RETRIES) * 3;
|
12
15
|
/**
|
@@ -35,6 +38,12 @@ export const DEFAULT_DEGRADED_THRESHOLD = 5000;
|
|
35
38
|
* there for more.
|
36
39
|
*
|
37
40
|
* @param options - The options to this function.
|
41
|
+
* @param options.maxRetries - The maximum number of times that a failing
|
42
|
+
* service should be re-invoked before giving up. Defaults to 3.
|
43
|
+
* @param options.retryFilterPolicy - The policy used to control when the
|
44
|
+
* service should be retried based on either the result of the servce or an
|
45
|
+
* error that it throws. For instance, you could use this to retry only certain
|
46
|
+
* errors. See `handleWhen` and friends from Cockatiel for more.
|
38
47
|
* @param options.maxConsecutiveFailures - The maximum number of times that the
|
39
48
|
* service is allowed to fail before pausing further retries. Defaults to 12.
|
40
49
|
* @param options.circuitBreakDuration - The length of time (in milliseconds) to
|
@@ -43,14 +52,6 @@ export const DEFAULT_DEGRADED_THRESHOLD = 5000;
|
|
43
52
|
* @param options.degradedThreshold - The length of time (in milliseconds) that
|
44
53
|
* governs when the service is regarded as degraded (affecting when `onDegraded`
|
45
54
|
* is called). Defaults to 5 seconds.
|
46
|
-
* @param options.onBreak - A function which is called when the service fails
|
47
|
-
* too many times in a row (specifically, more than `maxConsecutiveFailures`).
|
48
|
-
* @param options.onDegraded - A function which is called when the service
|
49
|
-
* succeeds before `maxConsecutiveFailures` is reached, but takes more time than
|
50
|
-
* the `degradedThreshold` to run.
|
51
|
-
* @param options.onRetry - A function which will be called the moment the
|
52
|
-
* policy kicks off a timer to re-run the function passed to the policy. This is
|
53
|
-
* primarily useful in tests where we are mocking timers.
|
54
55
|
* @returns The service policy.
|
55
56
|
* @example
|
56
57
|
* This function is designed to be used in the context of a service class like
|
@@ -59,6 +60,10 @@ export const DEFAULT_DEGRADED_THRESHOLD = 5000;
|
|
59
60
|
* class Service {
|
60
61
|
* constructor() {
|
61
62
|
* this.#policy = createServicePolicy({
|
63
|
+
* maxRetries: 3,
|
64
|
+
* retryFilterPolicy: handleWhen((error) => {
|
65
|
+
* return error.message.includes('oops');
|
66
|
+
* }),
|
62
67
|
* maxConsecutiveFailures: 3,
|
63
68
|
* circuitBreakDuration: 5000,
|
64
69
|
* degradedThreshold: 2000,
|
@@ -80,21 +85,16 @@ export const DEFAULT_DEGRADED_THRESHOLD = 5000;
|
|
80
85
|
* }
|
81
86
|
* ```
|
82
87
|
*/
|
83
|
-
export function createServicePolicy({ maxConsecutiveFailures = DEFAULT_MAX_CONSECUTIVE_FAILURES, circuitBreakDuration = DEFAULT_CIRCUIT_BREAK_DURATION, degradedThreshold = DEFAULT_DEGRADED_THRESHOLD,
|
84
|
-
|
85
|
-
}, onDegraded = () => {
|
86
|
-
// do nothing
|
87
|
-
}, onRetry = () => {
|
88
|
-
// do nothing
|
89
|
-
}, } = {}) {
|
90
|
-
const retryPolicy = retry(handleAll, {
|
88
|
+
export function createServicePolicy({ maxRetries = DEFAULT_MAX_RETRIES, retryFilterPolicy = handleAll, maxConsecutiveFailures = DEFAULT_MAX_CONSECUTIVE_FAILURES, circuitBreakDuration = DEFAULT_CIRCUIT_BREAK_DURATION, degradedThreshold = DEFAULT_DEGRADED_THRESHOLD, } = {}) {
|
89
|
+
const retryPolicy = retry(retryFilterPolicy, {
|
91
90
|
// Note that although the option here is called "max attempts", it's really
|
92
91
|
// maximum number of *retries* (attempts past the initial attempt).
|
93
|
-
maxAttempts:
|
92
|
+
maxAttempts: maxRetries,
|
94
93
|
// Retries of the service will be executed following ever increasing delays,
|
95
94
|
// determined by a backoff formula.
|
96
95
|
backoff: new ExponentialBackoff(),
|
97
96
|
});
|
97
|
+
const onRetry = retryPolicy.onRetry.bind(retryPolicy);
|
98
98
|
const circuitBreakerPolicy = circuitBreaker(handleAll, {
|
99
99
|
// While the circuit is open, any additional invocations of the service
|
100
100
|
// passed to the policy (either via automatic retries or by manually
|
@@ -105,37 +105,30 @@ export function createServicePolicy({ maxConsecutiveFailures = DEFAULT_MAX_CONSE
|
|
105
105
|
halfOpenAfter: circuitBreakDuration,
|
106
106
|
breaker: new ConsecutiveBreaker(maxConsecutiveFailures),
|
107
107
|
});
|
108
|
-
|
109
|
-
|
110
|
-
// Combined with the retry policy, this can happen if:
|
111
|
-
// - `maxConsecutiveFailures` < the default max retries (3) and the policy is
|
112
|
-
// executed once
|
113
|
-
// - `maxConsecutiveFailures` >= the default max retries (3) but the policy is
|
114
|
-
// executed multiple times, enough for the total number of retries to exceed
|
115
|
-
// `maxConsecutiveFailures`
|
116
|
-
circuitBreakerPolicy.onBreak(onBreak);
|
117
|
-
// The `onRetryPolicy` callback will be called each time the service is
|
118
|
-
// invoked (including retries).
|
119
|
-
retryPolicy.onRetry(onRetry);
|
108
|
+
const onBreak = circuitBreakerPolicy.onBreak.bind(circuitBreakerPolicy);
|
109
|
+
const onDegradedEventEmitter = new CockatielEventEmitter();
|
120
110
|
retryPolicy.onGiveUp(() => {
|
121
111
|
if (circuitBreakerPolicy.state === CircuitState.Closed) {
|
122
|
-
|
123
|
-
// exceeded and the maximum number of consecutive failures has not been
|
124
|
-
// reached yet (whether the policy is called once or multiple times).
|
125
|
-
onDegraded();
|
112
|
+
onDegradedEventEmitter.emit();
|
126
113
|
}
|
127
114
|
});
|
128
115
|
retryPolicy.onSuccess(({ duration }) => {
|
129
116
|
if (circuitBreakerPolicy.state === CircuitState.Closed &&
|
130
117
|
duration > degradedThreshold) {
|
131
|
-
|
132
|
-
// throw, but the time it takes for the service to run exceeds the
|
133
|
-
// `degradedThreshold`.
|
134
|
-
onDegraded();
|
118
|
+
onDegradedEventEmitter.emit();
|
135
119
|
}
|
136
120
|
});
|
137
|
-
|
138
|
-
// the
|
139
|
-
|
121
|
+
const onDegraded = onDegradedEventEmitter.addListener;
|
122
|
+
// Every time the retry policy makes an attempt, it executes the circuit
|
123
|
+
// breaker policy, which executes the service.
|
124
|
+
const policy = wrap(retryPolicy, circuitBreakerPolicy);
|
125
|
+
return {
|
126
|
+
...policy,
|
127
|
+
circuitBreakerPolicy,
|
128
|
+
retryPolicy,
|
129
|
+
onBreak,
|
130
|
+
onDegraded,
|
131
|
+
onRetry,
|
132
|
+
};
|
140
133
|
}
|
141
134
|
//# sourceMappingURL=create-service-policy.mjs.map
|
@@ -1 +1 @@
|
|
1
|
-
{"version":3,"file":"create-service-policy.mjs","sourceRoot":"","sources":["../src/create-service-policy.ts"],"names":[],"mappings":"AAAA,OAAO,EACL,cAAc,EACd,kBAAkB,EAClB,kBAAkB,EAClB,SAAS,EACT,KAAK,EACL,IAAI,EACJ,YAAY,EACb,kBAAkB;AAKnB;;;GAGG;AACH,MAAM,CAAC,MAAM,mBAAmB,GAAG,CAAC,CAAC;AAErC;;;GAGG;AACH,MAAM,CAAC,MAAM,gCAAgC,GAAG,CAAC,CAAC,GAAG,mBAAmB,CAAC,GAAG,CAAC,CAAC;AAE9E;;;GAGG;AACH,MAAM,CAAC,MAAM,8BAA8B,GAAG,EAAE,GAAG,EAAE,GAAG,IAAI,CAAC;AAE7D;;;GAGG;AACH,MAAM,CAAC,MAAM,0BAA0B,GAAG,IAAK,CAAC;AAEhD;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;GA4DG;AACH,MAAM,UAAU,mBAAmB,CAAC,EAClC,sBAAsB,GAAG,gCAAgC,EACzD,oBAAoB,GAAG,8BAA8B,EACrD,iBAAiB,GAAG,0BAA0B,EAC9C,OAAO,GAAG,GAAG,EAAE;IACb,aAAa;AACf,CAAC,EACD,UAAU,GAAG,GAAG,EAAE;IAChB,aAAa;AACf,CAAC,EACD,OAAO,GAAG,GAAG,EAAE;IACb,aAAa;AACf,CAAC,MAQC,EAAE;IACJ,MAAM,WAAW,GAAG,KAAK,CAAC,SAAS,EAAE;QACnC,2EAA2E;QAC3E,mEAAmE;QACnE,WAAW,EAAE,mBAAmB;QAChC,4EAA4E;QAC5E,mCAAmC;QACnC,OAAO,EAAE,IAAI,kBAAkB,EAAE;KAClC,CAAC,CAAC;IAEH,MAAM,oBAAoB,GAAG,cAAc,CAAC,SAAS,EAAE;QACrD,uEAAuE;QACvE,oEAAoE;QACpE,wEAAwE;QACxE,4EAA4E;QAC5E,qEAAqE;QACrE,qDAAqD;QACrD,aAAa,EAAE,oBAAoB;QACnC,OAAO,EAAE,IAAI,kBAAkB,CAAC,sBAAsB,CAAC;KACxD,CAAC,CAAC;IAEH,2EAA2E;IAC3E,2EAA2E;IAC3E,sDAAsD;IACtD,6EAA6E;IAC7E,gBAAgB;IAChB,8EAA8E;IAC9E,8EAA8E;IAC9E,6BAA6B;IAC7B,oBAAoB,CAAC,OAAO,CAAC,OAAO,CAAC,CAAC;IAEtC,uEAAuE;IACvE,+BAA+B;IAC/B,WAAW,CAAC,OAAO,CAAC,OAAO,CAAC,CAAC;IAE7B,WAAW,CAAC,QAAQ,CAAC,GAAG,EAAE;QACxB,IAAI,oBAAoB,CAAC,KAAK,KAAK,YAAY,CAAC,MAAM,EAAE;YACtD,uEAAuE;YACvE,uEAAuE;YACvE,qEAAqE;YACrE,UAAU,EAAE,CAAC;SACd;IACH,CAAC,CAAC,CAAC;IACH,WAAW,CAAC,SAAS,CAAC,CAAC,EAAE,QAAQ,EAAE,EAAE,EAAE;QACrC,IACE,oBAAoB,CAAC,KAAK,KAAK,YAAY,CAAC,MAAM;YAClD,QAAQ,GAAG,iBAAiB,EAC5B;YACA,wEAAwE;YACxE,kEAAkE;YAClE,uBAAuB;YACvB,UAAU,EAAE,CAAC;SACd;IACH,CAAC,CAAC,CAAC;IAEH,4EAA4E;IAC5E,eAAe;IACf,OAAO,IAAI,CAAC,WAAW,EAAE,oBAAoB,CAAC,CAAC;AACjD,CAAC","sourcesContent":["import {\n circuitBreaker,\n ConsecutiveBreaker,\n ExponentialBackoff,\n handleAll,\n retry,\n wrap,\n CircuitState,\n} from 'cockatiel';\nimport type { IPolicy } from 'cockatiel';\n\nexport type { IPolicy as IServicePolicy };\n\n/**\n * The maximum number of times that a failing service should be re-run before\n * giving up.\n */\nexport const DEFAULT_MAX_RETRIES = 3;\n\n/**\n * The maximum number of times that the service is allowed to fail before\n * pausing further retries.\n */\nexport const DEFAULT_MAX_CONSECUTIVE_FAILURES = (1 + DEFAULT_MAX_RETRIES) * 3;\n\n/**\n * The default length of time (in milliseconds) to temporarily pause retries of\n * the service after enough consecutive failures.\n */\nexport const DEFAULT_CIRCUIT_BREAK_DURATION = 30 * 60 * 1000;\n\n/**\n * The default length of time (in milliseconds) that governs when the service is\n * regarded as degraded (affecting when `onDegraded` is called).\n */\nexport const DEFAULT_DEGRADED_THRESHOLD = 5_000;\n\n/**\n * Constructs an object exposing an `execute` method which, given a function —\n * hereafter called the \"service\" — will retry that service with ever increasing\n * delays until it succeeds. If the policy detects too many consecutive\n * failures, it will block further retries until a designated time period has\n * passed; this particular behavior is primarily designed for services that wrap\n * API calls so as not to make needless HTTP requests when the API is down and\n * to be able to recover when the API comes back up. In addition, hooks allow\n * for responding to certain events, one of which can be used to detect when an\n * HTTP request is performing slowly.\n *\n * Internally, this function makes use of the retry and circuit breaker policies\n * from the [Cockatiel](https://www.npmjs.com/package/cockatiel) library; see\n * there for more.\n *\n * @param options - The options to this function.\n * @param options.maxConsecutiveFailures - The maximum number of times that the\n * service is allowed to fail before pausing further retries. Defaults to 12.\n * @param options.circuitBreakDuration - The length of time (in milliseconds) to\n * pause retries of the action after the number of failures reaches\n * `maxConsecutiveFailures`.\n * @param options.degradedThreshold - The length of time (in milliseconds) that\n * governs when the service is regarded as degraded (affecting when `onDegraded`\n * is called). Defaults to 5 seconds.\n * @param options.onBreak - A function which is called when the service fails\n * too many times in a row (specifically, more than `maxConsecutiveFailures`).\n * @param options.onDegraded - A function which is called when the service\n * succeeds before `maxConsecutiveFailures` is reached, but takes more time than\n * the `degradedThreshold` to run.\n * @param options.onRetry - A function which will be called the moment the\n * policy kicks off a timer to re-run the function passed to the policy. This is\n * primarily useful in tests where we are mocking timers.\n * @returns The service policy.\n * @example\n * This function is designed to be used in the context of a service class like\n * this:\n * ``` ts\n * class Service {\n * constructor() {\n * this.#policy = createServicePolicy({\n * maxConsecutiveFailures: 3,\n * circuitBreakDuration: 5000,\n * degradedThreshold: 2000,\n * onBreak: () => {\n * console.log('Circuit broke');\n * },\n * onDegraded: () => {\n * console.log('Service is degraded');\n * },\n * });\n * }\n *\n * async fetch() {\n * return await this.#policy.execute(async () => {\n * const response = await fetch('https://some/url');\n * return await response.json();\n * });\n * }\n * }\n * ```\n */\nexport function createServicePolicy({\n maxConsecutiveFailures = DEFAULT_MAX_CONSECUTIVE_FAILURES,\n circuitBreakDuration = DEFAULT_CIRCUIT_BREAK_DURATION,\n degradedThreshold = DEFAULT_DEGRADED_THRESHOLD,\n onBreak = () => {\n // do nothing\n },\n onDegraded = () => {\n // do nothing\n },\n onRetry = () => {\n // do nothing\n },\n}: {\n maxConsecutiveFailures?: number;\n circuitBreakDuration?: number;\n degradedThreshold?: number;\n onBreak?: () => void;\n onDegraded?: () => void;\n onRetry?: () => void;\n} = {}): IPolicy {\n const retryPolicy = retry(handleAll, {\n // Note that although the option here is called \"max attempts\", it's really\n // maximum number of *retries* (attempts past the initial attempt).\n maxAttempts: DEFAULT_MAX_RETRIES,\n // Retries of the service will be executed following ever increasing delays,\n // determined by a backoff formula.\n backoff: new ExponentialBackoff(),\n });\n\n const circuitBreakerPolicy = circuitBreaker(handleAll, {\n // While the circuit is open, any additional invocations of the service\n // passed to the policy (either via automatic retries or by manually\n // executing the policy again) will result in a BrokenCircuitError. This\n // will remain the case until `circuitBreakDuration` passes, after which the\n // service will be allowed to run again. If the service succeeds, the\n // circuit will close, otherwise it will remain open.\n halfOpenAfter: circuitBreakDuration,\n breaker: new ConsecutiveBreaker(maxConsecutiveFailures),\n });\n\n // The `onBreak` callback will be called if the service consistently throws\n // for as many times as exceeds the maximum consecutive number of failures.\n // Combined with the retry policy, this can happen if:\n // - `maxConsecutiveFailures` < the default max retries (3) and the policy is\n // executed once\n // - `maxConsecutiveFailures` >= the default max retries (3) but the policy is\n // executed multiple times, enough for the total number of retries to exceed\n // `maxConsecutiveFailures`\n circuitBreakerPolicy.onBreak(onBreak);\n\n // The `onRetryPolicy` callback will be called each time the service is\n // invoked (including retries).\n retryPolicy.onRetry(onRetry);\n\n retryPolicy.onGiveUp(() => {\n if (circuitBreakerPolicy.state === CircuitState.Closed) {\n // The `onDegraded` callback will be called if the number of retries is\n // exceeded and the maximum number of consecutive failures has not been\n // reached yet (whether the policy is called once or multiple times).\n onDegraded();\n }\n });\n retryPolicy.onSuccess(({ duration }) => {\n if (\n circuitBreakerPolicy.state === CircuitState.Closed &&\n duration > degradedThreshold\n ) {\n // The `onDegraded` callback will also be called if the service does not\n // throw, but the time it takes for the service to run exceeds the\n // `degradedThreshold`.\n onDegraded();\n }\n });\n\n // The retry policy really retries the circuit breaker policy, which invokes\n // the service.\n return wrap(retryPolicy, circuitBreakerPolicy);\n}\n"]}
|
1
|
+
{"version":3,"file":"create-service-policy.mjs","sourceRoot":"","sources":["../src/create-service-policy.ts"],"names":[],"mappings":"AAAA,OAAO,EACL,kBAAkB,EAClB,YAAY,EACZ,YAAY,IAAI,qBAAqB,EACrC,kBAAkB,EAClB,kBAAkB,EAClB,cAAc,EACd,SAAS,EACT,UAAU,EACV,KAAK,EACL,IAAI,EACL,kBAAkB;AASnB,OAAO,EAAE,YAAY,EAAE,kBAAkB,EAAE,SAAS,EAAE,UAAU,EAAE,CAAC;AAuEnE;;;GAGG;AACH,MAAM,CAAC,MAAM,mBAAmB,GAAG,CAAC,CAAC;AAErC;;;;;GAKG;AACH,MAAM,CAAC,MAAM,gCAAgC,GAAG,CAAC,CAAC,GAAG,mBAAmB,CAAC,GAAG,CAAC,CAAC;AAE9E;;;GAGG;AACH,MAAM,CAAC,MAAM,8BAA8B,GAAG,EAAE,GAAG,EAAE,GAAG,IAAI,CAAC;AAE7D;;;GAGG;AACH,MAAM,CAAC,MAAM,0BAA0B,GAAG,IAAK,CAAC;AAEhD;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;GA8DG;AACH,MAAM,UAAU,mBAAmB,CAAC,EAClC,UAAU,GAAG,mBAAmB,EAChC,iBAAiB,GAAG,SAAS,EAC7B,sBAAsB,GAAG,gCAAgC,EACzD,oBAAoB,GAAG,8BAA8B,EACrD,iBAAiB,GAAG,0BAA0B,MAChB,EAAE;IAChC,MAAM,WAAW,GAAG,KAAK,CAAC,iBAAiB,EAAE;QAC3C,2EAA2E;QAC3E,mEAAmE;QACnE,WAAW,EAAE,UAAU;QACvB,4EAA4E;QAC5E,mCAAmC;QACnC,OAAO,EAAE,IAAI,kBAAkB,EAAE;KAClC,CAAC,CAAC;IACH,MAAM,OAAO,GAAG,WAAW,CAAC,OAAO,CAAC,IAAI,CAAC,WAAW,CAAC,CAAC;IAEtD,MAAM,oBAAoB,GAAG,cAAc,CAAC,SAAS,EAAE;QACrD,uEAAuE;QACvE,oEAAoE;QACpE,wEAAwE;QACxE,4EAA4E;QAC5E,qEAAqE;QACrE,qDAAqD;QACrD,aAAa,EAAE,oBAAoB;QACnC,OAAO,EAAE,IAAI,kBAAkB,CAAC,sBAAsB,CAAC;KACxD,CAAC,CAAC;IACH,MAAM,OAAO,GAAG,oBAAoB,CAAC,OAAO,CAAC,IAAI,CAAC,oBAAoB,CAAC,CAAC;IAExE,MAAM,sBAAsB,GAAG,IAAI,qBAAqB,EAAQ,CAAC;IACjE,WAAW,CAAC,QAAQ,CAAC,GAAG,EAAE;QACxB,IAAI,oBAAoB,CAAC,KAAK,KAAK,YAAY,CAAC,MAAM,EAAE;YACtD,sBAAsB,CAAC,IAAI,EAAE,CAAC;SAC/B;IACH,CAAC,CAAC,CAAC;IACH,WAAW,CAAC,SAAS,CAAC,CAAC,EAAE,QAAQ,EAAE,EAAE,EAAE;QACrC,IACE,oBAAoB,CAAC,KAAK,KAAK,YAAY,CAAC,MAAM;YAClD,QAAQ,GAAG,iBAAiB,EAC5B;YACA,sBAAsB,CAAC,IAAI,EAAE,CAAC;SAC/B;IACH,CAAC,CAAC,CAAC;IACH,MAAM,UAAU,GAAG,sBAAsB,CAAC,WAAW,CAAC;IAEtD,wEAAwE;IACxE,8CAA8C;IAC9C,MAAM,MAAM,GAAG,IAAI,CAAC,WAAW,EAAE,oBAAoB,CAAC,CAAC;IAEvD,OAAO;QACL,GAAG,MAAM;QACT,oBAAoB;QACpB,WAAW;QACX,OAAO;QACP,UAAU;QACV,OAAO;KACR,CAAC;AACJ,CAAC","sourcesContent":["import {\n BrokenCircuitError,\n CircuitState,\n EventEmitter as CockatielEventEmitter,\n ConsecutiveBreaker,\n ExponentialBackoff,\n circuitBreaker,\n handleAll,\n handleWhen,\n retry,\n wrap,\n} from 'cockatiel';\nimport type {\n CircuitBreakerPolicy,\n Event as CockatielEvent,\n IPolicy,\n Policy,\n RetryPolicy,\n} from 'cockatiel';\n\nexport { CircuitState, BrokenCircuitError, handleAll, handleWhen };\n\nexport type { CockatielEvent };\n\n/**\n * The options for `createServicePolicy`.\n */\nexport type CreateServicePolicyOptions = {\n /**\n * The length of time (in milliseconds) to pause retries of the action after\n * the number of failures reaches `maxConsecutiveFailures`.\n */\n circuitBreakDuration?: number;\n /**\n * The length of time (in milliseconds) that governs when the service is\n * regarded as degraded (affecting when `onDegraded` is called).\n */\n degradedThreshold?: number;\n /**\n * The maximum number of times that the service is allowed to fail before\n * pausing further retries.\n */\n maxConsecutiveFailures?: number;\n /**\n * The maximum number of times that a failing service should be re-invoked\n * before giving up.\n */\n maxRetries?: number;\n /**\n * The policy used to control when the service should be retried based on\n * either the result of the service or an error that it throws. For instance,\n * you could use this to retry only certain errors. See `handleWhen` and\n * friends from Cockatiel for more.\n */\n retryFilterPolicy?: Policy;\n};\n\n/**\n * The service policy object.\n */\nexport type ServicePolicy = IPolicy & {\n /**\n * The Cockatiel circuit breaker policy that the service policy uses\n * internally.\n */\n circuitBreakerPolicy: CircuitBreakerPolicy;\n /**\n * The Cockatiel retry policy that the service policy uses internally.\n */\n retryPolicy: RetryPolicy;\n /**\n * A function which is called when the number of times that the service fails\n * in a row meets the set maximum number of consecutive failures.\n */\n onBreak: CircuitBreakerPolicy['onBreak'];\n /**\n * A function which is called in two circumstances: 1) when the service\n * succeeds before the maximum number of consecutive failures is reached, but\n * takes more time than the `degradedThreshold` to run, or 2) if the service\n * never succeeds before the retry policy gives up and before the maximum\n * number of consecutive failures has been reached.\n */\n onDegraded: CockatielEvent<void>;\n /**\n * A function which will be called by the retry policy each time the service\n * fails and the policy kicks off a timer to re-run the service. This is\n * primarily useful in tests where we are mocking timers.\n */\n onRetry: RetryPolicy['onRetry'];\n};\n\n/**\n * The maximum number of times that a failing service should be re-run before\n * giving up.\n */\nexport const DEFAULT_MAX_RETRIES = 3;\n\n/**\n * The maximum number of times that the service is allowed to fail before\n * pausing further retries. This is set to a value such that if given a\n * service that continually fails, the policy needs to be executed 3 times\n * before further retries are paused.\n */\nexport const DEFAULT_MAX_CONSECUTIVE_FAILURES = (1 + DEFAULT_MAX_RETRIES) * 3;\n\n/**\n * The default length of time (in milliseconds) to temporarily pause retries of\n * the service after enough consecutive failures.\n */\nexport const DEFAULT_CIRCUIT_BREAK_DURATION = 30 * 60 * 1000;\n\n/**\n * The default length of time (in milliseconds) that governs when the service is\n * regarded as degraded (affecting when `onDegraded` is called).\n */\nexport const DEFAULT_DEGRADED_THRESHOLD = 5_000;\n\n/**\n * Constructs an object exposing an `execute` method which, given a function —\n * hereafter called the \"service\" — will retry that service with ever increasing\n * delays until it succeeds. If the policy detects too many consecutive\n * failures, it will block further retries until a designated time period has\n * passed; this particular behavior is primarily designed for services that wrap\n * API calls so as not to make needless HTTP requests when the API is down and\n * to be able to recover when the API comes back up. In addition, hooks allow\n * for responding to certain events, one of which can be used to detect when an\n * HTTP request is performing slowly.\n *\n * Internally, this function makes use of the retry and circuit breaker policies\n * from the [Cockatiel](https://www.npmjs.com/package/cockatiel) library; see\n * there for more.\n *\n * @param options - The options to this function.\n * @param options.maxRetries - The maximum number of times that a failing\n * service should be re-invoked before giving up. Defaults to 3.\n * @param options.retryFilterPolicy - The policy used to control when the\n * service should be retried based on either the result of the servce or an\n * error that it throws. For instance, you could use this to retry only certain\n * errors. See `handleWhen` and friends from Cockatiel for more.\n * @param options.maxConsecutiveFailures - The maximum number of times that the\n * service is allowed to fail before pausing further retries. Defaults to 12.\n * @param options.circuitBreakDuration - The length of time (in milliseconds) to\n * pause retries of the action after the number of failures reaches\n * `maxConsecutiveFailures`.\n * @param options.degradedThreshold - The length of time (in milliseconds) that\n * governs when the service is regarded as degraded (affecting when `onDegraded`\n * is called). Defaults to 5 seconds.\n * @returns The service policy.\n * @example\n * This function is designed to be used in the context of a service class like\n * this:\n * ``` ts\n * class Service {\n * constructor() {\n * this.#policy = createServicePolicy({\n * maxRetries: 3,\n * retryFilterPolicy: handleWhen((error) => {\n * return error.message.includes('oops');\n * }),\n * maxConsecutiveFailures: 3,\n * circuitBreakDuration: 5000,\n * degradedThreshold: 2000,\n * onBreak: () => {\n * console.log('Circuit broke');\n * },\n * onDegraded: () => {\n * console.log('Service is degraded');\n * },\n * });\n * }\n *\n * async fetch() {\n * return await this.#policy.execute(async () => {\n * const response = await fetch('https://some/url');\n * return await response.json();\n * });\n * }\n * }\n * ```\n */\nexport function createServicePolicy({\n maxRetries = DEFAULT_MAX_RETRIES,\n retryFilterPolicy = handleAll,\n maxConsecutiveFailures = DEFAULT_MAX_CONSECUTIVE_FAILURES,\n circuitBreakDuration = DEFAULT_CIRCUIT_BREAK_DURATION,\n degradedThreshold = DEFAULT_DEGRADED_THRESHOLD,\n}: CreateServicePolicyOptions = {}): ServicePolicy {\n const retryPolicy = retry(retryFilterPolicy, {\n // Note that although the option here is called \"max attempts\", it's really\n // maximum number of *retries* (attempts past the initial attempt).\n maxAttempts: maxRetries,\n // Retries of the service will be executed following ever increasing delays,\n // determined by a backoff formula.\n backoff: new ExponentialBackoff(),\n });\n const onRetry = retryPolicy.onRetry.bind(retryPolicy);\n\n const circuitBreakerPolicy = circuitBreaker(handleAll, {\n // While the circuit is open, any additional invocations of the service\n // passed to the policy (either via automatic retries or by manually\n // executing the policy again) will result in a BrokenCircuitError. This\n // will remain the case until `circuitBreakDuration` passes, after which the\n // service will be allowed to run again. If the service succeeds, the\n // circuit will close, otherwise it will remain open.\n halfOpenAfter: circuitBreakDuration,\n breaker: new ConsecutiveBreaker(maxConsecutiveFailures),\n });\n const onBreak = circuitBreakerPolicy.onBreak.bind(circuitBreakerPolicy);\n\n const onDegradedEventEmitter = new CockatielEventEmitter<void>();\n retryPolicy.onGiveUp(() => {\n if (circuitBreakerPolicy.state === CircuitState.Closed) {\n onDegradedEventEmitter.emit();\n }\n });\n retryPolicy.onSuccess(({ duration }) => {\n if (\n circuitBreakerPolicy.state === CircuitState.Closed &&\n duration > degradedThreshold\n ) {\n onDegradedEventEmitter.emit();\n }\n });\n const onDegraded = onDegradedEventEmitter.addListener;\n\n // Every time the retry policy makes an attempt, it executes the circuit\n // breaker policy, which executes the service.\n const policy = wrap(retryPolicy, circuitBreakerPolicy);\n\n return {\n ...policy,\n circuitBreakerPolicy,\n retryPolicy,\n onBreak,\n onDegraded,\n onRetry,\n };\n}\n"]}
|
package/dist/index.cjs
CHANGED
@@ -14,9 +14,17 @@ var __exportStar = (this && this.__exportStar) || function(m, exports) {
|
|
14
14
|
for (var p in m) if (p !== "default" && !Object.prototype.hasOwnProperty.call(exports, p)) __createBinding(exports, m, p);
|
15
15
|
};
|
16
16
|
Object.defineProperty(exports, "__esModule", { value: true });
|
17
|
-
exports.isEqualCaseInsensitive = exports.weiHexToGweiDec = exports.toHex = exports.toChecksumHexAddress = exports.timeoutFetch = exports.successfulFetch = exports.safelyExecuteWithTimeout = exports.safelyExecute = exports.query = exports.normalizeEnsName = exports.isValidHexAddress = exports.isValidJson = exports.isSmartContractCode = exports.isSafeDynamicKey = exports.isSafeChainId = exports.isPlainObject = exports.isNonEmptyArray = exports.hexToText = exports.hexToBN = exports.handleFetch = exports.gweiDecToWEIBN = exports.getBuyURL = exports.fromHex = exports.fractionBN = exports.fetchWithErrorHandling = exports.convertHexToDecimal = exports.BNToHex = exports.createServicePolicy = void 0;
|
17
|
+
exports.isEqualCaseInsensitive = exports.weiHexToGweiDec = exports.toHex = exports.toChecksumHexAddress = exports.timeoutFetch = exports.successfulFetch = exports.safelyExecuteWithTimeout = exports.safelyExecute = exports.query = exports.normalizeEnsName = exports.isValidHexAddress = exports.isValidJson = exports.isSmartContractCode = exports.isSafeDynamicKey = exports.isSafeChainId = exports.isPlainObject = exports.isNonEmptyArray = exports.hexToText = exports.hexToBN = exports.handleFetch = exports.gweiDecToWEIBN = exports.getBuyURL = exports.fromHex = exports.fractionBN = exports.fetchWithErrorHandling = exports.convertHexToDecimal = exports.BNToHex = exports.handleWhen = exports.handleAll = exports.createServicePolicy = exports.DEFAULT_MAX_RETRIES = exports.DEFAULT_MAX_CONSECUTIVE_FAILURES = exports.DEFAULT_DEGRADED_THRESHOLD = exports.DEFAULT_CIRCUIT_BREAK_DURATION = exports.CircuitState = exports.BrokenCircuitError = void 0;
|
18
18
|
var create_service_policy_1 = require("./create-service-policy.cjs");
|
19
|
+
Object.defineProperty(exports, "BrokenCircuitError", { enumerable: true, get: function () { return create_service_policy_1.BrokenCircuitError; } });
|
20
|
+
Object.defineProperty(exports, "CircuitState", { enumerable: true, get: function () { return create_service_policy_1.CircuitState; } });
|
21
|
+
Object.defineProperty(exports, "DEFAULT_CIRCUIT_BREAK_DURATION", { enumerable: true, get: function () { return create_service_policy_1.DEFAULT_CIRCUIT_BREAK_DURATION; } });
|
22
|
+
Object.defineProperty(exports, "DEFAULT_DEGRADED_THRESHOLD", { enumerable: true, get: function () { return create_service_policy_1.DEFAULT_DEGRADED_THRESHOLD; } });
|
23
|
+
Object.defineProperty(exports, "DEFAULT_MAX_CONSECUTIVE_FAILURES", { enumerable: true, get: function () { return create_service_policy_1.DEFAULT_MAX_CONSECUTIVE_FAILURES; } });
|
24
|
+
Object.defineProperty(exports, "DEFAULT_MAX_RETRIES", { enumerable: true, get: function () { return create_service_policy_1.DEFAULT_MAX_RETRIES; } });
|
19
25
|
Object.defineProperty(exports, "createServicePolicy", { enumerable: true, get: function () { return create_service_policy_1.createServicePolicy; } });
|
26
|
+
Object.defineProperty(exports, "handleAll", { enumerable: true, get: function () { return create_service_policy_1.handleAll; } });
|
27
|
+
Object.defineProperty(exports, "handleWhen", { enumerable: true, get: function () { return create_service_policy_1.handleWhen; } });
|
20
28
|
__exportStar(require("./constants.cjs"), exports);
|
21
29
|
var util_1 = require("./util.cjs");
|
22
30
|
Object.defineProperty(exports, "BNToHex", { enumerable: true, get: function () { return util_1.BNToHex; } });
|
package/dist/index.cjs.map
CHANGED
@@ -1 +1 @@
|
|
1
|
-
{"version":3,"file":"index.cjs","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":";;;;;;;;;;;;;;;;;AAAA,
|
1
|
+
{"version":3,"file":"index.cjs","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":";;;;;;;;;;;;;;;;;AAAA,qEAUiC;AAT/B,2HAAA,kBAAkB,OAAA;AAClB,qHAAA,YAAY,OAAA;AACZ,uIAAA,8BAA8B,OAAA;AAC9B,mIAAA,0BAA0B,OAAA;AAC1B,yIAAA,gCAAgC,OAAA;AAChC,4HAAA,mBAAmB,OAAA;AACnB,4HAAA,mBAAmB,OAAA;AACnB,kHAAA,SAAS,OAAA;AACT,mHAAA,UAAU,OAAA;AAOZ,kDAA4B;AAE5B,mCA4BgB;AA3Bd,+FAAA,OAAO,OAAA;AACP,2GAAA,mBAAmB,OAAA;AACnB,8GAAA,sBAAsB,OAAA;AACtB,kGAAA,UAAU,OAAA;AACV,+FAAA,OAAO,OAAA;AACP,iGAAA,SAAS,OAAA;AACT,sGAAA,cAAc,OAAA;AACd,mGAAA,WAAW,OAAA;AACX,+FAAA,OAAO,OAAA;AACP,iGAAA,SAAS,OAAA;AACT,uGAAA,eAAe,OAAA;AACf,qGAAA,aAAa,OAAA;AACb,qGAAA,aAAa,OAAA;AACb,wGAAA,gBAAgB,OAAA;AAChB,2GAAA,mBAAmB,OAAA;AACnB,mGAAA,WAAW,OAAA;AACX,yGAAA,iBAAiB,OAAA;AACjB,wGAAA,gBAAgB,OAAA;AAChB,6FAAA,KAAK,OAAA;AACL,qGAAA,aAAa,OAAA;AACb,gHAAA,wBAAwB,OAAA;AACxB,uGAAA,eAAe,OAAA;AACf,oGAAA,YAAY,OAAA;AACZ,4GAAA,oBAAoB,OAAA;AACpB,6FAAA,KAAK,OAAA;AACL,uGAAA,eAAe,OAAA;AACf,8GAAA,sBAAsB,OAAA;AAExB,8CAAwB;AACxB,6CAAuB","sourcesContent":["export {\n BrokenCircuitError,\n CircuitState,\n DEFAULT_CIRCUIT_BREAK_DURATION,\n DEFAULT_DEGRADED_THRESHOLD,\n DEFAULT_MAX_CONSECUTIVE_FAILURES,\n DEFAULT_MAX_RETRIES,\n createServicePolicy,\n handleAll,\n handleWhen,\n} from './create-service-policy';\nexport type {\n CockatielEvent,\n CreateServicePolicyOptions,\n ServicePolicy,\n} from './create-service-policy';\nexport * from './constants';\nexport type { NonEmptyArray } from './util';\nexport {\n BNToHex,\n convertHexToDecimal,\n fetchWithErrorHandling,\n fractionBN,\n fromHex,\n getBuyURL,\n gweiDecToWEIBN,\n handleFetch,\n hexToBN,\n hexToText,\n isNonEmptyArray,\n isPlainObject,\n isSafeChainId,\n isSafeDynamicKey,\n isSmartContractCode,\n isValidJson,\n isValidHexAddress,\n normalizeEnsName,\n query,\n safelyExecute,\n safelyExecuteWithTimeout,\n successfulFetch,\n timeoutFetch,\n toChecksumHexAddress,\n toHex,\n weiHexToGweiDec,\n isEqualCaseInsensitive,\n} from './util';\nexport * from './types';\nexport * from './siwe';\n"]}
|
package/dist/index.d.cts
CHANGED
@@ -1,4 +1,5 @@
|
|
1
|
-
export { createServicePolicy } from "./create-service-policy.cjs";
|
1
|
+
export { BrokenCircuitError, CircuitState, DEFAULT_CIRCUIT_BREAK_DURATION, DEFAULT_DEGRADED_THRESHOLD, DEFAULT_MAX_CONSECUTIVE_FAILURES, DEFAULT_MAX_RETRIES, createServicePolicy, handleAll, handleWhen, } from "./create-service-policy.cjs";
|
2
|
+
export type { CockatielEvent, CreateServicePolicyOptions, ServicePolicy, } from "./create-service-policy.cjs";
|
2
3
|
export * from "./constants.cjs";
|
3
4
|
export type { NonEmptyArray } from "./util.cjs";
|
4
5
|
export { BNToHex, convertHexToDecimal, fetchWithErrorHandling, fractionBN, fromHex, getBuyURL, gweiDecToWEIBN, handleFetch, hexToBN, hexToText, isNonEmptyArray, isPlainObject, isSafeChainId, isSafeDynamicKey, isSmartContractCode, isValidJson, isValidHexAddress, normalizeEnsName, query, safelyExecute, safelyExecuteWithTimeout, successfulFetch, timeoutFetch, toChecksumHexAddress, toHex, weiHexToGweiDec, isEqualCaseInsensitive, } from "./util.cjs";
|
package/dist/index.d.cts.map
CHANGED
@@ -1 +1 @@
|
|
1
|
-
{"version":3,"file":"index.d.cts","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":"AAAA,OAAO,
|
1
|
+
{"version":3,"file":"index.d.cts","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":"AAAA,OAAO,EACL,kBAAkB,EAClB,YAAY,EACZ,8BAA8B,EAC9B,0BAA0B,EAC1B,gCAAgC,EAChC,mBAAmB,EACnB,mBAAmB,EACnB,SAAS,EACT,UAAU,GACX,oCAAgC;AACjC,YAAY,EACV,cAAc,EACd,0BAA0B,EAC1B,aAAa,GACd,oCAAgC;AACjC,gCAA4B;AAC5B,YAAY,EAAE,aAAa,EAAE,mBAAe;AAC5C,OAAO,EACL,OAAO,EACP,mBAAmB,EACnB,sBAAsB,EACtB,UAAU,EACV,OAAO,EACP,SAAS,EACT,cAAc,EACd,WAAW,EACX,OAAO,EACP,SAAS,EACT,eAAe,EACf,aAAa,EACb,aAAa,EACb,gBAAgB,EAChB,mBAAmB,EACnB,WAAW,EACX,iBAAiB,EACjB,gBAAgB,EAChB,KAAK,EACL,aAAa,EACb,wBAAwB,EACxB,eAAe,EACf,YAAY,EACZ,oBAAoB,EACpB,KAAK,EACL,eAAe,EACf,sBAAsB,GACvB,mBAAe;AAChB,4BAAwB;AACxB,2BAAuB"}
|
package/dist/index.d.mts
CHANGED
@@ -1,4 +1,5 @@
|
|
1
|
-
export { createServicePolicy } from "./create-service-policy.mjs";
|
1
|
+
export { BrokenCircuitError, CircuitState, DEFAULT_CIRCUIT_BREAK_DURATION, DEFAULT_DEGRADED_THRESHOLD, DEFAULT_MAX_CONSECUTIVE_FAILURES, DEFAULT_MAX_RETRIES, createServicePolicy, handleAll, handleWhen, } from "./create-service-policy.mjs";
|
2
|
+
export type { CockatielEvent, CreateServicePolicyOptions, ServicePolicy, } from "./create-service-policy.mjs";
|
2
3
|
export * from "./constants.mjs";
|
3
4
|
export type { NonEmptyArray } from "./util.mjs";
|
4
5
|
export { BNToHex, convertHexToDecimal, fetchWithErrorHandling, fractionBN, fromHex, getBuyURL, gweiDecToWEIBN, handleFetch, hexToBN, hexToText, isNonEmptyArray, isPlainObject, isSafeChainId, isSafeDynamicKey, isSmartContractCode, isValidJson, isValidHexAddress, normalizeEnsName, query, safelyExecute, safelyExecuteWithTimeout, successfulFetch, timeoutFetch, toChecksumHexAddress, toHex, weiHexToGweiDec, isEqualCaseInsensitive, } from "./util.mjs";
|
package/dist/index.d.mts.map
CHANGED
@@ -1 +1 @@
|
|
1
|
-
{"version":3,"file":"index.d.mts","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":"AAAA,OAAO,
|
1
|
+
{"version":3,"file":"index.d.mts","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":"AAAA,OAAO,EACL,kBAAkB,EAClB,YAAY,EACZ,8BAA8B,EAC9B,0BAA0B,EAC1B,gCAAgC,EAChC,mBAAmB,EACnB,mBAAmB,EACnB,SAAS,EACT,UAAU,GACX,oCAAgC;AACjC,YAAY,EACV,cAAc,EACd,0BAA0B,EAC1B,aAAa,GACd,oCAAgC;AACjC,gCAA4B;AAC5B,YAAY,EAAE,aAAa,EAAE,mBAAe;AAC5C,OAAO,EACL,OAAO,EACP,mBAAmB,EACnB,sBAAsB,EACtB,UAAU,EACV,OAAO,EACP,SAAS,EACT,cAAc,EACd,WAAW,EACX,OAAO,EACP,SAAS,EACT,eAAe,EACf,aAAa,EACb,aAAa,EACb,gBAAgB,EAChB,mBAAmB,EACnB,WAAW,EACX,iBAAiB,EACjB,gBAAgB,EAChB,KAAK,EACL,aAAa,EACb,wBAAwB,EACxB,eAAe,EACf,YAAY,EACZ,oBAAoB,EACpB,KAAK,EACL,eAAe,EACf,sBAAsB,GACvB,mBAAe;AAChB,4BAAwB;AACxB,2BAAuB"}
|
package/dist/index.mjs
CHANGED
@@ -1,4 +1,4 @@
|
|
1
|
-
export { createServicePolicy } from "./create-service-policy.mjs";
|
1
|
+
export { BrokenCircuitError, CircuitState, DEFAULT_CIRCUIT_BREAK_DURATION, DEFAULT_DEGRADED_THRESHOLD, DEFAULT_MAX_CONSECUTIVE_FAILURES, DEFAULT_MAX_RETRIES, createServicePolicy, handleAll, handleWhen } from "./create-service-policy.mjs";
|
2
2
|
export * from "./constants.mjs";
|
3
3
|
export { BNToHex, convertHexToDecimal, fetchWithErrorHandling, fractionBN, fromHex, getBuyURL, gweiDecToWEIBN, handleFetch, hexToBN, hexToText, isNonEmptyArray, isPlainObject, isSafeChainId, isSafeDynamicKey, isSmartContractCode, isValidJson, isValidHexAddress, normalizeEnsName, query, safelyExecute, safelyExecuteWithTimeout, successfulFetch, timeoutFetch, toChecksumHexAddress, toHex, weiHexToGweiDec, isEqualCaseInsensitive } from "./util.mjs";
|
4
4
|
export * from "./types.mjs";
|
package/dist/index.mjs.map
CHANGED
@@ -1 +1 @@
|
|
1
|
-
{"version":3,"file":"index.mjs","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":"AAAA,OAAO,
|
1
|
+
{"version":3,"file":"index.mjs","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":"AAAA,OAAO,EACL,kBAAkB,EAClB,YAAY,EACZ,8BAA8B,EAC9B,0BAA0B,EAC1B,gCAAgC,EAChC,mBAAmB,EACnB,mBAAmB,EACnB,SAAS,EACT,UAAU,EACX,oCAAgC;AAMjC,gCAA4B;AAE5B,OAAO,EACL,OAAO,EACP,mBAAmB,EACnB,sBAAsB,EACtB,UAAU,EACV,OAAO,EACP,SAAS,EACT,cAAc,EACd,WAAW,EACX,OAAO,EACP,SAAS,EACT,eAAe,EACf,aAAa,EACb,aAAa,EACb,gBAAgB,EAChB,mBAAmB,EACnB,WAAW,EACX,iBAAiB,EACjB,gBAAgB,EAChB,KAAK,EACL,aAAa,EACb,wBAAwB,EACxB,eAAe,EACf,YAAY,EACZ,oBAAoB,EACpB,KAAK,EACL,eAAe,EACf,sBAAsB,EACvB,mBAAe;AAChB,4BAAwB;AACxB,2BAAuB","sourcesContent":["export {\n BrokenCircuitError,\n CircuitState,\n DEFAULT_CIRCUIT_BREAK_DURATION,\n DEFAULT_DEGRADED_THRESHOLD,\n DEFAULT_MAX_CONSECUTIVE_FAILURES,\n DEFAULT_MAX_RETRIES,\n createServicePolicy,\n handleAll,\n handleWhen,\n} from './create-service-policy';\nexport type {\n CockatielEvent,\n CreateServicePolicyOptions,\n ServicePolicy,\n} from './create-service-policy';\nexport * from './constants';\nexport type { NonEmptyArray } from './util';\nexport {\n BNToHex,\n convertHexToDecimal,\n fetchWithErrorHandling,\n fractionBN,\n fromHex,\n getBuyURL,\n gweiDecToWEIBN,\n handleFetch,\n hexToBN,\n hexToText,\n isNonEmptyArray,\n isPlainObject,\n isSafeChainId,\n isSafeDynamicKey,\n isSmartContractCode,\n isValidJson,\n isValidHexAddress,\n normalizeEnsName,\n query,\n safelyExecute,\n safelyExecuteWithTimeout,\n successfulFetch,\n timeoutFetch,\n toChecksumHexAddress,\n toHex,\n weiHexToGweiDec,\n isEqualCaseInsensitive,\n} from './util';\nexport * from './types';\nexport * from './siwe';\n"]}
|
package/package.json
CHANGED
@@ -1,6 +1,6 @@
|
|
1
1
|
{
|
2
2
|
"name": "@metamask-previews/controller-utils",
|
3
|
-
"version": "11.
|
3
|
+
"version": "11.5.0-preview-0d0d258d",
|
4
4
|
"description": "Data and convenience functions shared by multiple packages",
|
5
5
|
"keywords": [
|
6
6
|
"MetaMask",
|
@@ -50,7 +50,7 @@
|
|
50
50
|
"@ethereumjs/util": "^8.1.0",
|
51
51
|
"@metamask/eth-query": "^4.0.0",
|
52
52
|
"@metamask/ethjs-unit": "^0.3.0",
|
53
|
-
"@metamask/utils": "^11.0
|
53
|
+
"@metamask/utils": "^11.1.0",
|
54
54
|
"@spruceid/siwe-parser": "2.1.0",
|
55
55
|
"@types/bn.js": "^5.1.5",
|
56
56
|
"bignumber.js": "^9.1.2",
|