Reduce number of required Arc
s
Should improve performance because cloning an `Arc` is expensive
This commit is contained in:
parent
2604b02e2b
commit
95a4a8d75d
12
src/lib.rs
12
src/lib.rs
|
@ -36,13 +36,12 @@ pub use types::*;
|
||||||
pub const REQUEST_URI_MAX_LEN: usize = 1024;
|
pub const REQUEST_URI_MAX_LEN: usize = 1024;
|
||||||
pub const GEMINI_PORT: u16 = 1965;
|
pub const GEMINI_PORT: u16 = 1965;
|
||||||
|
|
||||||
type Handler = Arc<dyn Fn(Request) -> HandlerResponse + Send + Sync>;
|
type Handler = Box<dyn Fn(Request) -> HandlerResponse + Send + Sync>;
|
||||||
pub (crate) type HandlerResponse = Pin<Box<dyn Future<Output = Result<Response>> + Send>>;
|
pub (crate) type HandlerResponse = Pin<Box<dyn Future<Output = Result<Response>> + Send>>;
|
||||||
|
|
||||||
#[derive(Clone)]
|
#[derive(Clone)]
|
||||||
pub struct Server {
|
pub struct Server {
|
||||||
tls_acceptor: TlsAcceptor,
|
tls_acceptor: TlsAcceptor,
|
||||||
listener: Arc<TcpListener>,
|
|
||||||
routes: Arc<RoutingNode<Handler>>,
|
routes: Arc<RoutingNode<Handler>>,
|
||||||
timeout: Duration,
|
timeout: Duration,
|
||||||
complex_timeout: Option<Duration>,
|
complex_timeout: Option<Duration>,
|
||||||
|
@ -53,9 +52,9 @@ impl Server {
|
||||||
Builder::bind(addr)
|
Builder::bind(addr)
|
||||||
}
|
}
|
||||||
|
|
||||||
async fn serve(self) -> Result<()> {
|
async fn serve(self, listener: TcpListener) -> Result<()> {
|
||||||
loop {
|
loop {
|
||||||
let (stream, _addr) = self.listener.accept().await
|
let (stream, _addr) = listener.accept().await
|
||||||
.context("Failed to accept client")?;
|
.context("Failed to accept client")?;
|
||||||
let this = self.clone();
|
let this = self.clone();
|
||||||
|
|
||||||
|
@ -298,7 +297,7 @@ impl<A: ToSocketAddrs> Builder<A> {
|
||||||
H: Send + Sync + 'static + Fn(Request) -> F,
|
H: Send + Sync + 'static + Fn(Request) -> F,
|
||||||
F: Send + Sync + 'static + Future<Output = Result<Response>>
|
F: Send + Sync + 'static + Future<Output = Result<Response>>
|
||||||
{
|
{
|
||||||
let wrapped = Arc::new(move|req| Box::pin((handler)(req)) as HandlerResponse);
|
let wrapped = Box::new(move|req| Box::pin((handler)(req)) as HandlerResponse);
|
||||||
self.routes.add_route(path, wrapped);
|
self.routes.add_route(path, wrapped);
|
||||||
self
|
self
|
||||||
}
|
}
|
||||||
|
@ -314,13 +313,12 @@ impl<A: ToSocketAddrs> Builder<A> {
|
||||||
|
|
||||||
let server = Server {
|
let server = Server {
|
||||||
tls_acceptor: TlsAcceptor::from(config),
|
tls_acceptor: TlsAcceptor::from(config),
|
||||||
listener: Arc::new(listener),
|
|
||||||
routes: Arc::new(self.routes),
|
routes: Arc::new(self.routes),
|
||||||
timeout: self.timeout,
|
timeout: self.timeout,
|
||||||
complex_timeout: self.complex_body_timeout_override,
|
complex_timeout: self.complex_body_timeout_override,
|
||||||
};
|
};
|
||||||
|
|
||||||
server.serve().await
|
server.serve(listener).await
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
Loading…
Reference in a new issue