Skip to content

Commit

Permalink
feat(coordinator client): add timeout and retry (#14)
Browse files Browse the repository at this point in the history
  • Loading branch information
0xmountaintop authored Sep 26, 2024
1 parent b8bea4f commit ba26e21
Show file tree
Hide file tree
Showing 3 changed files with 19 additions and 6 deletions.
3 changes: 3 additions & 0 deletions src/config.rs
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,9 @@ pub struct Config {
#[derive(Debug, Serialize, Deserialize, Clone)]
pub struct CoordinatorConfig {
pub base_url: String,
pub retry_count: u32,
pub retry_wait_time_sec: u64,
pub connection_timeout_sec: u64,
}

#[derive(Debug, Serialize, Deserialize, Clone)]
Expand Down
20 changes: 15 additions & 5 deletions src/coordinator_handler/api.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,23 +2,33 @@ use super::{
ChallengeResponseData, GetTaskRequest, GetTaskResponseData, LoginRequest, LoginResponseData,
Response, SubmitProofRequest, SubmitProofResponseData,
};
use crate::config::CoordinatorConfig;
use core::time::Duration;
use reqwest::{header::CONTENT_TYPE, Url};
use reqwest_middleware::{ClientBuilder, ClientWithMiddleware};
use reqwest_retry::{policies::ExponentialBackoff, RetryTransientMiddleware};
use serde::Serialize;

pub struct Api {
pub base_url: Url,
send_timeout: Duration,
pub client: ClientWithMiddleware,
}

impl Api {
pub fn new(base_url: &str) -> anyhow::Result<Self> {
pub fn new(cfg: CoordinatorConfig) -> anyhow::Result<Self> {
let retry_wait_duration = Duration::from_secs(cfg.retry_wait_time_sec);
let retry_policy = ExponentialBackoff::builder()
.retry_bounds(retry_wait_duration / 2, retry_wait_duration)
.build_with_max_retries(cfg.retry_count);

let client = ClientBuilder::new(reqwest::Client::new())
// .with(RetryTransientMiddleware::new_with_policy(retry_policy)) // TODO: retry policy
.with(RetryTransientMiddleware::new_with_policy(retry_policy))
.build();

Ok(Self {
base_url: Url::parse(base_url)?,
base_url: Url::parse(&cfg.base_url)?,
send_timeout: core::time::Duration::from_secs(cfg.connection_timeout_sec),
client,
})
}
Expand Down Expand Up @@ -47,7 +57,7 @@ impl Api {
.header(CONTENT_TYPE, "application/json")
.bearer_auth(token)
.body(request_body)
// .timeout(self.send_timeout) // TODO: send_timeout
.timeout(self.send_timeout)
.send()
.await?;

Expand Down Expand Up @@ -76,7 +86,7 @@ impl Api {
.client
.get(url)
.header(CONTENT_TYPE, "application/json")
// .timeout(self.send_timeout) // TODO: send_timeout
.timeout(self.send_timeout)
.send()
.await?;

Expand Down
2 changes: 1 addition & 1 deletion src/coordinator_handler/coordinator_client.rs
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ impl CoordinatorClient {
let rt = tokio::runtime::Builder::new_current_thread()
.enable_all()
.build()?;
let api = Api::new(&cfg.base_url)?; // TODO: retry policy
let api = Api::new(cfg)?;
let client = Self {
circuit_type,
vks,
Expand Down

0 comments on commit ba26e21

Please sign in to comment.