-
Notifications
You must be signed in to change notification settings - Fork 249
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
Add basic failover functionality #1216
Conversation
🦋 Changeset detectedLatest commit: 65c6899 The changes in this PR will be included in the next version bump. This PR includes changesets to release 5 packages
Not sure what this means? Click here to learn what changesets are. Click here if you're a maintainer who wants to add another changeset to this PR |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Looks good, I've suggested some minor changes
|
||
// Provider example | ||
const provider = new providers.JsonRpcProvider({ | ||
// preoritized list of RPC Servers |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
// preoritized list of RPC Servers | |
// prioritized list of RPC Servers |
packages/providers/src/fetch_json.ts
Outdated
@@ -24,36 +26,45 @@ export async function fetchJson(connectionInfoOrUrl: string | ConnectionInfo, js | |||
} else { | |||
connectionInfo = connectionInfoOrUrl as ConnectionInfo; | |||
} | |||
|
|||
if (Array.isArray(connectionInfo.url) && !connectionInfo.url.length) { | |||
throw new Error('The prioritized array of RPC Server URLs should not be empty'); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
throw new Error('The prioritized array of RPC Server URLs should not be empty'); | |
throw new Error('The prioritized array of RPC Server URLs must not be empty'); |
packages/providers/src/fetch_json.ts
Outdated
headers: { ...connectionInfo.headers, 'Content-Type': 'application/json' } | ||
headers: { | ||
'Content-Type': 'application/json', | ||
'x-api-key': connectionInfo.apiKeys ? connectionInfo.apiKeys[currentRpcServer] : undefined, |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I think that null
is semantically better than undefined
in this case
'x-api-key': connectionInfo.apiKeys ? connectionInfo.apiKeys[currentRpcServer] : undefined, | |
'x-api-key': connectionInfo.apiKeys ? connectionInfo.apiKeys[currentRpcServer] : null, |
@@ -333,6 +333,19 @@ export class JsonRpcProvider extends Provider { | |||
return await this.sendJsonRpc('gas_price', [blockId]); | |||
} | |||
|
|||
/** | |||
* Part of the RPC failover design. | |||
* Changing current (first) rpc server and moves it to the end of the queue. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
* Changing current (first) rpc server and moves it to the end of the queue. | |
* Rotates the current RPC server with a new one (if available) and moves the former to the end of the queue. |
* Changing current (first) rpc server and moves it to the end of the queue. | ||
*/ | ||
private rotateRpcServers() { | ||
if (typeof this.connection.url === 'string') { return; } |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
if (typeof this.connection.url === 'string') { return; } | |
if (typeof this.connection.url === 'string' || this.connection.url.length === 1) { return; } |
try { | ||
await provider.status(); | ||
} catch (e) { | ||
expect(e.message).toEqual('The prioritized array of RPC Server URLs should not be empty'); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
expect(e.message).toEqual('The prioritized array of RPC Server URLs should not be empty'); | |
expect(e.message).toEqual('The prioritized array of RPC Server URLs cannot be empty'); |
The failover mechanism looks exciting to me, I've been tracking it since #760, and it's going to be very useful However, I wanted to make sure the implementation would cover all use cases, and one of them is what if a user has different connection data for providers For instance, how can the user provide the following list of RPCs?
And it's widespread for each provider to have its own set of headers or other parameters that are required, there's no standard for it I propose not to extend the
To make it work properly, we would also need to introduce the new field
@gtsonevv |
Hey @denbite, thank you for the feedback. On first glance your approach seems reasonable, we'll thoroughly analyze it internally and get back to you. |
It would be great to have the link to the available RPC providers also easily discoverable for the devs configuring the connection: https://docs.near.org/api/rpc/providers |
quite a few people have been asking me recently about the failover mechanism to make their applications more robust, @vikinatora @gtsonevv do you guys have ETA for this feature? P.S. let me know if you need any help in developing, I'd be happy to take over and deliver this great functionality to active developers as quickly as possible |
@denbite We can give you an ETA, as we have commited to delivering other functionalities. We'd be happy for you take over and finish this functionality. |
@gtsonevv, we can close this one. |
closing as requested by @vikinatora |
Pre-flight checklist
pnpm changeset
to create achangeset
JSON document appropriate for this change.Motivation
Added support for multiple API Keys (since we can have multiple RPC Servers)
Added basic failover functionality
With this design of failover, we will change (rotate) our server when needed and stick to this new server for our next calls. It will help to avoid long responses when first (or more) servers are down.
For now, server rotation is happening only on Timeout error.
Also, check this comment: #733 (comment)
Test Plan
Added unit tests.
Related issues/PRs
#733
#760