From 5e2d776a8f251c82eed71196e780416b19b21516 Mon Sep 17 00:00:00 2001 From: fakeshadow <24548779@qq.com> Date: Fri, 22 Jan 2021 22:09:24 -0800 Subject: [PATCH] remove mutable borrow of service state from actix_http::client and awc --- actix-http/src/client/pool.rs | 6 +++--- awc/src/builder.rs | 9 +++------ awc/src/connect.rs | 16 ++++++++-------- awc/src/lib.rs | 7 ++----- awc/src/sender.rs | 10 ++++------ awc/src/ws.rs | 6 +----- awc/tests/test_client.rs | 4 ++-- 7 files changed, 23 insertions(+), 35 deletions(-) diff --git a/actix-http/src/client/pool.rs b/actix-http/src/client/pool.rs index 155f1b62b..51754eb54 100644 --- a/actix-http/src/client/pool.rs +++ b/actix-http/src/client/pool.rs @@ -46,7 +46,7 @@ impl From for Key { } /// Connections pool -pub(crate) struct ConnectionPool(Rc>, Rc>>); +pub(crate) struct ConnectionPool(Rc, Rc>>); impl ConnectionPool where @@ -54,7 +54,7 @@ where T: Service + 'static, { pub(crate) fn new(connector: T, config: ConnectorConfig) -> Self { - let connector_rc = Rc::new(RefCell::new(connector)); + let connector_rc = Rc::new(connector); let inner_rc = Rc::new(RefCell::new(Inner { config, acquired: 0, @@ -429,7 +429,7 @@ struct ConnectorPoolSupport where Io: AsyncRead + AsyncWrite + Unpin + 'static, { - connector: T, + connector: Rc, inner: Rc>>, } diff --git a/awc/src/builder.rs b/awc/src/builder.rs index 351a493af..39eb24c39 100644 --- a/awc/src/builder.rs +++ b/awc/src/builder.rs @@ -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, headers: HeaderMap, timeout: Option, - connector: Option>>, + connector: Option>, } impl Default for ClientBuilder { @@ -56,7 +55,7 @@ impl ClientBuilder { ::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 - ) + Box::new(ConnectorWrapper(connector.finish())) as _ }; let config = ClientConfig { headers: self.headers, diff --git a/awc/src/connect.rs b/awc/src/connect.rs index 8ee239f76..2ffb8ec37 100644 --- a/awc/src/connect.rs +++ b/awc/src/connect.rs @@ -20,14 +20,14 @@ pub(crate) struct ConnectorWrapper(pub T); pub(crate) trait Connect { fn send_request( - &mut self, + &self, head: RequestHead, body: Body, addr: Option, ) -> Pin>>>; fn send_request_extra( - &mut self, + &self, head: Rc, extra_headers: Option, 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, ) -> 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, extra_headers: Option, addr: Option, @@ -78,7 +78,7 @@ where T::Future: 'static, { fn send_request( - &mut self, + &self, head: RequestHead, body: Body, addr: Option, @@ -101,7 +101,7 @@ where } fn send_request_extra( - &mut self, + &self, head: Rc, extra_headers: Option, body: Body, @@ -126,7 +126,7 @@ where } fn open_tunnel( - &mut self, + &self, head: RequestHead, addr: Option, ) -> Pin< @@ -158,7 +158,7 @@ where } fn open_tunnel_extra( - &mut self, + &self, head: Rc, extra_headers: Option, addr: Option, diff --git a/awc/src/lib.rs b/awc/src/lib.rs index f750a3df2..a92125b5a 100644 --- a/awc/src/lib.rs +++ b/awc/src/lib.rs @@ -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); pub(crate) struct ClientConfig { - pub(crate) connector: RefCell>, + pub(crate) connector: Box, pub(crate) headers: HeaderMap, pub(crate) timeout: Option, } @@ -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)), })) diff --git a/awc/src/sender.rs b/awc/src/sender.rs index 9fb821a0e..5f790a038 100644 --- a/awc/src/sender.rs +++ b/awc/src/sender.rs @@ -183,15 +183,13 @@ impl RequestSender { where B: Into, { - 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)) diff --git a/awc/src/ws.rs b/awc/src/ws.rs index 17086cf2a..7c795226b 100644 --- a/awc/src/ws.rs +++ b/awc/src/ws.rs @@ -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 { diff --git a/awc/tests/test_client.rs b/awc/tests/test_client.rs index 23ecda173..a9f5dc370 100644 --- a/awc/tests/test_client.rs +++ b/awc/tests/test_client.rs @@ -720,9 +720,9 @@ async fn test_client_cookie_handling() { async fn client_unread_response() { let addr = test::unused_addr(); - std::thread::spawn(move || { - let lst = std::net::TcpListener::bind(addr).unwrap(); + let lst = std::net::TcpListener::bind(addr).unwrap(); + std::thread::spawn(move || { for stream in lst.incoming() { let mut stream = stream.unwrap(); let mut b = [0; 1000];