diff --git a/actix-tls/Cargo.toml b/actix-tls/Cargo.toml index acdd0419..aa5f0c89 100755 --- a/actix-tls/Cargo.toml +++ b/actix-tls/Cargo.toml @@ -49,6 +49,7 @@ derive_more = "0.99.5" futures-core = { version = "0.3.7", default-features = false, features = ["alloc"] } http = { version = "0.2.3", optional = true } log = "0.4" +tokio-util = { version = "0.6.3", default-features = false } # openssl tls-openssl = { package = "openssl", version = "0.10", optional = true } diff --git a/actix-tls/src/connect/connector.rs b/actix-tls/src/connect/connector.rs index 5284eff4..9acb1dd5 100755 --- a/actix-tls/src/connect/connector.rs +++ b/actix-tls/src/connect/connector.rs @@ -11,6 +11,7 @@ use actix_rt::net::TcpStream; use actix_service::{Service, ServiceFactory}; use futures_core::{future::LocalBoxFuture, ready}; use log::{error, trace}; +use tokio_util::sync::ReusableBoxFuture; use super::connect::{Address, Connect, ConnectAddrs, Connection}; use super::error::ConnectError; @@ -65,7 +66,7 @@ pub enum TcpConnectorResponse { req: Option, port: u16, addrs: Option>, - stream: Option>>, + stream: Option>>, }, Error(Option), } @@ -90,7 +91,7 @@ impl TcpConnectorResponse { req: Some(req), port, addrs: None, - stream: Some(Box::pin(TcpStream::connect(addr))), + stream: Some(ReusableBoxFuture::new(TcpStream::connect(addr))), }, // when resolver returns multiple socket addr for request they would be popped from @@ -119,7 +120,7 @@ impl Future for TcpConnectorResponse { stream, } => loop { if let Some(new) = stream.as_mut() { - match ready!(new.as_mut().poll(cx)) { + match ready!(new.poll(cx)) { Ok(sock) => { let req = req.take().unwrap(); trace!( @@ -146,7 +147,11 @@ impl Future for TcpConnectorResponse { // try to connect let addr = addrs.as_mut().unwrap().pop_front().unwrap(); - *stream = Some(Box::pin(TcpStream::connect(addr))); + + match stream { + Some(rbf) => rbf.set(TcpStream::connect(addr)), + None => *stream = Some(ReusableBoxFuture::new(TcpStream::connect(addr))), + } }, } }