Skip to content

Commit

Permalink
feat(client): allow to set read_timeout and connect_timeout
Browse files Browse the repository at this point in the history
Signed-off-by: Benjamin Neff <benjamin@coding4coffee.ch>
  • Loading branch information
SuperTux88 committed Jun 14, 2024
1 parent f19660a commit 6bc6d18
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 1 deletion.
2 changes: 1 addition & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ jwt = "0.16"
lazy_static = "1.4"
olpc-cjson = "0.1"
regex = "1.6"
reqwest = { version = "0.12", default-features = false, features = [
reqwest = { version = "0.12.4", default-features = false, features = [
"json",
"stream",
] }
Expand Down
20 changes: 20 additions & 0 deletions src/client.rs
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@ use std::collections::{BTreeMap, HashMap};
use std::convert::TryFrom;
use std::hash::Hash;
use std::sync::Arc;
use std::time::Duration;
use tokio::io::{AsyncWrite, AsyncWriteExt};
use tokio::sync::RwLock;
use tracing::{debug, trace, warn};
Expand Down Expand Up @@ -304,6 +305,13 @@ impl TryFrom<ClientConfig> for Client {
client_builder = client_builder.add_root_certificate(cert);
}

if let Some(timeout) = config.read_timeout {
client_builder = client_builder.read_timeout(timeout);
}
if let Some(timeout) = config.connect_timeout {
client_builder = client_builder.connect_timeout(timeout);
}

let default_token_expiration_secs = config.default_token_expiration_secs;
Ok(Self {
config: Arc::new(config),
Expand Down Expand Up @@ -1661,6 +1669,16 @@ pub struct ClientConfig {
///
/// This defaults to [`DEFAULT_TOKEN_EXPIRATION_SECS`].
pub default_token_expiration_secs: usize,

/// Enables a read timeout for the client.
///
/// See [`reqwest::ClientBuilder::read_timeout`] for more information.
pub read_timeout: Option<Duration>,

/// Set a timeout for the connect phase for the client.
///
/// See [`reqwest::ClientBuilder::connect_timeout`] for more information.
pub connect_timeout: Option<Duration>,
}

impl Default for ClientConfig {
Expand All @@ -1675,6 +1693,8 @@ impl Default for ClientConfig {
max_concurrent_upload: DEFAULT_MAX_CONCURRENT_UPLOAD,
max_concurrent_download: DEFAULT_MAX_CONCURRENT_DOWNLOAD,
default_token_expiration_secs: DEFAULT_TOKEN_EXPIRATION_SECS,
read_timeout: None,
connect_timeout: None,
}
}
}
Expand Down

0 comments on commit 6bc6d18

Please sign in to comment.