Skip to content

Complete createServicePolicy#5149

Merged
mcmire merged 8 commits intomainfrom
extract-create-service-policy-3
Jan 22, 2025
Merged

Complete createServicePolicy#5149
mcmire merged 8 commits intomainfrom
extract-create-service-policy-3

Conversation

@mcmire
Copy link
Copy Markdown
Contributor

@mcmire mcmire commented Jan 14, 2025

Explanation

This commit is a continuation of a previous commit which added a utility function for use in service classes which ensures that they follow the circuit breaker and retry patterns.

This commit completes the function by adding:

  • retryFilterPolicy, which allows for retrying certain errors and not handling others.
  • maxRetries, which allows for giving up early before the max consecutive failures is reached. Note that because this can combine with maxConsecutiveFailures, it adds a bunch of other code paths and so this requires adding a lot of new tests.

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

Base automatically changed from extract-create-service-policy-2 to main January 14, 2025 19:57
@mcmire mcmire force-pushed the extract-create-service-policy-3 branch from 80fa066 to 7605c1e Compare January 14, 2025 22:53
This commit is a continuation of a previous commit which added a utility
function for use in service classes which ensures that they follow the
circuit breaker and retry patterns.

This commit completes the function by adding:

- `retryFilterPolicy`, which allows for retrying certain errors and
  not handling others.
- `maxRetries`, which allows for giving up early before the max
  consecutive failures is reached. Note that because this can combine
  with `maxConsecutiveFailures`, it adds a bunch of other code paths
  and so this requires adding a lot of new tests.
@mcmire mcmire force-pushed the extract-create-service-policy-3 branch from 7605c1e to 66bbe94 Compare January 14, 2025 22:55
Copy link
Copy Markdown
Contributor Author

@mcmire mcmire Jan 14, 2025

Choose a reason for hiding this comment

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

So... the amount of changes here are still very large, and it really screws up the diff. I probably could have pre-organized these tests up front the way they are presented now to make the diff easier to read, but regardless, there are a large number of tests being added.

I will repeat the same instructions I gave before: it's probably best to run this command to view the test names from a high level:

yarn workspace @metamask/controller-utils run jest packages/controller-utils/src/create-service-policy.test.ts --no-coverage

Or, go into VSCode, run "Fold All", and then progressively unfold the describe blocks.

There you should be able to see some patterns. First, I've divided the tests into three major scenarios:

  1. wrapping a service that succeeds on the first try
  2. wrapping a service that always fails
  3. wrapping a service that fails continuously and then succeeds on the final try

The 1st scenario is pretty simple; all we need to really test is that onBreak is never called, and onDegraded is sometimes called.

The 2nd scenario adds complexity, because since both maxRetries and maxConsecutiveFailures are optional, there are four code paths we have to test, and then inside that there are more code paths depending on whether the number of retries is greater than, less than, or equal to the number of consecutive failures. All of that affects whether/when onBreak or onDegraded are called.

Finally, the 3rd scenario is also important because that is where we can test that the service gets re-evaluated if it fails enough times that the circuitBreakDuration elapses. (We also test a bunch of stuff we are already testing in the 2nd scenario. Despite the duplication I thought these tests were still important so we know what happens.)

// do nothing
},
}: {
maxRetries?: number;
Copy link
Copy Markdown
Contributor Author

@mcmire mcmire Jan 14, 2025

Choose a reason for hiding this comment

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

Should we call this retries instead of maxRetries?

I guess a better question is, how would you expect to use this function?

const policy = createServicePolicy({ maxRetries: 3 });

vs:

const policy = createServicePolicy({ retries: 3 });

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

I would opt in to keep maxRetries for clarity and to avoid ambiguity.

@mcmire mcmire marked this pull request as ready for review January 14, 2025 23:11
@mcmire mcmire requested a review from a team as a code owner January 14, 2025 23:11
@mcmire mcmire added the team-wallet-framework Deprecated: Please use `team-core-platform` instead. label Jan 15, 2025
Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

See original (outdated but still relevant) comment: #5149 (comment)

cryptodev-2s
cryptodev-2s previously approved these changes Jan 22, 2025
Copy link
Copy Markdown
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.

LGTM, Feel free to reach out to me again once the conflicts are resolved.

@mcmire mcmire merged commit aec3860 into main Jan 22, 2025
@mcmire mcmire deleted the extract-create-service-policy-3 branch January 22, 2025 23:33
zone-live pushed a commit that referenced this pull request Jan 27, 2025
This commit is a continuation of a previous commit which added a utility
function for use in service classes which ensures that they follow the
circuit breaker and retry patterns.

This commit completes the function by adding:

- `retryFilterPolicy`, which allows for retrying certain errors and not
handling others.
- `maxRetries`, which allows for giving up early before the max
consecutive failures is reached. Note that because this can combine with
`maxConsecutiveFailures`, it adds a bunch of other code paths and so
this requires adding a lot of new tests.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

team-wallet-framework Deprecated: Please use `team-core-platform` instead.

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants