1
0
mirror of https://github.com/fafhrd91/actix-net synced 2024-11-27 16:52:58 +01:00

use reusable box future in tls connector

This commit is contained in:
Rob Ede 2021-02-04 15:14:48 +00:00
parent 7f0eddd794
commit b5399c5631
No known key found for this signature in database
GPG Key ID: C2A3B36E841A91E6
2 changed files with 10 additions and 4 deletions

View File

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

View File

@ -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<T> {
req: Option<T>,
port: u16,
addrs: Option<VecDeque<SocketAddr>>,
stream: Option<LocalBoxFuture<'static, Result<TcpStream, io::Error>>>,
stream: Option<ReusableBoxFuture<Result<TcpStream, io::Error>>>,
},
Error(Option<ConnectError>),
}
@ -90,7 +91,7 @@ impl<T: Address> TcpConnectorResponse<T> {
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<T: Address> Future for TcpConnectorResponse<T> {
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<T: Address> Future for TcpConnectorResponse<T> {
// 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))),
}
},
}
}