mirror of https://github.com/fafhrd91/actix-web
remove mutable borrow of service state from actix_http::client and awc
This commit is contained in:
parent
5f2e383436
commit
5e2d776a8f
|
@ -46,7 +46,7 @@ impl From<Authority> for Key {
|
||||||
}
|
}
|
||||||
|
|
||||||
/// Connections pool
|
/// 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>
|
impl<T, Io> ConnectionPool<T, Io>
|
||||||
where
|
where
|
||||||
|
@ -54,7 +54,7 @@ where
|
||||||
T: Service<Connect, Response = (Io, Protocol), Error = ConnectError> + 'static,
|
T: Service<Connect, Response = (Io, Protocol), Error = ConnectError> + 'static,
|
||||||
{
|
{
|
||||||
pub(crate) fn new(connector: T, config: ConnectorConfig) -> Self {
|
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 {
|
let inner_rc = Rc::new(RefCell::new(Inner {
|
||||||
config,
|
config,
|
||||||
acquired: 0,
|
acquired: 0,
|
||||||
|
@ -429,7 +429,7 @@ struct ConnectorPoolSupport<T, Io>
|
||||||
where
|
where
|
||||||
Io: AsyncRead + AsyncWrite + Unpin + 'static,
|
Io: AsyncRead + AsyncWrite + Unpin + 'static,
|
||||||
{
|
{
|
||||||
connector: T,
|
connector: Rc<T>,
|
||||||
inner: Rc<RefCell<Inner<Io>>>,
|
inner: Rc<RefCell<Inner<Io>>>,
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -1,4 +1,3 @@
|
||||||
use std::cell::RefCell;
|
|
||||||
use std::convert::TryFrom;
|
use std::convert::TryFrom;
|
||||||
use std::fmt;
|
use std::fmt;
|
||||||
use std::rc::Rc;
|
use std::rc::Rc;
|
||||||
|
@ -24,7 +23,7 @@ pub struct ClientBuilder {
|
||||||
conn_window_size: Option<u32>,
|
conn_window_size: Option<u32>,
|
||||||
headers: HeaderMap,
|
headers: HeaderMap,
|
||||||
timeout: Option<Duration>,
|
timeout: Option<Duration>,
|
||||||
connector: Option<RefCell<Box<dyn Connect>>>,
|
connector: Option<Box<dyn Connect>>,
|
||||||
}
|
}
|
||||||
|
|
||||||
impl Default for ClientBuilder {
|
impl Default for ClientBuilder {
|
||||||
|
@ -56,7 +55,7 @@ impl ClientBuilder {
|
||||||
<T::Response as Connection>::Future: 'static,
|
<T::Response as Connection>::Future: 'static,
|
||||||
T::Future: 'static,
|
T::Future: 'static,
|
||||||
{
|
{
|
||||||
self.connector = Some(RefCell::new(Box::new(ConnectorWrapper(connector))));
|
self.connector = Some(Box::new(ConnectorWrapper(connector)));
|
||||||
self
|
self
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -182,9 +181,7 @@ 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)
|
||||||
};
|
};
|
||||||
RefCell::new(
|
Box::new(ConnectorWrapper(connector.finish())) as _
|
||||||
Box::new(ConnectorWrapper(connector.finish())) as Box<dyn Connect>
|
|
||||||
)
|
|
||||||
};
|
};
|
||||||
let config = ClientConfig {
|
let config = ClientConfig {
|
||||||
headers: self.headers,
|
headers: self.headers,
|
||||||
|
|
|
@ -20,14 +20,14 @@ pub(crate) struct ConnectorWrapper<T>(pub T);
|
||||||
|
|
||||||
pub(crate) trait Connect {
|
pub(crate) trait Connect {
|
||||||
fn send_request(
|
fn send_request(
|
||||||
&mut self,
|
&self,
|
||||||
head: RequestHead,
|
head: RequestHead,
|
||||||
body: Body,
|
body: Body,
|
||||||
addr: Option<net::SocketAddr>,
|
addr: Option<net::SocketAddr>,
|
||||||
) -> Pin<Box<dyn Future<Output = Result<ClientResponse, SendRequestError>>>>;
|
) -> Pin<Box<dyn Future<Output = Result<ClientResponse, SendRequestError>>>>;
|
||||||
|
|
||||||
fn send_request_extra(
|
fn send_request_extra(
|
||||||
&mut self,
|
&self,
|
||||||
head: Rc<RequestHead>,
|
head: Rc<RequestHead>,
|
||||||
extra_headers: Option<HeaderMap>,
|
extra_headers: Option<HeaderMap>,
|
||||||
body: Body,
|
body: Body,
|
||||||
|
@ -36,7 +36,7 @@ pub(crate) trait Connect {
|
||||||
|
|
||||||
/// Send request, returns Response and Framed
|
/// Send request, returns Response and Framed
|
||||||
fn open_tunnel(
|
fn open_tunnel(
|
||||||
&mut self,
|
&self,
|
||||||
head: RequestHead,
|
head: RequestHead,
|
||||||
addr: Option<net::SocketAddr>,
|
addr: Option<net::SocketAddr>,
|
||||||
) -> Pin<
|
) -> Pin<
|
||||||
|
@ -52,7 +52,7 @@ pub(crate) trait Connect {
|
||||||
|
|
||||||
/// Send request and extra headers, returns Response and Framed
|
/// Send request and extra headers, returns Response and Framed
|
||||||
fn open_tunnel_extra(
|
fn open_tunnel_extra(
|
||||||
&mut self,
|
&self,
|
||||||
head: Rc<RequestHead>,
|
head: Rc<RequestHead>,
|
||||||
extra_headers: Option<HeaderMap>,
|
extra_headers: Option<HeaderMap>,
|
||||||
addr: Option<net::SocketAddr>,
|
addr: Option<net::SocketAddr>,
|
||||||
|
@ -78,7 +78,7 @@ where
|
||||||
T::Future: 'static,
|
T::Future: 'static,
|
||||||
{
|
{
|
||||||
fn send_request(
|
fn send_request(
|
||||||
&mut self,
|
&self,
|
||||||
head: RequestHead,
|
head: RequestHead,
|
||||||
body: Body,
|
body: Body,
|
||||||
addr: Option<net::SocketAddr>,
|
addr: Option<net::SocketAddr>,
|
||||||
|
@ -101,7 +101,7 @@ where
|
||||||
}
|
}
|
||||||
|
|
||||||
fn send_request_extra(
|
fn send_request_extra(
|
||||||
&mut self,
|
&self,
|
||||||
head: Rc<RequestHead>,
|
head: Rc<RequestHead>,
|
||||||
extra_headers: Option<HeaderMap>,
|
extra_headers: Option<HeaderMap>,
|
||||||
body: Body,
|
body: Body,
|
||||||
|
@ -126,7 +126,7 @@ where
|
||||||
}
|
}
|
||||||
|
|
||||||
fn open_tunnel(
|
fn open_tunnel(
|
||||||
&mut self,
|
&self,
|
||||||
head: RequestHead,
|
head: RequestHead,
|
||||||
addr: Option<net::SocketAddr>,
|
addr: Option<net::SocketAddr>,
|
||||||
) -> Pin<
|
) -> Pin<
|
||||||
|
@ -158,7 +158,7 @@ where
|
||||||
}
|
}
|
||||||
|
|
||||||
fn open_tunnel_extra(
|
fn open_tunnel_extra(
|
||||||
&mut self,
|
&self,
|
||||||
head: Rc<RequestHead>,
|
head: Rc<RequestHead>,
|
||||||
extra_headers: Option<HeaderMap>,
|
extra_headers: Option<HeaderMap>,
|
||||||
addr: Option<net::SocketAddr>,
|
addr: Option<net::SocketAddr>,
|
||||||
|
|
|
@ -93,7 +93,6 @@
|
||||||
#![doc(html_logo_url = "https://actix.rs/img/logo.png")]
|
#![doc(html_logo_url = "https://actix.rs/img/logo.png")]
|
||||||
#![doc(html_favicon_url = "https://actix.rs/favicon.ico")]
|
#![doc(html_favicon_url = "https://actix.rs/favicon.ico")]
|
||||||
|
|
||||||
use std::cell::RefCell;
|
|
||||||
use std::convert::TryFrom;
|
use std::convert::TryFrom;
|
||||||
use std::rc::Rc;
|
use std::rc::Rc;
|
||||||
use std::time::Duration;
|
use std::time::Duration;
|
||||||
|
@ -145,7 +144,7 @@ use self::connect::{Connect, ConnectorWrapper};
|
||||||
pub struct Client(Rc<ClientConfig>);
|
pub struct Client(Rc<ClientConfig>);
|
||||||
|
|
||||||
pub(crate) struct ClientConfig {
|
pub(crate) struct ClientConfig {
|
||||||
pub(crate) connector: RefCell<Box<dyn Connect>>,
|
pub(crate) connector: Box<dyn Connect>,
|
||||||
pub(crate) headers: HeaderMap,
|
pub(crate) headers: HeaderMap,
|
||||||
pub(crate) timeout: Option<Duration>,
|
pub(crate) timeout: Option<Duration>,
|
||||||
}
|
}
|
||||||
|
@ -153,9 +152,7 @@ pub(crate) struct ClientConfig {
|
||||||
impl Default for Client {
|
impl Default for Client {
|
||||||
fn default() -> Self {
|
fn default() -> Self {
|
||||||
Client(Rc::new(ClientConfig {
|
Client(Rc::new(ClientConfig {
|
||||||
connector: RefCell::new(Box::new(ConnectorWrapper(
|
connector: Box::new(ConnectorWrapper(Connector::new().finish())),
|
||||||
Connector::new().finish(),
|
|
||||||
))),
|
|
||||||
headers: HeaderMap::new(),
|
headers: HeaderMap::new(),
|
||||||
timeout: Some(Duration::from_secs(5)),
|
timeout: Some(Duration::from_secs(5)),
|
||||||
}))
|
}))
|
||||||
|
|
|
@ -183,15 +183,13 @@ impl RequestSender {
|
||||||
where
|
where
|
||||||
B: Into<Body>,
|
B: Into<Body>,
|
||||||
{
|
{
|
||||||
let mut connector = config.connector.borrow_mut();
|
|
||||||
|
|
||||||
let fut = match self {
|
let fut = match self {
|
||||||
RequestSender::Owned(head) => {
|
RequestSender::Owned(head) => {
|
||||||
connector.send_request(head, body.into(), addr)
|
config.connector.send_request(head, body.into(), addr)
|
||||||
}
|
|
||||||
RequestSender::Rc(head, extra_headers) => {
|
|
||||||
connector.send_request_extra(head, extra_headers, 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))
|
SendClientRequest::new(fut, response_decompress, timeout.or(config.timeout))
|
||||||
|
|
|
@ -325,11 +325,7 @@ impl WebsocketsRequest {
|
||||||
let max_size = self.max_size;
|
let max_size = self.max_size;
|
||||||
let server_mode = self.server_mode;
|
let server_mode = self.server_mode;
|
||||||
|
|
||||||
let fut = self
|
let fut = self.config.connector.open_tunnel(head, self.addr);
|
||||||
.config
|
|
||||||
.connector
|
|
||||||
.borrow_mut()
|
|
||||||
.open_tunnel(head, self.addr);
|
|
||||||
|
|
||||||
// set request timeout
|
// set request timeout
|
||||||
let (head, framed) = if let Some(to) = self.config.timeout {
|
let (head, framed) = if let Some(to) = self.config.timeout {
|
||||||
|
|
|
@ -720,9 +720,9 @@ async fn test_client_cookie_handling() {
|
||||||
async fn client_unread_response() {
|
async fn client_unread_response() {
|
||||||
let addr = test::unused_addr();
|
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() {
|
for stream in lst.incoming() {
|
||||||
let mut stream = stream.unwrap();
|
let mut stream = stream.unwrap();
|
||||||
let mut b = [0; 1000];
|
let mut b = [0; 1000];
|
||||||
|
|
Loading…
Reference in New Issue