Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Update createServicePolicy to expose sub-policies; add re-exports from cockatiel #5192

Merged
merged 2 commits into from
Jan 24, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion packages/controller-utils/CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -9,10 +9,11 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0

### Added

- Add utility function for reducing boilerplate for service classes ([#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))
- Add utility function for reducing boilerplate for service classes ([#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))
- Add function `createServicePolicy`
- Add constants `DEFAULT_CIRCUIT_BREAK_DURATION`, `DEFAULT_DEGRADED_THRESHOLD`, `DEFAULT_MAX_CONSECUTIVE_FAILURES`, and `DEFAULT_MAX_RETRIES`
- Add types `ServicePolicy` and `CreateServicePolicyOptions`
- Re-export `BrokenCircuitError`, `CircuitState`, `handleAll`, and `handleWhen` from `cockatiel`

## [11.4.5]

Expand Down
2 changes: 1 addition & 1 deletion packages/controller-utils/jest.config.js
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ module.exports = merge(baseConfig, {
coverageThreshold: {
global: {
branches: 78.12,
functions: 84.61,
functions: 80.7,
lines: 87.3,
statements: 86.5,
},
Expand Down
19 changes: 17 additions & 2 deletions packages/controller-utils/src/create-service-policy.ts
Original file line number Diff line number Diff line change
@@ -1,11 +1,13 @@
import {
circuitBreaker,
BrokenCircuitError,
CircuitState,
ConsecutiveBreaker,
ExponentialBackoff,
circuitBreaker,
handleAll,
handleWhen,
retry,
wrap,
CircuitState,
} from 'cockatiel';
import type {
CircuitBreakerPolicy,
Expand All @@ -14,6 +16,8 @@ import type {
RetryPolicy,
} from 'cockatiel';

export { CircuitState, BrokenCircuitError, handleAll, handleWhen };

/**
* The options for `createServicePolicy`.
*/
Expand Down Expand Up @@ -51,6 +55,15 @@ export type CreateServicePolicyOptions = {
* The service policy object.
*/
export type ServicePolicy = IPolicy & {
/**
* The Cockatiel circuit breaker policy that the service policy uses
* internally.
*/
circuitBreakerPolicy: CircuitBreakerPolicy;
/**
* The Cockatiel retry policy that the service policy uses internally.
*/
retryPolicy: RetryPolicy;
/**
* A function which is called when the number of times that the service fails
* in a row meets the set maximum number of consecutive failures.
Expand Down Expand Up @@ -218,6 +231,8 @@ export function createServicePolicy({

return {
...policy,
circuitBreakerPolicy,
retryPolicy,
onBreak,
onDegraded,
onRetry,
Expand Down
4 changes: 4 additions & 0 deletions packages/controller-utils/src/index.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,11 +4,15 @@ describe('@metamask/controller-utils', () => {
it('has expected JavaScript exports', () => {
expect(Object.keys(allExports)).toMatchInlineSnapshot(`
Array [
"BrokenCircuitError",
"CircuitState",
"DEFAULT_CIRCUIT_BREAK_DURATION",
"DEFAULT_DEGRADED_THRESHOLD",
"DEFAULT_MAX_CONSECUTIVE_FAILURES",
"DEFAULT_MAX_RETRIES",
"createServicePolicy",
"handleAll",
"handleWhen",
"BNToHex",
"convertHexToDecimal",
"fetchWithErrorHandling",
Expand Down
4 changes: 4 additions & 0 deletions packages/controller-utils/src/index.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,13 @@
export {
BrokenCircuitError,
CircuitState,
DEFAULT_CIRCUIT_BREAK_DURATION,
DEFAULT_DEGRADED_THRESHOLD,
DEFAULT_MAX_CONSECUTIVE_FAILURES,
DEFAULT_MAX_RETRIES,
createServicePolicy,
handleAll,
handleWhen,
} from './create-service-policy';
export type {
CreateServicePolicyOptions,
Expand Down
Loading