eval: Add jitter to retry attempts (#32542)
Adds some jitter to avoid the issue that all requests will retry at roughly the same time in eval where we have a lot of concurrent requests. Release Notes: - N/A
This commit is contained in:
parent
6c4728f00f
commit
2ecc24eb26
1 changed files with 8 additions and 3 deletions
|
@ -1634,15 +1634,20 @@ impl EditAgentTest {
|
|||
}
|
||||
|
||||
async fn retry_on_rate_limit<R>(mut request: impl AsyncFnMut() -> Result<R>) -> Result<R> {
|
||||
let mut attempt = 0;
|
||||
loop {
|
||||
attempt += 1;
|
||||
match request().await {
|
||||
Ok(result) => return Ok(result),
|
||||
Err(err) => match err.downcast::<LanguageModelCompletionError>() {
|
||||
Ok(err) => match err {
|
||||
LanguageModelCompletionError::RateLimit(duration) => {
|
||||
// Wait until after we are allowed to try again
|
||||
eprintln!("Rate limit exceeded. Waiting for {duration:?}...",);
|
||||
Timer::after(duration).await;
|
||||
// Wait for the duration supplied, with some jitter to avoid all requests being made at the same time.
|
||||
let jitter = duration.mul_f64(rand::thread_rng().gen_range(0.0..0.5));
|
||||
eprintln!(
|
||||
"Attempt #{attempt}: Rate limit exceeded. Retry after {duration:?} + jitter of {jitter:?}"
|
||||
);
|
||||
Timer::after(duration + jitter).await;
|
||||
continue;
|
||||
}
|
||||
_ => return Err(err.into()),
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue