Skip to content

Commit 067a807

Browse files
committed
refactor(network-controller): improve RPC service chain configuration
- Simplify RPC service configuration creation using map - Update CHANGELOG with corrected event payload documentation - Fix test descriptions for clarity - Remove unused RpcServiceConfiguration type
1 parent 0f55384 commit 067a807

File tree

4 files changed

+9
-14
lines changed

4 files changed

+9
-14
lines changed

packages/network-controller/CHANGELOG.md

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -20,9 +20,9 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
2020
- The event payloads for all events have been changed as well: `failoverEndpointUrl` has been renamed to `endpointUrl`, and `endpointUrl` has been renamed to `primaryEndpointUrl`. In addition, `networkClientId` has been added to the payload.
2121
- **BREAKING:** Rename and update payload data for `NetworkController:rpcEndpointRequestRetried` ([#7166](https://github.com/MetaMask/core/pull/7166))
2222
- This event is now called `NetworkController:rpcEndpointRetried`.
23-
- The event payload has been changed as well: `failoverEndpointUrl` has been renamed to `endpointUrl`, and `endpointUrl` has been renamed to `primaryEndpointUrl`. In addition, `networkClientId` and `attempt` have been added to the payload.
23+
- The event payload has been changed as well: `failoverEndpointUrl` has been removed, and `primaryEndpointUrl` has been added. In addition, `networkClientId` and `attempt` have been added to the payload.
2424
- **BREAKING:** Update `AbstractRpcService`/`RpcServiceRequestable` to remove `{ isolated: true }` from the `onBreak` event data type ([#7166](https://github.com/MetaMask/core/pull/7166))
25-
- This represented the error produced when `isolate` is called on a Cockatiel circuit breaker policy, which we don't do (at least, not in the way it is designed to be called)
25+
- This represented the error produced when `isolate` is called on a Cockatiel circuit breaker policy. This never happens for our service (we use `isolate` internally, but this error is suppressed and cannot trigger `onBreak`)
2626
- Move peer dependencies for controller and service packages to direct dependencies ([#7209](https://github.com/MetaMask/core/pull/7209))
2727
- The dependencies moved are:
2828
- `@metamask/error-reporting-service` (^3.0.0)

packages/network-controller/src/create-network-client.ts

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -207,11 +207,11 @@ function createRpcServiceChain({
207207
const availableEndpointUrls: [string, ...string[]] = isRpcFailoverEnabled
208208
? [primaryEndpointUrl, ...(configuration.failoverRpcUrls ?? [])]
209209
: [primaryEndpointUrl];
210-
const buildRpcServiceConfiguration = (endpointUrl: string) => ({
210+
const rpcServiceConfigurations = availableEndpointUrls.map((endpointUrl) => ({
211211
...getRpcServiceOptions(endpointUrl),
212212
endpointUrl,
213213
logger,
214-
});
214+
}));
215215

216216
const getError = (value: object) => {
217217
if ('error' in value) {
@@ -223,8 +223,8 @@ function createRpcServiceChain({
223223
};
224224

225225
const rpcServiceChain = new RpcServiceChain([
226-
buildRpcServiceConfiguration(availableEndpointUrls[0]),
227-
...availableEndpointUrls.slice(1).map(buildRpcServiceConfiguration),
226+
rpcServiceConfigurations[0],
227+
...rpcServiceConfigurations.slice(1),
228228
]);
229229

230230
rpcServiceChain.onBreak(

packages/network-controller/src/rpc-service/rpc-service-chain.test.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -975,7 +975,7 @@ describe('RpcServiceChain', () => {
975975
await expect(rpcServiceChain.request(jsonRpcRequest)).rejects.toThrow(
976976
expectedError,
977977
);
978-
// Retry the second endpoint for a third time, until max retries is hit.
978+
// Retry the third endpoint for a third time, until max retries is hit.
979979
// The circuit will break on the last time.
980980
await expect(rpcServiceChain.request(jsonRpcRequest)).rejects.toThrow(
981981
expectedError,
@@ -1251,7 +1251,7 @@ describe('RpcServiceChain', () => {
12511251
});
12521252
});
12531253

1254-
it("calls onDegraded again when a service's underlying circuit breaks, and then after waiting, the service responds successfully but slowly", async () => {
1254+
it('calls onDegraded again when the service chain becomes unavailable, and then after waiting, the failover service responds successfully but slowly', async () => {
12551255
const endpointUrl = 'https://some.endpoint';
12561256
nock(endpointUrl)
12571257
.post('/', {

packages/network-controller/src/rpc-service/rpc-service-chain.ts

Lines changed: 1 addition & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -37,8 +37,6 @@ const STATUSES = {
3737
*/
3838
type Status = (typeof STATUSES)[keyof typeof STATUSES];
3939

40-
type RpcServiceConfiguration = Omit<RpcServiceOptions, 'failoverService'>;
41-
4240
/**
4341
* This class constructs and manages requests to a chain of RpcService objects
4442
* which represent RPC endpoints with which to access a particular network. The
@@ -93,10 +91,7 @@ export class RpcServiceChain {
9391
* {@link RpcServiceOptions}.
9492
*/
9593
constructor(
96-
rpcServiceConfigurations: [
97-
RpcServiceConfiguration,
98-
...RpcServiceConfiguration[],
99-
],
94+
rpcServiceConfigurations: [RpcServiceOptions, ...RpcServiceOptions[]],
10095
) {
10196
this.#services = rpcServiceConfigurations.map(
10297
(rpcServiceConfiguration) => new RpcService(rpcServiceConfiguration),

0 commit comments

Comments
 (0)