remove mutable borrow of service state from actix_http::client and awc

This commit is contained in:
fakeshadow 2021-01-22 22:09:24 -08:00
parent 5f2e383436
commit 5e2d776a8f
7 changed files with 23 additions and 35 deletions

View File

@ -46,7 +46,7 @@ impl From<Authority> for Key {
}
/// Connections pool
pub(crate) struct ConnectionPool<T, Io: 'static>(Rc<RefCell<T>>, Rc<RefCell<Inner<Io>>>);
pub(crate) struct ConnectionPool<T, Io: 'static>(Rc<T>, Rc<RefCell<Inner<Io>>>);
impl<T, Io> ConnectionPool<T, Io>
where
@ -54,7 +54,7 @@ where
T: Service<Connect, Response = (Io, Protocol), Error = ConnectError> + '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<T, Io>
where
Io: AsyncRead + AsyncWrite + Unpin + 'static,
{
connector: T,
connector: Rc<T>,
inner: Rc<RefCell<Inner<Io>>>,
}

View File

@ -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,

View File

@ -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>,

View File

@ -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)),
}))

View File

@ -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))

View File

@ -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 {

View File

@ -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();
std::thread::spawn(move || {
for stream in lst.incoming() {
let mut stream = stream.unwrap();
let mut b = [0; 1000];