mirror of
https://github.com/fafhrd91/actix-web
synced 2025-06-26 23:17:42 +02:00
Use immutable reference of service state. Update awc dns resolver. (#1905)
This commit is contained in:
@ -1,4 +1,3 @@
|
||||
use std::cell::RefCell;
|
||||
use std::convert::TryFrom;
|
||||
use std::fmt;
|
||||
use std::rc::Rc;
|
||||
@ -24,7 +23,7 @@ pub struct ClientBuilder {
|
||||
conn_window_size: Option<u32>,
|
||||
headers: HeaderMap,
|
||||
timeout: Option<Duration>,
|
||||
connector: Option<RefCell<Box<dyn Connect>>>,
|
||||
connector: Option<Box<dyn Connect>>,
|
||||
}
|
||||
|
||||
impl Default for ClientBuilder {
|
||||
@ -56,7 +55,7 @@ impl ClientBuilder {
|
||||
<T::Response as Connection>::Future: 'static,
|
||||
T::Future: 'static,
|
||||
{
|
||||
self.connector = Some(RefCell::new(Box::new(ConnectorWrapper(connector))));
|
||||
self.connector = Some(Box::new(ConnectorWrapper(connector)));
|
||||
self
|
||||
}
|
||||
|
||||
@ -182,9 +181,7 @@ impl ClientBuilder {
|
||||
if let Some(val) = self.stream_window_size {
|
||||
connector = connector.initial_window_size(val)
|
||||
};
|
||||
RefCell::new(
|
||||
Box::new(ConnectorWrapper(connector.finish())) as Box<dyn Connect>
|
||||
)
|
||||
Box::new(ConnectorWrapper(connector.finish())) as _
|
||||
};
|
||||
let config = ClientConfig {
|
||||
headers: self.headers,
|
||||
|
@ -20,14 +20,14 @@ pub(crate) struct ConnectorWrapper<T>(pub T);
|
||||
|
||||
pub(crate) trait Connect {
|
||||
fn send_request(
|
||||
&mut self,
|
||||
&self,
|
||||
head: RequestHead,
|
||||
body: Body,
|
||||
addr: Option<net::SocketAddr>,
|
||||
) -> Pin<Box<dyn Future<Output = Result<ClientResponse, SendRequestError>>>>;
|
||||
|
||||
fn send_request_extra(
|
||||
&mut self,
|
||||
&self,
|
||||
head: Rc<RequestHead>,
|
||||
extra_headers: Option<HeaderMap>,
|
||||
body: Body,
|
||||
@ -36,7 +36,7 @@ pub(crate) trait Connect {
|
||||
|
||||
/// Send request, returns Response and Framed
|
||||
fn open_tunnel(
|
||||
&mut self,
|
||||
&self,
|
||||
head: RequestHead,
|
||||
addr: Option<net::SocketAddr>,
|
||||
) -> Pin<
|
||||
@ -52,7 +52,7 @@ pub(crate) trait Connect {
|
||||
|
||||
/// Send request and extra headers, returns Response and Framed
|
||||
fn open_tunnel_extra(
|
||||
&mut self,
|
||||
&self,
|
||||
head: Rc<RequestHead>,
|
||||
extra_headers: Option<HeaderMap>,
|
||||
addr: Option<net::SocketAddr>,
|
||||
@ -78,7 +78,7 @@ where
|
||||
T::Future: 'static,
|
||||
{
|
||||
fn send_request(
|
||||
&mut self,
|
||||
&self,
|
||||
head: RequestHead,
|
||||
body: Body,
|
||||
addr: Option<net::SocketAddr>,
|
||||
@ -101,7 +101,7 @@ where
|
||||
}
|
||||
|
||||
fn send_request_extra(
|
||||
&mut self,
|
||||
&self,
|
||||
head: Rc<RequestHead>,
|
||||
extra_headers: Option<HeaderMap>,
|
||||
body: Body,
|
||||
@ -126,7 +126,7 @@ where
|
||||
}
|
||||
|
||||
fn open_tunnel(
|
||||
&mut self,
|
||||
&self,
|
||||
head: RequestHead,
|
||||
addr: Option<net::SocketAddr>,
|
||||
) -> Pin<
|
||||
@ -158,7 +158,7 @@ where
|
||||
}
|
||||
|
||||
fn open_tunnel_extra(
|
||||
&mut self,
|
||||
&self,
|
||||
head: Rc<RequestHead>,
|
||||
extra_headers: Option<HeaderMap>,
|
||||
addr: Option<net::SocketAddr>,
|
||||
|
@ -93,7 +93,6 @@
|
||||
#![doc(html_logo_url = "https://actix.rs/img/logo.png")]
|
||||
#![doc(html_favicon_url = "https://actix.rs/favicon.ico")]
|
||||
|
||||
use std::cell::RefCell;
|
||||
use std::convert::TryFrom;
|
||||
use std::rc::Rc;
|
||||
use std::time::Duration;
|
||||
@ -145,7 +144,7 @@ use self::connect::{Connect, ConnectorWrapper};
|
||||
pub struct Client(Rc<ClientConfig>);
|
||||
|
||||
pub(crate) struct ClientConfig {
|
||||
pub(crate) connector: RefCell<Box<dyn Connect>>,
|
||||
pub(crate) connector: Box<dyn Connect>,
|
||||
pub(crate) headers: HeaderMap,
|
||||
pub(crate) timeout: Option<Duration>,
|
||||
}
|
||||
@ -153,9 +152,7 @@ pub(crate) struct ClientConfig {
|
||||
impl Default for Client {
|
||||
fn default() -> Self {
|
||||
Client(Rc::new(ClientConfig {
|
||||
connector: RefCell::new(Box::new(ConnectorWrapper(
|
||||
Connector::new().finish(),
|
||||
))),
|
||||
connector: Box::new(ConnectorWrapper(Connector::new().finish())),
|
||||
headers: HeaderMap::new(),
|
||||
timeout: Some(Duration::from_secs(5)),
|
||||
}))
|
||||
|
@ -183,15 +183,13 @@ impl RequestSender {
|
||||
where
|
||||
B: Into<Body>,
|
||||
{
|
||||
let mut connector = config.connector.borrow_mut();
|
||||
|
||||
let fut = match self {
|
||||
RequestSender::Owned(head) => {
|
||||
connector.send_request(head, body.into(), addr)
|
||||
}
|
||||
RequestSender::Rc(head, extra_headers) => {
|
||||
connector.send_request_extra(head, extra_headers, body.into(), addr)
|
||||
config.connector.send_request(head, body.into(), addr)
|
||||
}
|
||||
RequestSender::Rc(head, extra_headers) => config
|
||||
.connector
|
||||
.send_request_extra(head, extra_headers, body.into(), addr),
|
||||
};
|
||||
|
||||
SendClientRequest::new(fut, response_decompress, timeout.or(config.timeout))
|
||||
|
@ -325,11 +325,7 @@ impl WebsocketsRequest {
|
||||
let max_size = self.max_size;
|
||||
let server_mode = self.server_mode;
|
||||
|
||||
let fut = self
|
||||
.config
|
||||
.connector
|
||||
.borrow_mut()
|
||||
.open_tunnel(head, self.addr);
|
||||
let fut = self.config.connector.open_tunnel(head, self.addr);
|
||||
|
||||
// set request timeout
|
||||
let (head, framed) = if let Some(to) = self.config.timeout {
|
||||
|
Reference in New Issue
Block a user