-
Notifications
You must be signed in to change notification settings - Fork 2.1k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
fix(prover): Create reqwest client only once (#3324)
## What ❔ Create reqwest client only once. Additionally HttpClient exports metric `calls` with all the requests and correct status codes. <!-- What are the changes this PR brings about? --> <!-- Example: This PR adds a PR template to the repo. --> <!-- (For bigger PRs adding more context is appreciated) --> ## Why ❔ Creating reqwest client is expensive because it initializes TLS, loads certificates, etc. So it should be create only once and reused. Create new internal mod http_client instead of patching zksync_utils because fn `send_request_with_retries` is used only in prover_autoscaler and outdated prover_fri, which will be removed soon. <!-- Why are these changes done? What goal do they contribute to? What are the principles behind them? --> <!-- Example: PR templates ensure PR reviewers, observers, and future iterators are in context about the evolution of repos. --> ## Checklist <!-- Check your PR fulfills the following items. --> <!-- For draft PRs check the boxes as you complete them. --> - [x] PR title corresponds to the body of PR (we generate changelog entries from PRs). - [ ] Tests for the changes have been added / updated. - [ ] Documentation comments have been added / updated. - [x] Code has been formatted via `zkstack dev fmt` and `zkstack dev lint`. ref ZKD-1855
- Loading branch information
Showing
7 changed files
with
151 additions
and
46 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,94 @@ | ||
use reqwest::{header::HeaderMap, Client, Error, Method, Response, StatusCode}; | ||
use tokio::time::{sleep, Duration}; | ||
|
||
use crate::metrics::AUTOSCALER_METRICS; | ||
|
||
#[derive(Clone)] | ||
pub struct HttpClient { | ||
client: Client, | ||
max_retries: usize, | ||
} | ||
|
||
impl Default for HttpClient { | ||
fn default() -> Self { | ||
Self { | ||
client: Client::new(), | ||
max_retries: 5, | ||
} | ||
} | ||
} | ||
|
||
#[derive(Debug)] | ||
pub enum HttpError { | ||
ReqwestError(Error), | ||
RetryExhausted(String), | ||
} | ||
|
||
impl HttpClient { | ||
/// Method to send HTTP request with fixed number of retires with exponential back-offs. | ||
pub async fn send_request_with_retries( | ||
&self, | ||
url: &str, | ||
method: Method, | ||
headers: Option<HeaderMap>, | ||
body: Option<Vec<u8>>, | ||
) -> Result<Response, HttpError> { | ||
let mut retries = 0usize; | ||
let mut delay = Duration::from_secs(1); | ||
loop { | ||
let result = self | ||
.send_request(url, method.clone(), headers.clone(), body.clone()) | ||
.await; | ||
AUTOSCALER_METRICS.calls[&( | ||
url.into(), | ||
match result { | ||
Ok(ref response) => response.status().as_u16(), | ||
Err(ref err) => err | ||
.status() | ||
.unwrap_or(StatusCode::INTERNAL_SERVER_ERROR) | ||
.as_u16(), | ||
}, | ||
)] | ||
.inc(); | ||
match result { | ||
Ok(response) if response.status().is_success() => return Ok(response), | ||
Ok(response) => { | ||
tracing::error!("Received non OK http response {:?}", response.status()) | ||
} | ||
Err(err) => tracing::error!("Error while sending http request {:?}", err), | ||
} | ||
|
||
if retries >= self.max_retries { | ||
return Err(HttpError::RetryExhausted(format!( | ||
"All {} http retires failed", | ||
self.max_retries | ||
))); | ||
} | ||
retries += 1; | ||
sleep(delay).await; | ||
delay = delay.checked_mul(2).unwrap_or(Duration::MAX); | ||
} | ||
} | ||
|
||
async fn send_request( | ||
&self, | ||
url: &str, | ||
method: Method, | ||
headers: Option<HeaderMap>, | ||
body: Option<Vec<u8>>, | ||
) -> Result<Response, Error> { | ||
let mut request = self.client.request(method, url); | ||
|
||
if let Some(headers) = headers { | ||
request = request.headers(headers); | ||
} | ||
|
||
if let Some(body) = body { | ||
request = request.body(body); | ||
} | ||
|
||
let request = request.build()?; | ||
let response = self.client.execute(request).await?; | ||
Ok(response) | ||
} | ||
} |
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