Async http server. Even slower

This commit is contained in:
Igor Katson 2024-02-26 22:52:53 +00:00
parent 2a371537fe
commit 15d17355b5
No known key found for this signature in database
GPG key ID: B4EC22B66D61A3F5

View file

@ -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<Box<dyn Future<Output = anyhow::Result<()>> + 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()
}
}