-
Notifications
You must be signed in to change notification settings - Fork 327
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
fix: add WASM-compatible retry mechanism for RPC requests #481
Open
DeVikingMark
wants to merge
6
commits into
a16z:master
Choose a base branch
from
DeVikingMark:shmixer
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
6 commits
Select commit
Hold shift + click to select a range
7ee5d7c
Create http_rpc.rs
DeVikingMark 990f334
Create http_rpc_tests.rs
DeVikingMark c713b85
Update http_rpc_tests.rs
DeVikingMark f6dc528
Update http_rpc.rs
DeVikingMark 299fff7
Update http_rpc_tests.rs
DeVikingMark 18ec67b
Update Cargo.toml
DeVikingMark File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,76 @@ | ||
use std::time::Duration; | ||
use wasmtimer::tokio::sleep; | ||
|
||
/// Retry mechanism configuration | ||
#[derive(Clone, Debug)] | ||
pub struct RetryConfig { | ||
/// Maximum number of attempts to execute the request | ||
pub max_attempts: u32, | ||
/// Initial delay between retry attempts | ||
pub initial_backoff: Duration, | ||
/// Maximum delay between retry attempts | ||
pub max_backoff: Duration, | ||
} | ||
|
||
impl HttpProvider { | ||
// Add the ability to configure retry settings | ||
pub fn with_retry_config(mut self, config: RetryConfig) -> Self { | ||
self.retry_config = config; | ||
self | ||
} | ||
|
||
async fn execute_with_retry<T>(&self, request: Request<T>) -> Result<Response<T>, Error> | ||
where | ||
T: serde::Serialize + Send + Sync, | ||
{ | ||
let mut attempts = 0; | ||
let mut backoff = self.retry_config.initial_backoff; | ||
|
||
loop { | ||
attempts += 1; | ||
match self.execute_internal(request.clone()).await { | ||
Ok(response) => return Ok(response), | ||
Err(err) => { | ||
if !should_retry(&err) || attempts >= self.retry_config.max_attempts { | ||
return Err(err.into()); | ||
} | ||
|
||
tracing::debug!( | ||
"Request failed with error: {:?}. Retrying ({}/{})", | ||
err, | ||
attempts, | ||
self.retry_config.max_attempts | ||
); | ||
|
||
sleep(backoff).await; | ||
|
||
backoff = std::cmp::min( | ||
backoff * 2, | ||
self.retry_config.max_backoff | ||
); | ||
} | ||
} | ||
} | ||
} | ||
} | ||
|
||
// Extend the list of errors that can trigger a retry | ||
fn should_retry(error: &Error) -> bool { | ||
match error { | ||
Error::RateLimitExceeded(_) => true, | ||
Error::ConnectionError(_) => true, | ||
Error::TimeoutError => true, | ||
Error::ServerError(status) => status.as_u16() >= 500, | ||
_ => false, | ||
} | ||
} | ||
|
||
// Update the existing execute method to use the retry mechanism | ||
impl Provider for HttpProvider { | ||
async fn execute<T>(&self, request: Request<T>) -> Result<Response<T>, Error> | ||
where | ||
T: serde::Serialize + Send + Sync, | ||
{ | ||
self.execute_with_retry(request).await | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,39 @@ | ||
use mockito::{mock, Server}; | ||
use std::time::Duration; | ||
|
||
#[tokio::test] | ||
async fn test_retry_mechanism() { | ||
let mut server = Server::new(); | ||
|
||
// Test successful retry after a temporary error | ||
let mock = server.mock("POST", "/") | ||
.with_status(429) // Rate limit error | ||
.create(); | ||
|
||
let provider = HttpProvider::new(server.url().as_str()); | ||
let request = Request::new("eth_blockNumber", ()); | ||
|
||
let result = provider.execute(request).await; | ||
assert!(result.is_err()); | ||
mock.assert_hits(1); | ||
|
||
// Test the maximum number of retry attempts | ||
let mock = server.mock("POST", "/") | ||
.with_status(429) | ||
.expect(3) // Expect exactly 3 attempts | ||
.create(); | ||
|
||
let result = provider.execute(request).await; | ||
assert!(result.is_err()); | ||
mock.assert(); | ||
|
||
// Test for errors that should not trigger retries | ||
let mock = server.mock("POST", "/") | ||
.with_status(400) // Bad Request | ||
.expect(1) // Expect only 1 attempt | ||
.create(); | ||
|
||
let result = provider.execute(request).await; | ||
assert!(result.is_err()); | ||
mock.assert(); | ||
} |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
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.
It seems that this is not in the right directory so isn't even being compiled? Should be in src/execution/rpc.