1
0
mirror of https://github.com/fafhrd91/actix-web synced 2025-08-23 05:55:13 +02:00

add request timeout

This commit is contained in:
Nikolay Kim
2019-03-28 22:33:41 -07:00
parent ea4d98d669
commit 1e7096a63a
8 changed files with 119 additions and 50 deletions

View File

@@ -22,6 +22,7 @@
//! ```
use std::cell::RefCell;
use std::rc::Rc;
use std::time::Duration;
pub use actix_http::{client::Connector, http};
@@ -66,25 +67,23 @@ use self::connect::{Connect, ConnectorWrapper};
/// }
/// ```
#[derive(Clone)]
pub struct Client {
pub(crate) connector: Rc<RefCell<dyn Connect>>,
pub(crate) config: Rc<ClientConfig>,
}
pub struct Client(Rc<ClientConfig>);
pub(crate) struct ClientConfig {
pub(crate) connector: RefCell<Box<dyn Connect>>,
pub(crate) headers: HeaderMap,
pub(crate) timeout: Option<Duration>,
}
impl Default for Client {
fn default() -> Self {
Client {
connector: Rc::new(RefCell::new(ConnectorWrapper(
Client(Rc::new(ClientConfig {
connector: RefCell::new(Box::new(ConnectorWrapper(
Connector::new().service(),
))),
config: Rc::new(ClientConfig {
headers: HeaderMap::new(),
}),
}
headers: HeaderMap::new(),
timeout: Some(Duration::from_secs(5)),
}))
}
}
@@ -104,9 +103,9 @@ impl Client {
where
Uri: HttpTryFrom<U>,
{
let mut req = ClientRequest::new(method, url, self.connector.clone());
let mut req = ClientRequest::new(method, url, self.0.clone());
for (key, value) in &self.config.headers {
for (key, value) in &self.0.headers {
req.head.headers.insert(key.clone(), value.clone());
}
req
@@ -180,9 +179,8 @@ impl Client {
where
Uri: HttpTryFrom<U>,
{
let mut req = WebsocketsRequest::new(url, self.connector.clone());
for (key, value) in &self.config.headers {
let mut req = WebsocketsRequest::new(url, self.0.clone());
for (key, value) in &self.0.headers {
req.head.headers.insert(key.clone(), value.clone());
}
req