-
Notifications
You must be signed in to change notification settings - Fork 83
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
# Description Properly handles `429 Too Many Requests` response from DEX solvers by utilizing `shared::RateLimiter` to back off further requests with a cool-down period. # Changes - A new`RateLimited` variant on solvers::infra::dex::Error - Parse each individual dex solver's HTTP sub-error with status 429 into this error type - Used `shared::rate_limiter::RateLimiter` though `solvers::boundary` - A new config for solvers with `max_retries` for the `solvers` crate ## How to test TBD: is it possible to simulate it in staging? ## Related Issues Fixes #2068
- Loading branch information
1 parent
a72c4c3
commit 575c679
Showing
13 changed files
with
287 additions
and
43 deletions.
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
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,53 @@ | ||
use { | ||
anyhow::Result, | ||
std::{future::Future, time::Duration}, | ||
thiserror::Error, | ||
}; | ||
|
||
pub struct RateLimiter { | ||
inner: shared::RateLimiter, | ||
} | ||
|
||
#[derive(Debug, Clone)] | ||
pub struct RateLimitingStrategy { | ||
inner: shared::RateLimitingStrategy, | ||
} | ||
|
||
impl RateLimitingStrategy { | ||
pub fn try_new( | ||
back_off_growth_factor: f64, | ||
min_back_off: Duration, | ||
max_back_off: Duration, | ||
) -> Result<Self> { | ||
shared::RateLimitingStrategy::try_new(back_off_growth_factor, min_back_off, max_back_off) | ||
.map(|shared| Self { inner: shared }) | ||
} | ||
} | ||
|
||
#[derive(Error, Debug, Clone, Default)] | ||
pub enum RateLimiterError { | ||
#[default] | ||
#[error("rate limited")] | ||
RateLimited, | ||
} | ||
|
||
impl RateLimiter { | ||
pub fn new(strategy: RateLimitingStrategy, name: String) -> Self { | ||
Self { | ||
inner: shared::RateLimiter::from_strategy(strategy.inner, name), | ||
} | ||
} | ||
|
||
pub async fn execute_with_back_off<T>( | ||
&self, | ||
task: impl Future<Output = T>, | ||
requires_back_off: impl Fn(&T) -> bool, | ||
) -> Result<T, RateLimiterError> { | ||
self.inner | ||
.execute_with_back_off(task, requires_back_off) | ||
.await | ||
.map_err(|err| match err { | ||
shared::RateLimiterError::RateLimited => RateLimiterError::RateLimited, | ||
}) | ||
} | ||
} |
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
Oops, something went wrong.