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

Refactor services to align with eventual RpcService #5109

Merged
merged 5 commits into from
Jan 24, 2025

Conversation

mcmire
Copy link
Contributor

@mcmire mcmire commented Jan 7, 2025

Explanation

In a future commit, we are adding an RpcService class to network-controller to handle automatic failovers. This class is different from the existing service classes in a couple of ways:

  • RpcService will make use of a new createServicePolicy function which encapsulates retry and circuit breaker logic. This will relieve any module that consumes RpcService from needing to test this logic.
  • RpcService will have onBreak and onDegraded methods instead of taking onBreak and onDegraded callbacks as constructor options. This is reflected not only in the class but also in the abstract RPC service interface.

To these ends, this commit makes the following changes to CodefiTokenPricesServiceV2 in assets-controllers and ClientConfigApiService in remote-feature-flag-controller:

  • Refactor service class to use createServicePolicy
  • Update the abstract service class interface to extend ServicePolicy from controller-utils
  • Deprecate the onBreak and onDegraded constructor options, and add methods instead

Note that we are not including TokenSearchApiService in this commit, even though it is a class, because it does not use the retry or circuit breaker policy.

References

Closes #4994.

Changelog

(Updated in changelog.)

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

@mcmire mcmire force-pushed the extract-create-service-policy branch from 627b096 to 530bc4c Compare January 8, 2025 22:44
@mcmire mcmire force-pushed the refactor-service-classes branch from 1c790a7 to 987ec77 Compare January 8, 2025 22:45
@mcmire mcmire force-pushed the refactor-service-classes branch from 987ec77 to 778223c Compare January 16, 2025 18:36
@mcmire mcmire changed the base branch from extract-create-service-policy to extract-create-service-policy-3 January 16, 2025 18:39
@mcmire mcmire changed the title Use common function for service policy boilerplate Refactor service classes to use createServicePolicy Jan 16, 2025
@mcmire mcmire force-pushed the refactor-service-classes branch 2 times, most recently from 9d15cfb to 3b0eaff Compare January 16, 2025 18:56
Base automatically changed from extract-create-service-policy-3 to main January 22, 2025 23:33
Both RetryPolicy and CircuitBreakerPolicy from the Cockatiel library
allow for listening for events using `on*` methods. Our "service policy"
allows for listening to some of the same events as well as the
"degraded" event. For parity with Cockatiel — and for easier use in
general — this commit removes the callbacks that `createServicePolicy`
takes and replaces them with `on*` methods.
@mcmire mcmire force-pushed the refactor-service-classes branch 2 times, most recently from 476c1a8 to fe74a57 Compare January 23, 2025 20:13
@mcmire mcmire changed the base branch from main to convert-callbacks-to-methods January 23, 2025 20:16
@mcmire mcmire force-pushed the refactor-service-classes branch 4 times, most recently from 78b5120 to dba35e9 Compare January 23, 2025 22:23
@mcmire mcmire changed the title Refactor service classes to use createServicePolicy Refactor services to align with eventual RpcService Jan 23, 2025
In a future commit, we are adding an RpcService class to
`network-controller` to handle automatic failovers. This class is
different from the existing service classes in a couple of ways:

- RpcService will make use of a new `createServicePolicy` function which
  encapsulates retry and circuit breaker logic. This will relieve the
  consuming module for RpcService from needing to test this logic.
- RpcService will have `onBreak` and `onDegraded` methods instead of
  taking `onBreak` and `onDegraded` callbacks as constructor options.
  This is reflected not only in the class but also in the abstract RPC
  service interface.

To these ends, this commit makes the following changes to
`CodefiTokenPricesServiceV2` in `assets-controllers` and
`ClientConfigApiService` in `remote-feature-flag-controller`:

- Refactor service class to use `createServicePolicy`
- Update the abstract service class interface to extend `ServicePolicy`
  from `controller-utils`, and prepend `I` to the type to indicate that
  it's not a superclass, but an interface
