From 15d17355b56cd9e0c0addb84ba4f28540c9d083b Mon Sep 17 00:00:00 2001 From: Igor Katson Date: Mon, 26 Feb 2024 22:52:53 +0000 Subject: [PATCH] Async http server. Even slower --- crates/librqbit/src/http_api.rs | 22 +++++++++++++++------- 1 file changed, 15 insertions(+), 7 deletions(-) diff --git a/crates/librqbit/src/http_api.rs b/crates/librqbit/src/http_api.rs index 09f95a9..a2f1abe 100644 --- a/crates/librqbit/src/http_api.rs +++ b/crates/librqbit/src/http_api.rs @@ -3,11 +3,12 @@ use axum::body::Bytes; use axum::extract::{Path, Query, State}; use axum::response::IntoResponse; use axum::routing::{get, post}; -use futures::TryStreamExt; +use futures::{Future, FutureExt, TryStreamExt}; use itertools::Itertools; use serde::{Deserialize, Serialize}; use std::net::SocketAddr; +use std::pin::Pin; use std::str::FromStr; use std::time::Duration; use tracing::{debug, info}; @@ -44,7 +45,10 @@ impl HttpApi { /// Run the HTTP server forever on the given address. /// If read_only is passed, no state-modifying methods will be exposed. - pub async fn make_http_api_and_run(self, addr: SocketAddr) -> anyhow::Result<()> { + pub fn make_http_api_and_run( + self, + addr: SocketAddr, + ) -> Pin> + Send>> { let state = self.inner; async fn api_root() -> impl IntoResponse { @@ -288,11 +292,15 @@ impl HttpApi { info!(%addr, "starting HTTP server"); use tokio::net::TcpListener; - let listener = TcpListener::bind(&addr) - .await - .with_context(|| format!("error binding to {addr}"))?; - axum::serve(listener, app).await?; - Ok(()) + + async move { + let listener = TcpListener::bind(&addr) + .await + .with_context(|| format!("error binding to {addr}"))?; + axum::serve(listener, app).await?; + Ok(()) + } + .boxed() } }