1
0
Fork 0
mirror of https://github.com/actix/actix-web.git synced 2024-11-29 21:11:17 +00:00

refactor awc::ClientBuilder (#2008)

This commit is contained in:
fakeshadow 2021-02-18 04:30:09 -08:00 committed by GitHub
parent 1838d9cd0f
commit 946cccaa1a
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
11 changed files with 91 additions and 73 deletions

View file

@ -96,14 +96,12 @@ pub async fn test_server_with_addr<F: ServiceFactory<TcpStream>>(
.conn_lifetime(time::Duration::from_secs(0)) .conn_lifetime(time::Duration::from_secs(0))
.timeout(time::Duration::from_millis(30000)) .timeout(time::Duration::from_millis(30000))
.ssl(builder.build()) .ssl(builder.build())
.finish()
} }
#[cfg(not(feature = "openssl"))] #[cfg(not(feature = "openssl"))]
{ {
Connector::new() Connector::new()
.conn_lifetime(time::Duration::from_secs(0)) .conn_lifetime(time::Duration::from_secs(0))
.timeout(time::Duration::from_millis(30000)) .timeout(time::Duration::from_millis(30000))
.finish()
} }
}; };

View file

@ -10,6 +10,10 @@ mod h1proto;
mod h2proto; mod h2proto;
mod pool; mod pool;
pub use actix_tls::connect::{
Connect as TcpConnect, ConnectError as TcpConnectError, Connection as TcpConnection,
};
pub use self::connection::Connection; pub use self::connection::Connection;
pub use self::connector::Connector; pub use self::connector::Connector;
pub use self::error::{ConnectError, FreezeRequestError, InvalidUrl, SendRequestError}; pub use self::error::{ConnectError, FreezeRequestError, InvalidUrl, SendRequestError};

View file