- Deprecate the `onBreak` and `onDegraded` constructor options, and add
  methods instead
@mcmire mcmire force-pushed the refactor-service-classes branch from dba35e9 to 2e29f94 Compare January 23, 2025 22:32
@@ -829,8 +1055,9 @@ describe('CodefiTokenPricesServiceV2', () => {
clock.restore();
});

it('does not call onDegraded when requests succeeds faster than threshold', async () => {
Copy link
Contributor Author

Choose a reason for hiding this comment

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

We don't need these tests because this functionality is already covered in the tests for createServicePolicy. We just need to test that the onDegraded callback option works somehow.

@@ -1187,7 +1126,7 @@ describe('CodefiTokenPricesServiceV2', () => {
clock.restore();
});

it('stops making fetch requests after too many consecutive failures', async () => {
Copy link
Contributor Author

Choose a reason for hiding this comment

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

Similar for this test, it's already covered.


/**
* A service object responsible for fetching feature flags.
*/
export type AbstractClientConfigApiService =
PublicInterface<ClientConfigApiService>;
export type IAbstractClientConfigApiService = Partial<
Copy link
Contributor Author

Choose a reason for hiding this comment

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

I opted not to use PublicInterface here because it aligns with AbstractTokenPricesService, and it's more explicit IMO.

});

describe('circuit breaker', () => {
it('opens the circuit breaker after consecutive failures', async () => {
Copy link
Contributor Author

Choose a reason for hiding this comment

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

Similar to tests in codefi-v2.test.ts, we don't need these since they're already covered by the createServicePolicy tests.

Base automatically changed from convert-callbacks-to-methods to main January 23, 2025 23:00
@mcmire mcmire marked this pull request as ready for review January 23, 2025 23:50
@mcmire mcmire requested review from a team as code owners January 23, 2025 23:50
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.

LGTM!


- Deprecate `ClientConfigApiService` constructor options `onBreak` and `onDegraded` in favor of methods ([#5109](https://github.com/MetaMask/core/pull/5109))
- Add `@metamask/controller-utils@^11.4.5` as a dependency ([#5109](https://github.com/MetaMask/core/pull/5109))
- `cockatiel` should still be in the dependency tree because it's now a dependency of `@metamask/controller-utils`
Copy link
Contributor

Choose a reason for hiding this comment

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

(comment, can be resolved) IIRC extension assets controller had added patches for cockatiel imports. It would be interesting to see if they are still needed, or if the patch is instead moved to this utils package.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Ah interesting. I wonder if Cockatiel defines a default export in addition to named exports. But to answer your question yes I think these patches may need to be moved to controller-utils.

@mcmire mcmire merged commit 5058fef into main Jan 24, 2025
123 checks passed
@mcmire mcmire deleted the refactor-service-classes branch January 24, 2025 14:52
zone-live pushed a commit that referenced this pull request Jan 27, 2025
In a future commit, we are adding an RpcService class to
`network-controller` to handle automatic failovers. This class is
different from the existing service classes in a couple of ways:

- RpcService will make use of a new `createServicePolicy` function which
encapsulates retry and circuit breaker logic. This will relieve any
module that consumes RpcService from needing to test this logic.
- RpcService will have `onBreak` and `onDegraded` methods instead of
taking `onBreak` and `onDegraded` callbacks as constructor options. This
is reflected not only in the class but also in the abstract RPC service
interface.

To these ends, this commit makes the following changes to
`CodefiTokenPricesServiceV2` in `assets-controllers` and
`ClientConfigApiService` in `remote-feature-flag-controller`:

- Refactor service class to use `createServicePolicy`
- Update the abstract service class interface to extend `ServicePolicy`
from `controller-utils`
- Deprecate the `onBreak` and `onDegraded` constructor options, and add
methods instead

Note that we are not including `TokenSearchApiService` in this commit,
even though it is a class, because it does not use the retry or circuit
breaker policy.
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.

Refactor service classes to reuse common service setup code
3 participants