-
Notifications
You must be signed in to change notification settings - Fork 91
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge branch 'dex-solver-rate-limit' into colocation_rc
- Loading branch information
Showing
14 changed files
with
309 additions
and
41 deletions.
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
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,58 @@ | ||
use { | ||
anyhow::Result, | ||
shared::rate_limiter::{ | ||
RateLimiter as SharedRateLimiter, | ||
RateLimiterError as SharedRateLimiterError, | ||
RateLimitingStrategy as SharedRateLimitingStrategy, | ||
}, | ||
std::{future::Future, time::Duration}, | ||
thiserror::Error, | ||
}; | ||
|
||
pub struct RateLimiter { | ||
inner: SharedRateLimiter, | ||
} | ||
|
||
#[derive(Debug, Clone)] | ||
pub struct RateLimitingStrategy { | ||
inner: SharedRateLimitingStrategy, | ||
} | ||
|
||
impl RateLimitingStrategy { | ||
pub fn try_new( | ||
back_off_growth_factor: f64, | ||
min_back_off: Duration, | ||
max_back_off: Duration, | ||
) -> Result<Self> { | ||
SharedRateLimitingStrategy::try_new(back_off_growth_factor, min_back_off, max_back_off) | ||
.map(|shared| Self { inner: shared }) | ||
} | ||
} | ||
|
||
#[derive(Error, Debug, Clone, Default, PartialEq)] | ||
pub enum RateLimiterError { | ||
#[default] | ||
#[error("rate limited")] | ||
RateLimited, | ||
} | ||
|
||
impl RateLimiter { | ||
pub fn new(strategy: RateLimitingStrategy, name: String) -> Self { | ||
Self { | ||
inner: SharedRateLimiter::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 { | ||
SharedRateLimiterError::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
Oops, something went wrong.