-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add exponential backoff when rate limited
- Loading branch information
Showing
6 changed files
with
50 additions
and
7 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,3 +1,7 @@ | ||
## 0.1.2 | ||
|
||
- Add exponential backoff when rate limited | ||
|
||
## 0.1.1 | ||
|
||
- Add context length management features | ||
|
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
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -20,6 +20,7 @@ mod model; | |
mod ollama; | ||
mod openai; | ||
mod option; | ||
mod requests; | ||
mod template; | ||
#[cfg(test)] | ||
mod tests; | ||
|
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,32 @@ | ||
use serde::Serialize; | ||
|
||
pub fn request_with_retry( | ||
req: ureq::Request, | ||
body: impl Serialize, | ||
) -> Result<ureq::Response, ureq::Error> { | ||
const MAX_TRIES: u32 = 4; | ||
let mut try_num = 0; | ||
let delay = 1000; | ||
loop { | ||
let response = req.clone().send_json(&body); | ||
match response { | ||
Ok(res) => return Ok(res), | ||
Err(ureq::Error::Status(code, response)) => { | ||
if code != 429 || try_num > MAX_TRIES { | ||
return Err(ureq::Error::Status(code, response)); | ||
} | ||
|
||
// This is potentially retryable. We don't do anything smart right now, just a | ||
// random exponential backoff. | ||
|
||
let perturb = fastrand::i32(-100..100); | ||
let this_delay = 2i32.pow(try_num) * delay + perturb; | ||
|
||
eprintln!("Rate limited... waiting {this_delay}ms to retry"); | ||
std::thread::sleep(std::time::Duration::from_millis(this_delay as u64)); | ||
try_num += 1; | ||
} | ||
e @ Err(_) => return e, | ||
} | ||
} | ||
} |