From bf10f6dfcf98da4128186c4892240e86aae6a72f Mon Sep 17 00:00:00 2001 From: Nikolay Kim Date: Sat, 8 Sep 2018 10:16:43 -0700 Subject: [PATCH] simplify openssl acceptor --- src/ssl/openssl.rs | 40 ++++++++++------------------------------ 1 file changed, 10 insertions(+), 30 deletions(-) diff --git a/src/ssl/openssl.rs b/src/ssl/openssl.rs index 69690ee27..2a0d80a78 100644 --- a/src/ssl/openssl.rs +++ b/src/ssl/openssl.rs @@ -2,13 +2,13 @@ use std::io; use std::marker::PhantomData; use futures::{future::ok, future::FutureResult, Async, Future, Poll}; -use openssl::ssl::{AlpnError, Error, SslAcceptor, SslAcceptorBuilder, SslConnector}; +use openssl::ssl::{Error, SslAcceptor, SslConnector}; use tokio_io::{AsyncRead, AsyncWrite}; use tokio_openssl::{AcceptAsync, ConnectAsync, SslAcceptorExt, SslConnectorExt, SslStream}; use super::MAX_CONN_COUNTER; use connector::ConnectionInfo; -use server_service::{Counter, CounterGuard}; +use worker::{Connections, ConnectionsGuard}; use {NewService, Service}; /// Support `SSL` connections via openssl package @@ -21,32 +21,12 @@ pub struct OpensslAcceptor { impl OpensslAcceptor { /// Create default `OpensslAcceptor` - pub fn new(builder: SslAcceptorBuilder) -> Self { + pub fn new(acceptor: SslAcceptor) -> Self { OpensslAcceptor { - acceptor: builder.build(), + acceptor, io: PhantomData, } } - - /// Create `OpensslWith` with `HTTP1.1` and `HTTP2`. - pub fn for_http(mut builder: SslAcceptorBuilder) -> io::Result { - let protos = b"\x08http/1.1\x02h2"; - - builder.set_alpn_select_callback(|_, protos| { - const H2: &[u8] = b"\x02h2"; - if protos.windows(3).any(|window| window == H2) { - Ok(b"h2") - } else { - Err(AlpnError::NOACK) - } - }); - builder.set_alpn_protos(&protos[..])?; - - Ok(OpensslAcceptor { - acceptor: builder.build(), - io: PhantomData, - }) - } } impl Clone for OpensslAcceptor { @@ -67,11 +47,11 @@ impl NewService for OpensslAcceptor { type Future = FutureResult; fn new_service(&self) -> Self::Future { - MAX_CONN_COUNTER.with(|counter| { + MAX_CONN_COUNTER.with(|conns| { ok(OpensslAcceptorService { acceptor: self.acceptor.clone(), + conns: conns.clone(), io: PhantomData, - counter: counter.clone(), }) }) } @@ -80,7 +60,7 @@ impl NewService for OpensslAcceptor { pub struct OpensslAcceptorService { acceptor: SslAcceptor, io: PhantomData, - counter: Counter, + conns: Connections, } impl Service for OpensslAcceptorService { @@ -90,7 +70,7 @@ impl Service for OpensslAcceptorService { type Future = OpensslAcceptorServiceFut; fn poll_ready(&mut self) -> Poll<(), Self::Error> { - if self.counter.check() { + if self.conns.check() { Ok(Async::Ready(())) } else { Ok(Async::NotReady) @@ -99,7 +79,7 @@ impl Service for OpensslAcceptorService { fn call(&mut self, req: Self::Request) -> Self::Future { OpensslAcceptorServiceFut { - _guard: self.counter.get(), + _guard: self.conns.get(), fut: SslAcceptorExt::accept_async(&self.acceptor, req), } } @@ -110,7 +90,7 @@ where T: AsyncRead + AsyncWrite, { fut: AcceptAsync, - _guard: CounterGuard, + _guard: ConnectionsGuard, } impl Future for OpensslAcceptorServiceFut {