Source: scaphandre
Version: 1.0.2-4
Severity: wishlist
Tags: patch
X-Debbugs-Cc: n...@debian.org

Dear Maintainer,

Here is a patch to upgrade scaphandre to use the latest hyper 1.x, so we can 
get rid of the remaining 0.14.

It's a naive copy of https://hyper.rs/guides/1/server/hello-world/, it 
compiles, but I don't have a host running Debian to test the actual 
functionality.

I'll leave the honor of upstreaming to you.

--- a/Cargo.toml
+++ b/Cargo.toml
@@ -23,7 +23,8 @@
  time = "0.3"
  colored = "2.0"
  chrono = "0.4"
-hyper = { version = "0.14", features = ["full"], optional = true }
+hyper = { version = "1.5", features = ["full"], optional = true }
+hyper-util = { version = "0.1", features = ["tokio"] }
  tokio = { version = "1.26.0", features = ["full"], optional = true }
  sysinfo = { version = "0.30.13" }
  isahc = { version = "1.7.2", optional = true }
--- a/src/exporters/prometheus.rs
+++ b/src/exporters/prometheus.rs
@@ -9,8 +9,11 @@
  use crate::sensors::utils::current_system_time_since_epoch;
  use crate::sensors::{Sensor, Topology};
  use chrono::Utc;
-use hyper::service::{make_service_fn, service_fn};
-use hyper::{Body, Request, Response, Server};
+use hyper::{Request, Response};
+use hyper::server::conn::http1::Builder;
+use hyper::service::service_fn;
+use hyper_util::rt::TokioIo;
+use tokio::net::TcpListener;
  use std::convert::Infallible;
  use std::{
      collections::HashMap,
@@ -105,32 +108,28 @@
      socket_addr: SocketAddr,
      metric_generator: MetricGenerator,
      endpoint_suffix: &str,
-) {
+) -> std::io::Result<()> {
      let power_metrics = PowerMetrics {
          last_request: Mutex::new(Duration::new(0, 0)),
          metric_generator: Mutex::new(metric_generator),
      };
      let context = Arc::new(power_metrics);
-    let make_svc = make_service_fn(move |_| {
+
+    let listener = TcpListener::bind(socket_addr).await?;
+    loop {
+        let (stream, _) = listener.accept().await?;
+        let io = TokioIo::new(stream);
+
          let ctx = context.clone();
          let sfx = endpoint_suffix.to_string();
-        async {
-            Ok::<_, Infallible>(service_fn(move |req| {
-                show_metrics(req, ctx.clone(), sfx.clone())
-            }))
-        }
-    });
-    let server = Server::bind(&socket_addr);
-    let res = server.serve(make_svc);
-    let (tx, rx) = tokio::sync::oneshot::channel::<()>();
-    let graceful = res.with_graceful_shutdown(async {
-        rx.await.ok();
-    });
-
-    if let Err(e) = graceful.await {
-        error!("server error: {}", e);
+        tokio::spawn(async move {
+            let svc = service_fn(move |req| { show_metrics(req, ctx.clone(), 
sfx.clone()) });
+            if let Err(e) = Builder::new()
+                .serve_connection(io, svc).await {
+                error!("server error: {}", e);
+            }
+        });
      }
-    let _ = tx.send(());
  }

  /// Adds lines related to a metric in the body (String) of response.
@@ -151,11 +150,11 @@
  }

  /// Handles requests and returns data formated for Prometheus.
-async fn show_metrics(
-    req: Request<Body>,
+async fn show_metrics<B>(
+    req: Request<B>,
      context: Arc<PowerMetrics>,
      suffix: String,
-) -> Result<Response<Body>, Infallible> {
+) -> Result<Response<String>, Infallible> {
      trace!("{}", req.uri());
      let mut body = String::new();
      if req.uri().path() == format!("/{}", &suffix) {
@@ -232,7 +231,7 @@
      } else {
          let _ = write!(body, "<a href=\"https://github.com/hubblo-org/scaphandre/\";>Scaphandre's</a> 
prometheus exporter here. Metrics available on <a href=\"/{suffix}\">/{suffix}</a>");
      }
-    Ok(Response::new(body.into()))
+    Ok(Response::new(body))
  }

  //  Copyright 2020 The scaphandre authors.


--
     ,Sdrager
Blair Noctis

Attachment: OpenPGP_signature.asc
Description: OpenPGP digital signature

Reply via email to