@ -6,10 +6,15 @@
### Changed ### Changed
* Feature `cookies` is now optional and enabled by default. [#1981] * Feature `cookies` is now optional and enabled by default. [#1981]
* `ClientBuilder::connector` method would take `actix_http::client::Connector<T, U>` type. [#2008]
### Removed
* `ClientBuilder::default` function [#2008]
* `ClientBuilder::disable_redirects` and `ClientBuilder::max_redirects` method [#2008]
[#1931]: https://github.com/actix/actix-web/pull/1931 [#1931]: https://github.com/actix/actix-web/pull/1931
[#1981]: https://github.com/actix/actix-web/pull/1981 [#1981]: https://github.com/actix/actix-web/pull/1981
[#2008]: https://github.com/actix/actix-web/pull/2008
## 3.0.0-beta.2 - 2021-02-10 ## 3.0.0-beta.2 - 2021-02-10
### Added ### Added

View file

@ -3,59 +3,80 @@ use std::fmt;
use std::rc::Rc; use std::rc::Rc;
use std::time::Duration; use std::time::Duration;
use actix_http::client::{Connect as HttpConnect, ConnectError, Connection, Connector}; use actix_codec::{AsyncRead, AsyncWrite};
use actix_http::http::{self, header, Error as HttpError, HeaderMap, HeaderName}; use actix_http::{
client::{Connector, TcpConnect, TcpConnectError, TcpConnection},
http::{self, header, Error as HttpError, HeaderMap, HeaderName, Uri},
};
use actix_rt::net::TcpStream;
use actix_service::Service; use actix_service::Service;
use crate::connect::{ConnectService, ConnectorWrapper}; use crate::connect::ConnectorWrapper;
use crate::{Client, ClientConfig}; use crate::{Client, ClientConfig};
/// An HTTP Client builder /// An HTTP Client builder
/// ///
/// This type can be used to construct an instance of `Client` through a /// This type can be used to construct an instance of `Client` through a
/// builder-like pattern. /// builder-like pattern.
pub struct ClientBuilder { pub struct ClientBuilder<T = (), U = ()> {
default_headers: bool, default_headers: bool,
allow_redirects: bool,
max_redirects: usize,
max_http_version: Option<http::Version>, max_http_version: Option<http::Version>,
stream_window_size: Option<u32>, stream_window_size: Option<u32>,
conn_window_size: Option<u32>, conn_window_size: Option<u32>,
headers: HeaderMap, headers: HeaderMap,
timeout: Option<Duration>, timeout: Option<Duration>,
connector: Option<ConnectService>, connector: Connector<T, U>,
}
impl Default for ClientBuilder {
fn default() -> Self {
Self::new()
}
} }
impl ClientBuilder { impl ClientBuilder {
pub fn new() -> Self { #[allow(clippy::new_ret_no_self)]
pub fn new() -> ClientBuilder<
impl Service<
TcpConnect<Uri>,
Response = TcpConnection<Uri, TcpStream>,
Error = TcpConnectError,
> + Clone,
TcpStream,
> {
ClientBuilder { ClientBuilder {
default_headers: true, default_headers: true,
allow_redirects: true,
max_redirects: 10,
headers: HeaderMap::new(), headers: HeaderMap::new(),
timeout: Some(Duration::from_secs(5)), timeout: Some(Duration::from_secs(5)),
connector: None, connector: Connector::new(),
max_http_version: None, max_http_version: None,
stream_window_size: None, stream_window_size: None,
conn_window_size: None, conn_window_size: None,
} }
} }
}
/// Use custom connector service. impl<S, Io> ClientBuilder<S, Io>
pub fn connector<T>(mut self, connector: T) -> Self
where where
T: Service<HttpConnect, Error = ConnectError> + 'static, S: Service<TcpConnect<Uri>, Response = TcpConnection<Uri, Io>, Error = TcpConnectError>
T::Response: Connection, + Clone
T::Future: 'static, + 'static,
Io: AsyncRead + AsyncWrite + Unpin + fmt::Debug + 'static,
{ {
self.connector = Some(Box::new(ConnectorWrapper::new(connector))); /// Use custom connector service.
self pub fn connector<S1, Io1>(self, connector: Connector<S1, Io1>) -> ClientBuilder<S1, Io1>
where
S1: Service<
TcpConnect<Uri>,
Response = TcpConnection<Uri, Io1>,
Error = TcpConnectError,
> + Clone
+ 'static,
Io1: AsyncRead + AsyncWrite + Unpin + fmt::Debug + 'static,
{
ClientBuilder {
default_headers: self.default_headers,
headers: self.headers,
timeout: self.timeout,
connector,
max_http_version: self.max_http_version,
stream_window_size: self.stream_window_size,
conn_window_size: self.conn_window_size,
}
} }
/// Set request timeout /// Set request timeout
@ -73,14 +94,6 @@ impl ClientBuilder {
self self
} }
/// Do not follow redirects.
///
/// Redirects are allowed by default.
pub fn disable_redirects(mut self) -> Self {
self.allow_redirects = false;
self
}
/// Maximum supported HTTP major version. /// Maximum supported HTTP major version.
/// ///
/// Supported versions are HTTP/1.1 and HTTP/2. /// Supported versions are HTTP/1.1 and HTTP/2.
@ -107,14 +120,6 @@ impl ClientBuilder {
self self
} }
/// Set max number of redirects.
///
/// Max redirects is set to 10 by default.
pub fn max_redirects(mut self, num: usize) -> Self {
self.max_redirects = num;
self
}
/// Do not add default request headers. /// Do not add default request headers.
/// By default `Date` and `User-Agent` headers are set. /// By default `Date` and `User-Agent` headers are set.
pub fn no_default_headers(mut self) -> Self { pub fn no_default_headers(mut self) -> Self {
@ -144,9 +149,9 @@ impl ClientBuilder {
} }
/// Set client wide HTTP basic authorization header /// Set client wide HTTP basic authorization header
pub fn basic_auth<U>(self, username: U, password: Option<&str>) -> Self pub fn basic_auth<N>(self, username: N, password: Option<&str>) -> Self
where where
U: fmt::Display, N: fmt::Display,
{ {
let auth = match password { let auth = match password {
Some(password) => format!("{}:{}", username, password), Some(password) => format!("{}:{}", username, password),
@ -168,12 +173,10 @@ impl ClientBuilder {
/// Finish build process and create `Client` instance. /// Finish build process and create `Client` instance.
pub fn finish(self) -> Client { pub fn finish(self) -> Client {
let connector = if let Some(connector) = self.connector { let mut connector = self.connector;
connector
} else {
let mut connector = Connector::new();
if let Some(val) = self.max_http_version { if let Some(val) = self.max_http_version {
connector = connector.max_http_version(val) connector = connector.max_http_version(val);
}; };
if let Some(val) = self.conn_window_size { if let Some(val) = self.conn_window_size {
connector = connector.initial_connection_window_size(val) connector = connector.initial_connection_window_size(val)
@ -181,13 +184,13 @@ impl ClientBuilder {
if let Some(val) = self.stream_window_size { if let Some(val) = self.stream_window_size {
connector = connector.initial_window_size(val) connector = connector.initial_window_size(val)
}; };
Box::new(ConnectorWrapper::new(connector.finish())) as _
};
let config = ClientConfig { let config = ClientConfig {
headers: self.headers, headers: self.headers,
timeout: self.timeout, timeout: self.timeout,
connector, connector: Box::new(ConnectorWrapper::new(connector.finish())) as _,
}; };
Client(Rc::new(config)) Client(Rc::new(config))
} }
} }

View file

@ -101,8 +101,13 @@ use std::time::Duration;
pub use actix_http::cookie; pub use actix_http::cookie;
pub use actix_http::{client::Connector, http}; pub use actix_http::{client::Connector, http};
use actix_http::http::{Error as HttpError, HeaderMap, Method, Uri}; use actix_http::{
use actix_http::RequestHead; client::{TcpConnect, TcpConnectError, TcpConnection},
http::{Error as HttpError, HeaderMap, Method, Uri},
RequestHead,
};
use actix_rt::net::TcpStream;
use actix_service::Service;
mod builder; mod builder;
mod connect; mod connect;
@ -169,7 +174,14 @@ impl Client {
/// Create `Client` builder. /// Create `Client` builder.
/// This function is equivalent of `ClientBuilder::new()`. /// This function is equivalent of `ClientBuilder::new()`.
pub fn builder() -> ClientBuilder { pub fn builder() -> ClientBuilder<
impl Service<
TcpConnect<Uri>,
Response = TcpConnection<Uri, TcpStream>,
Error = TcpConnectError,
> + Clone,
TcpStream,
> {
ClientBuilder::new() ClientBuilder::new()
} }

View file

@ -120,8 +120,7 @@ async fn test_timeout() {
let connector = awc::Connector::new() let connector = awc::Connector::new()
.connector(actix_tls::connect::default_connector()) .connector(actix_tls::connect::default_connector())
.timeout(Duration::from_secs(15)) .timeout(Duration::from_secs(15));
.finish();
let client = awc::Client::builder() let client = awc::Client::builder()
.connector(connector) .connector(connector)
@ -368,7 +367,7 @@ async fn test_connection_wait_queue() {
.await; .await;
let client = awc::Client::builder() let client = awc::Client::builder()
.connector(awc::Connector::new().limit(1).finish()) .connector(awc::Connector::new().limit(1))
.finish(); .finish();
// req 1 // req 1
@ -417,7 +416,7 @@ async fn test_connection_wait_queue_force_close() {
.await; .await;
let client = awc::Client::builder() let client = awc::Client::builder()
.connector(awc::Connector::new().limit(1).finish()) .connector(awc::Connector::new().limit(1))
.finish(); .finish();
// req 1 // req 1

View file

@ -58,7 +58,7 @@ async fn test_connection_window_size() {
.map_err(|e| log::error!("Can not set alpn protocol: {:?}", e)); .map_err(|e| log::error!("Can not set alpn protocol: {:?}", e));
let client = awc::Client::builder() let client = awc::Client::builder()
.connector(awc::Connector::new().ssl(builder.build()).finish()) .connector(awc::Connector::new().ssl(builder.build()))
.initial_window_size(100) .initial_window_size(100)
.initial_connection_window_size(100) .initial_connection_window_size(100)
.finish(); .finish();

View file

@ -82,7 +82,7 @@ async fn test_connection_reuse_h2() {
.set_certificate_verifier(Arc::new(danger::NoCertificateVerification)); .set_certificate_verifier(Arc::new(danger::NoCertificateVerification));
let client = awc::Client::builder() let client = awc::Client::builder()
.connector(awc::Connector::new().rustls(Arc::new(config)).finish()) .connector(awc::Connector::new().rustls(Arc::new(config)))
.finish(); .finish();
// req 1 // req 1

View file

@ -72,7 +72,7 @@ async fn test_connection_reuse_h2() {
.map_err(|e| log::error!("Can not set alpn protocol: {:?}", e)); .map_err(|e| log::error!("Can not set alpn protocol: {:?}", e));
let client = awc::Client::builder() let client = awc::Client::builder()
.connector(awc::Connector::new().ssl(builder.build()).finish()) .connector(awc::Connector::new().ssl(builder.build()))
.finish(); .finish();
// req 1 // req 1

View file

@ -761,14 +761,12 @@ where
.conn_lifetime(time::Duration::from_secs(0)) .conn_lifetime(time::Duration::from_secs(0))
.timeout(time::Duration::from_millis(30000)) .timeout(time::Duration::from_millis(30000))
.ssl(builder.build()) .ssl(builder.build())
.finish()
} }
#[cfg(not(feature = "openssl"))] #[cfg(not(feature = "openssl"))]
{ {
Connector::new() Connector::new()
.conn_lifetime(time::Duration::from_secs(0)) .conn_lifetime(time::Duration::from_secs(0))
.timeout(time::Duration::from_millis(30000)) .timeout(time::Duration::from_millis(30000))
.finish()
} }
}; };

View file

@ -137,8 +137,7 @@ async fn test_start_ssl() {
.connector( .connector(
awc::Connector::new() awc::Connector::new()
.ssl(builder.build()) .ssl(builder.build())
.timeout(Duration::from_millis(100)) .timeout(Duration::from_millis(100)),
.finish(),
) )
.finish(); .finish();