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

add readiness support for OpensslAcceptor

This commit is contained in:
Nikolay Kim 2018-09-07 10:38:39 -07:00
parent d917b47868
commit 467350c9fc
2 changed files with 94 additions and 6 deletions

View File

@ -1,9 +1,18 @@
//! SSL Services //! SSL Services
use std::sync::atomic::{AtomicUsize, Ordering};
#[cfg(feature = "ssl")] #[cfg(feature = "ssl")]
mod openssl; mod openssl;
#[cfg(feature = "ssl")] #[cfg(feature = "ssl")]
pub use self::openssl::{OpensslAcceptor, OpensslConnector}; pub use self::openssl::{OpensslAcceptor, OpensslConnector};
pub(crate) const MAX_CONN: AtomicUsize = AtomicUsize::new(0);
/// Set max concurrent ssl connect operation per thread
pub fn max_concurrent_ssl_connect(num: usize) {
MAX_CONN.store(num, Ordering::Relaxed);
}
// #[cfg(feature = "tls")] // #[cfg(feature = "tls")]
// mod nativetls; // mod nativetls;
// #[cfg(feature = "tls")] // #[cfg(feature = "tls")]

View File

@ -1,11 +1,16 @@
use std::cell::Cell;
use std::io; use std::io;
use std::marker::PhantomData; use std::marker::PhantomData;
use std::rc::Rc;
use std::sync::atomic::Ordering;
use futures::{future, future::FutureResult, Async, Future, Poll}; use futures::task::AtomicTask;
use futures::{future::ok, future::FutureResult, Async, Future, Poll};
use openssl::ssl::{AlpnError, Error, SslAcceptor, SslAcceptorBuilder, SslConnector}; use openssl::ssl::{AlpnError, Error, SslAcceptor, SslAcceptorBuilder, SslConnector};
use tokio_io::{AsyncRead, AsyncWrite}; use tokio_io::{AsyncRead, AsyncWrite};
use tokio_openssl::{AcceptAsync, ConnectAsync, SslAcceptorExt, SslConnectorExt, SslStream}; use tokio_openssl::{AcceptAsync, ConnectAsync, SslAcceptorExt, SslConnectorExt, SslStream};
use super::MAX_CONN;
use connector::ConnectionInfo; use connector::ConnectionInfo;
use {NewService, Service}; use {NewService, Service};
@ -46,6 +51,7 @@ impl<T> OpensslAcceptor<T> {
}) })
} }
} }
impl<T: AsyncRead + AsyncWrite> Clone for OpensslAcceptor<T> { impl<T: AsyncRead + AsyncWrite> Clone for OpensslAcceptor<T> {
fn clone(&self) -> Self { fn clone(&self) -> Self {
Self { Self {
@ -64,9 +70,10 @@ impl<T: AsyncRead + AsyncWrite> NewService for OpensslAcceptor<T> {
type Future = FutureResult<Self::Service, io::Error>; type Future = FutureResult<Self::Service, io::Error>;
fn new_service(&self) -> Self::Future { fn new_service(&self) -> Self::Future {
future::ok(OpensslAcceptorService { ok(OpensslAcceptorService {
acceptor: self.acceptor.clone(), acceptor: self.acceptor.clone(),
io: PhantomData, io: PhantomData,
inner: Rc::new(Inner::default()),
}) })
} }
} }
@ -74,20 +81,92 @@ impl<T: AsyncRead + AsyncWrite> NewService for OpensslAcceptor<T> {
pub struct OpensslAcceptorService<T> { pub struct OpensslAcceptorService<T> {
acceptor: SslAcceptor, acceptor: SslAcceptor,
io: PhantomData<T>, io: PhantomData<T>,
inner: Rc<Inner>,
} }
impl<T: AsyncRead + AsyncWrite> Service for OpensslAcceptorService<T> { impl<T: AsyncRead + AsyncWrite> Service for OpensslAcceptorService<T> {
type Request = T; type Request = T;
type Response = SslStream<T>; type Response = SslStream<T>;
type Error = Error; type Error = Error;
type Future = AcceptAsync<T>; type Future = OpensslAcceptorServiceFut<T>;
fn poll_ready(&mut self) -> Poll<(), Self::Error> { fn poll_ready(&mut self) -> Poll<(), Self::Error> {
if self.inner.check() {
Ok(Async::Ready(())) Ok(Async::Ready(()))
} else {
Ok(Async::NotReady)
}
} }
fn call(&mut self, req: Self::Request) -> Self::Future { fn call(&mut self, req: Self::Request) -> Self::Future {
SslAcceptorExt::accept_async(&self.acceptor, req) self.inner.inc();
OpensslAcceptorServiceFut {
inner: self.inner.clone(),
fut: SslAcceptorExt::accept_async(&self.acceptor, req),
}
}
}
struct Inner {
maxconn: usize,
count: Cell<usize>,
task: AtomicTask,
}
impl Default for Inner {
fn default() -> Inner {
Inner {
maxconn: MAX_CONN.load(Ordering::Relaxed),
count: Cell::new(0),
task: AtomicTask::new(),
}
}
}
impl Inner {
fn inc(&self) {
let num = self.count.get() + 1;
self.count.set(num);
if num == self.maxconn {
self.task.register();
}
}
fn dec(&self) {
let num = self.count.get();
self.count.set(num - 1);
if num == self.maxconn {
self.task.notify();
}
}
fn check(&self) -> bool {
self.count.get() < self.maxconn
}
}
pub struct OpensslAcceptorServiceFut<T>
where
T: AsyncRead + AsyncWrite,
{
fut: AcceptAsync<T>,
inner: Rc<Inner>,
}
impl<T: AsyncRead + AsyncWrite> Future for OpensslAcceptorServiceFut<T> {
type Item = SslStream<T>;
type Error = Error;
fn poll(&mut self) -> Poll<Self::Item, Self::Error> {
let res = self.fut.poll();
if let Ok(Async::NotReady) = res {
Ok(Async::NotReady)
} else {
self.inner.dec();
res
}
} }
} }
@ -146,7 +225,7 @@ impl<T, Io: AsyncRead + AsyncWrite, E> NewService for OpensslConnector<T, Io, E>
type Future = FutureResult<Self::Service, Self::InitError>; type Future = FutureResult<Self::Service, Self::InitError>;
fn new_service(&self) -> Self::Future { fn new_service(&self) -> Self::Future {
future::ok(OpensslConnectorService { ok(OpensslConnectorService {
connector: self.connector.clone(), connector: self.connector.clone(),
t: PhantomData, t: PhantomData,
io: PhantomData, io: PhantomData,