Skip to content

Commit

Permalink
refactor(telemeter): use hyper_util::rt::TokioTimer
Browse files Browse the repository at this point in the history
  • Loading branch information
loyd committed Apr 5, 2024
1 parent f332b6b commit 6f06511
Show file tree
Hide file tree
Showing 2 changed files with 4 additions and 61 deletions.
4 changes: 1 addition & 3 deletions elfo-telemeter/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down
61 changes: 3 additions & 58 deletions elfo-telemeter/src/hyper.rs
Original file line number Diff line number Diff line change
@@ -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};

Expand Down Expand Up @@ -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(
Expand Down Expand Up @@ -163,53 +158,3 @@ fn flat_error(res: Result<Result<(), impl ToString>, 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<dyn rt::Sleep>> {
Box::pin(TokioSleep {
inner: tokio::time::sleep(duration),
})
}

fn sleep_until(&self, deadline: Instant) -> Pin<Box<dyn rt::Sleep>> {
Box::pin(TokioSleep {
inner: tokio::time::sleep_until(deadline.into()),
})
}

fn reset(&self, sleep: &mut Pin<Box<dyn rt::Sleep>>, new_deadline: Instant) {
if let Some(sleep) = sleep.as_mut().downcast_mut_pin::<TokioSleep>() {
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::Output> {
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());
}
}

0 comments on commit 6f06511

Please sign in to comment.