http: sync client: add HTTP request timeout option
This commits adds the possibility to set a HTTP request timeout for the sync client. For now, I've opted to add this as a separate option than can be set via a separate new_with_timeout method as compared to adding it to the HttpOptions struct. While it of course would make a lot of sense to add it to the latter, this would require adding support for request timeouts to the async client as well. Some users of the async client handle request timeouts externally via tokio::time::timeout, so these would need to modified as well. I don't want to touch this at the moment, so I've opted to introduce the timeout to the sync client only for now. We can always revisit this at a later time and move the option to the HttpOptions struct. Signed-off-by: Lukas Wagner <l.wagner@proxmox.com>
This commit is contained in:
parent
622e43d5c3
commit
a7c68a3166
@ -1,6 +1,7 @@
|
||||
use std::collections::HashMap;
|
||||
use std::io::Read;
|
||||
use std::sync::Arc;
|
||||
use std::time::Duration;
|
||||
|
||||
use anyhow::Error;
|
||||
use http::Response;
|
||||
@ -12,16 +13,31 @@ use crate::HttpOptions;
|
||||
/// Blocking HTTP client for usage with [`HttpClient`].
|
||||
pub struct Client {
|
||||
options: HttpOptions,
|
||||
timeout: Option<Duration>,
|
||||
}
|
||||
|
||||
impl Client {
|
||||
pub fn new(options: HttpOptions) -> Self {
|
||||
Self { options }
|
||||
Self {
|
||||
options,
|
||||
timeout: None,
|
||||
}
|
||||
}
|
||||
|
||||
pub fn new_with_timeout(options: HttpOptions, timeout: Duration) -> Self {
|
||||
Self {
|
||||
options,
|
||||
timeout: Some(timeout),
|
||||
}
|
||||
}
|
||||
|
||||
fn agent(&self) -> Result<ureq::Agent, Error> {
|
||||
let mut builder = ureq::AgentBuilder::new();
|
||||
|
||||
if let Some(timeout) = self.timeout {
|
||||
builder = builder.timeout(timeout);
|
||||
};
|
||||
|
||||
let connector = Arc::new(native_tls::TlsConnector::new()?);
|
||||
builder = builder.tls_connector(connector);
|
||||
|
||||
|
Loading…
x
Reference in New Issue
Block a user