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

Start extracting service policy boilerplate to common function #5141

Merged
merged 8 commits into from
Jan 14, 2025

Conversation

mcmire
Copy link
Contributor

@mcmire mcmire commented Jan 13, 2025

Explanation

We would like to use the Cockatiel library in our service classes to ensure that requests are retried using the circuit breaker pattern. Some of our service classes do this already, but we are copying and pasting the code around.

This commit extracts some of the boilerplate code to a new function in the @metamask/controller-utils package, createServicePolicy, so that we no longer have to do this. Because it would require an enormous amount of tests, this commit does not include a complete version of createServicePolicy, notably omitting the maxRetries option as well as an onDegraded callback so consumers can handle slow requests. Those will be added later.

References

Progresses #4994.

Changelog

(Updated in PR.)

Checklist

  • I've updated the test suite for new or updated code as appropriate
  • I've updated documentation (JSDoc, Markdown, etc.) for new or updated code as appropriate
  • I've highlighted breaking changes using the "BREAKING" category above as appropriate
  • I've prepared draft pull requests for clients and consumer packages to resolve any breaking changes

it('has expected JavaScript exports', () => {
expect(Object.keys(allExports)).toMatchInlineSnapshot(`
Array [
"createServicePolicy",
Copy link
Contributor Author

@mcmire mcmire Jan 13, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This line specifically refers to the new export we are adding; the rest of the exports already exist, we are just adding tests for all exports for the first time.

@mcmire mcmire force-pushed the extract-create-service-policy-1 branch from cb1b08e to d23f319 Compare January 13, 2025 21:23
We would like to use the Cockatiel library in our service classes to
ensure that requests are retried using the circuit breaker pattern. Some
of our service classes do this already, but we are copying and pasting
the code around. This commit extracts some of the boilerplate code to a
new function in the `@metamask/controller-utils` package,
`createServicePolicy` so that we no longer have to do this. Because it
would require an enormous amount of tests, this commit does not include
a complete version of `createServicePolicy`, notably omitting
`maxRetries` option as well as a way to handle slow requests; those will
be added later.
@mcmire mcmire force-pushed the extract-create-service-policy-1 branch from d23f319 to 86ef36f Compare January 13, 2025 21:55
@mcmire mcmire marked this pull request as ready for review January 13, 2025 22:11
@mcmire mcmire requested a review from a team as a code owner January 13, 2025 22:11
maxAttempts: DEFAULT_MAX_RETRIES,
// Retries of the service will be executed following ever increasing delays,
// determined by a backoff formula.
backoff: new ExponentialBackoff(),
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Nit: do we want to make this customizable but defaulting to ExponentialBackoff ?

onBreak?: () => void;
onRetry?: () => void;
} = {}): IPolicy {
const retryPolicy = retry(handleAll, {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Just a question we are using handleAll which will retry any kind of failure ? do we want to filter some failures we don't want to retry ?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes, good thought. I plan on adding an option to customize that in another PR.

@@ -0,0 +1,589 @@
import { useFakeTimers } from 'sinon';
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Nit: can't we just use jest fake timers ?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

No, because we're still using an older version of Jest, and it doesn't support the "async" varieties of next and tick.

Copy link
Contributor

@cryptodev-2s cryptodev-2s left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Overall, this looks good to me. I’ve left a few comments/questions; once they’re answered, I can approve.

Copy link
Contributor

@cryptodev-2s cryptodev-2s left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

After checking the PR description, I believe I got all my questions answered. maybe the areas of improvements can be handled separately. I can approve when CI succeeds

@mcmire mcmire merged commit 6fdfbd7 into main Jan 14, 2025
117 checks passed
@mcmire mcmire deleted the extract-create-service-policy-1 branch January 14, 2025 16:19
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants