From 6f06511d9d1100ea6f876436b7704cff5e0fc37c Mon Sep 17 00:00:00 2001 From: Paul Loyd Date: Fri, 5 Apr 2024 19:53:12 +0200 Subject: [PATCH] refactor(telemeter): use `hyper_util::rt::TokioTimer` --- elfo-telemeter/Cargo.toml | 4 +-- elfo-telemeter/src/hyper.rs | 61 ++----------------------------------- 2 files changed, 4 insertions(+), 61 deletions(-) diff --git a/elfo-telemeter/Cargo.toml b/elfo-telemeter/Cargo.toml index ea4a286e..ee09c5d3 100644 --- a/elfo-telemeter/Cargo.toml +++ b/elfo-telemeter/Cargo.toml @@ -18,10 +18,8 @@ elfo-core = { version = "0.2.0-alpha.14", path = "../elfo-core", features = ["un tokio = "1" hyper = { version = "1.0.1", features = ["server", "http1"] } -hyper-util = { version = "0.1.1", features = ["tokio"] } +hyper-util = { version = "0.1.3", features = ["tokio"] } http-body-util = "0.1" -bytes = "1" -pin-project-lite = "0.2" serde = { version = "1.0.120", features = ["derive"] } metrics = "0.17" metrics-util = "0.10" diff --git a/elfo-telemeter/src/hyper.rs b/elfo-telemeter/src/hyper.rs index be0d73ce..6ef3b332 100644 --- a/elfo-telemeter/src/hyper.rs +++ b/elfo-telemeter/src/hyper.rs @@ -1,24 +1,19 @@ use std::{ convert::Infallible, - future::Future, io::{self, Write}, net::SocketAddr, - pin::Pin, string::ToString, - task::Poll, - time::{Duration, Instant}, + time::Duration, }; use http_body_util::Full; use hyper::{ body::Body, header::{HeaderMap, ACCEPT_ENCODING, CONTENT_ENCODING}, - rt, server::conn, service, Method, Request, Response, StatusCode, }; -use hyper_util::rt::TokioIo; -use pin_project_lite::pin_project; +use hyper_util::rt::{TokioIo, TokioTimer}; use tokio::{net::TcpListener, time::timeout}; use tracing::{debug, info, warn}; @@ -57,7 +52,7 @@ pub(crate) async fn server(addr: SocketAddr, ctx: Context) -> ServerFailed { let ctx = ctx.clone(); let serving = conn::http1::Builder::new() - .timer(TokioTimer) + .timer(TokioTimer::new()) .keep_alive(false) // KA is meaningless for rare requests. .header_read_timeout(HEADER_READ_TIMEOUT) .serve_connection( @@ -163,53 +158,3 @@ fn flat_error(res: Result, impl ToString>) -> Result<( Err(err) => Err(err.to_string()), } } - -// === TokioTimer === -// TODO: Replace once https://github.com/hyperium/hyper-util/pull/73 is released. -// Don't forget to remove `pin-project-lite` from `Cargo.toml`. - -#[derive(Clone, Debug)] -struct TokioTimer; - -impl rt::Timer for TokioTimer { - fn sleep(&self, duration: Duration) -> Pin> { - Box::pin(TokioSleep { - inner: tokio::time::sleep(duration), - }) - } - - fn sleep_until(&self, deadline: Instant) -> Pin> { - Box::pin(TokioSleep { - inner: tokio::time::sleep_until(deadline.into()), - }) - } - - fn reset(&self, sleep: &mut Pin>, new_deadline: Instant) { - if let Some(sleep) = sleep.as_mut().downcast_mut_pin::() { - sleep.reset(new_deadline) - } - } -} - -pin_project! { - struct TokioSleep { - #[pin] - inner: tokio::time::Sleep, - } -} - -impl Future for TokioSleep { - type Output = (); - - fn poll(self: Pin<&mut Self>, cx: &mut std::task::Context<'_>) -> Poll { - self.project().inner.poll(cx) - } -} - -impl rt::Sleep for TokioSleep {} - -impl TokioSleep { - fn reset(self: Pin<&mut Self>, deadline: Instant) { - self.project().inner.as_mut().reset(deadline.into()); - } -}