1
0
mirror of https://github.com/fafhrd91/actix-net synced 2025-06-29 20:44:59 +02:00

fix actix-tls tests (#241)

This commit is contained in:
fakeshadow
2020-12-29 19:36:17 +08:00
committed by GitHub
parent 0934078947
commit 03eb96d6d4
12 changed files with 291 additions and 207 deletions

View File

@ -1,10 +1,9 @@
use std::marker::PhantomData;
use std::task::{Context, Poll};
use actix_codec::{AsyncRead, AsyncWrite};
use actix_service::{Service, ServiceFactory};
use actix_utils::counter::Counter;
use futures_util::future::{self, FutureExt, LocalBoxFuture, TryFutureExt};
use futures_util::future::{ready, LocalBoxFuture, Ready};
pub use native_tls::Error;
pub use tokio_native_tls::{TlsAcceptor, TlsStream};
@ -14,75 +13,64 @@ use super::MAX_CONN_COUNTER;
/// Accept TLS connections via `native-tls` package.
///
/// `native-tls` feature enables this `Acceptor` type.
pub struct Acceptor<T> {
pub struct Acceptor {
acceptor: TlsAcceptor,
io: PhantomData<T>,
}
impl<T> Acceptor<T>
where
T: AsyncRead + AsyncWrite + Unpin,
{
impl Acceptor {
/// Create `native-tls` based `Acceptor` service factory.
#[inline]
pub fn new(acceptor: TlsAcceptor) -> Self {
Acceptor {
acceptor,
io: PhantomData,
}
Acceptor { acceptor }
}
}
impl<T> Clone for Acceptor<T> {
impl Clone for Acceptor {
#[inline]
fn clone(&self) -> Self {
Self {
acceptor: self.acceptor.clone(),
io: PhantomData,
}
}
}
impl<T> ServiceFactory<T> for Acceptor<T>
impl<T> ServiceFactory<T> for Acceptor
where
T: AsyncRead + AsyncWrite + Unpin + 'static,
{
type Response = TlsStream<T>;
type Error = Error;
type Service = NativeTlsAcceptorService<T>;
type Config = ();
type Service = NativeTlsAcceptorService;
type InitError = ();
type Future = future::Ready<Result<Self::Service, Self::InitError>>;
type Future = Ready<Result<Self::Service, Self::InitError>>;
fn new_service(&self, _: ()) -> Self::Future {
MAX_CONN_COUNTER.with(|conns| {
future::ok(NativeTlsAcceptorService {
ready(Ok(NativeTlsAcceptorService {
acceptor: self.acceptor.clone(),
conns: conns.clone(),
io: PhantomData,
})
}))
})
}
}
pub struct NativeTlsAcceptorService<T> {
pub struct NativeTlsAcceptorService {
acceptor: TlsAcceptor,
io: PhantomData<T>,
conns: Counter,
}
impl<T> Clone for NativeTlsAcceptorService<T> {
impl Clone for NativeTlsAcceptorService {
fn clone(&self) -> Self {
Self {
acceptor: self.acceptor.clone(),
io: PhantomData,
conns: self.conns.clone(),
}
}
}
impl<T> Service<T> for NativeTlsAcceptorService<T>
impl<T> Service<T> for NativeTlsAcceptorService
where
T: AsyncRead + AsyncWrite + Unpin + 'static,
{
@ -101,12 +89,10 @@ where
fn call(&mut self, io: T) -> Self::Future {
let guard = self.conns.get();
let this = self.clone();
async move { this.acceptor.accept(io).await }
.map_ok(move |io| {
// Required to preserve `CounterGuard` until `Self::Future` is completely resolved.
let _ = guard;
io
})
.boxed_local()
Box::pin(async move {
let io = this.acceptor.accept(io).await;
drop(guard);
io
})
}
}

View File

@ -1,5 +1,4 @@
use std::future::Future;
use std::marker::PhantomData;
use std::pin::Pin;
use std::task::{Context, Poll};
@ -7,7 +6,7 @@ use actix_codec::{AsyncRead, AsyncWrite};
use actix_service::{Service, ServiceFactory};
use actix_utils::counter::{Counter, CounterGuard};
use futures_util::{
future::{ok, Ready},
future::{ready, Ready},
ready,
};
@ -21,61 +20,54 @@ use super::MAX_CONN_COUNTER;
/// Accept TLS connections via `openssl` package.
///
/// `openssl` feature enables this `Acceptor` type.
pub struct Acceptor<T: AsyncRead + AsyncWrite> {
pub struct Acceptor {
acceptor: SslAcceptor,
io: PhantomData<T>,
}
impl<T: AsyncRead + AsyncWrite> Acceptor<T> {
impl Acceptor {
/// Create OpenSSL based `Acceptor` service factory.
#[inline]
pub fn new(acceptor: SslAcceptor) -> Self {
Acceptor {
acceptor,
io: PhantomData,
}
Acceptor { acceptor }
}
}
impl<T: AsyncRead + AsyncWrite> Clone for Acceptor<T> {
impl Clone for Acceptor {
#[inline]
fn clone(&self) -> Self {
Self {
acceptor: self.acceptor.clone(),
io: PhantomData,
}
}
}
impl<T> ServiceFactory<T> for Acceptor<T>
impl<T> ServiceFactory<T> for Acceptor
where
T: AsyncRead + AsyncWrite + Unpin + 'static,
{
type Response = SslStream<T>;
type Error = SslError;
type Config = ();
type Service = AcceptorService<T>;
type Service = AcceptorService;
type InitError = ();
type Future = Ready<Result<Self::Service, Self::InitError>>;
fn new_service(&self, _: ()) -> Self::Future {
MAX_CONN_COUNTER.with(|conns| {
ok(AcceptorService {
ready(Ok(AcceptorService {
acceptor: self.acceptor.clone(),
conns: conns.clone(),
io: PhantomData,
})
}))
})
}
}
pub struct AcceptorService<T> {
pub struct AcceptorService {
acceptor: SslAcceptor,
conns: Counter,
io: PhantomData<T>,
}
impl<T> Service<T> for AcceptorService<T>
impl<T> Service<T> for AcceptorService
where
T: AsyncRead + AsyncWrite + Unpin + 'static,
{

View File

@ -1,6 +1,5 @@
use std::future::Future;
use std::io;
use std::marker::PhantomData;
use std::pin::Pin;
use std::sync::Arc;
use std::task::{Context, Poll};
@ -8,7 +7,7 @@ use std::task::{Context, Poll};
use actix_codec::{AsyncRead, AsyncWrite};
use actix_service::{Service, ServiceFactory};
use actix_utils::counter::{Counter, CounterGuard};
use futures_util::future::{ok, Ready};
use futures_util::future::{ready, Ready};
use tokio_rustls::{Accept, TlsAcceptor};
pub use rustls::{ServerConfig, Session};
@ -20,66 +19,58 @@ use super::MAX_CONN_COUNTER;
/// Accept TLS connections via `rustls` package.
///
/// `rustls` feature enables this `Acceptor` type.
pub struct Acceptor<T> {
pub struct Acceptor {
config: Arc<ServerConfig>,
io: PhantomData<T>,
}
impl<T> Acceptor<T>
where
T: AsyncRead + AsyncWrite,
{
impl Acceptor {
/// Create Rustls based `Acceptor` service factory.
#[inline]
pub fn new(config: ServerConfig) -> Self {
Acceptor {
config: Arc::new(config),
io: PhantomData,
}
}
}
impl<T> Clone for Acceptor<T> {
impl Clone for Acceptor {
#[inline]
fn clone(&self) -> Self {
Self {
config: self.config.clone(),
io: PhantomData,
}
}
}
impl<T> ServiceFactory<T> for Acceptor<T>
impl<T> ServiceFactory<T> for Acceptor
where
T: AsyncRead + AsyncWrite + Unpin,
{
type Response = TlsStream<T>;
type Error = io::Error;
type Service = AcceptorService<T>;
type Config = ();
type Service = AcceptorService;
type InitError = ();
type Future = Ready<Result<Self::Service, Self::InitError>>;
fn new_service(&self, _: ()) -> Self::Future {
MAX_CONN_COUNTER.with(|conns| {
ok(AcceptorService {
ready(Ok(AcceptorService {
acceptor: self.config.clone().into(),
conns: conns.clone(),
io: PhantomData,
})
}))
})
}
}
/// Rustls based `Acceptor` service
pub struct AcceptorService<T> {
pub struct AcceptorService {
acceptor: TlsAcceptor,
io: PhantomData<T>,
conns: Counter,
}
impl<T> Service<T> for AcceptorService<T>
impl<T> Service<T> for AcceptorService
where
T: AsyncRead + AsyncWrite + Unpin,
{
@ -119,11 +110,6 @@ where
fn poll(self: Pin<&mut Self>, cx: &mut Context<'_>) -> Poll<Self::Output> {
let this = self.get_mut();
let res = futures_util::ready!(Pin::new(&mut this.fut).poll(cx));
match res {
Ok(io) => Poll::Ready(Ok(io)),
Err(e) => Poll::Ready(Err(e)),
}
Pin::new(&mut this.fut).poll(cx)
}
}