Skip to content

Commit 4ea42c9

Browse files
authored
refactor(server): remove spawning of 2nd tokio runtime
Closes #4448. Pull-Request: #4454.
1 parent 411a049 commit 4ea42c9

File tree

2 files changed

+14
-26
lines changed

2 files changed

+14
-26
lines changed

misc/server/src/http_service.rs

Lines changed: 10 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@
2121
use hyper::http::StatusCode;
2222
use hyper::service::Service;
2323
use hyper::{Body, Method, Request, Response, Server};
24-
use log::{error, info};
24+
use log::info;
2525
use prometheus_client::encoding::text::encode;
2626
use prometheus_client::registry::Registry;
2727
use std::future::Future;
@@ -30,31 +30,22 @@ use std::sync::{Arc, Mutex};
3030
use std::task::{Context, Poll};
3131

3232
const METRICS_CONTENT_TYPE: &str = "application/openmetrics-text;charset=utf-8;version=1.0.0";
33-
3433
pub(crate) async fn metrics_server(
3534
registry: Registry,
3635
metrics_path: String,
37-
) -> Result<(), std::io::Error> {
36+
) -> Result<(), hyper::Error> {
3837
// Serve on localhost.
3938
let addr = ([0, 0, 0, 0], 8888).into();
4039

41-
// Use the tokio runtime to run the hyper server.
42-
let rt = tokio::runtime::Runtime::new()?;
43-
rt.block_on(async {
44-
let server =
45-
Server::bind(&addr).serve(MakeMetricService::new(registry, metrics_path.clone()));
46-
info!(
47-
"Metrics server on http://{}{}",
48-
server.local_addr(),
49-
metrics_path
50-
);
51-
if let Err(e) = server.await {
52-
error!("server error: {}", e);
53-
}
54-
Ok(())
55-
})
40+
let server = Server::bind(&addr).serve(MakeMetricService::new(registry, metrics_path.clone()));
41+
info!(
42+
"Metrics server on http://{}{}",
43+
server.local_addr(),
44+
metrics_path
45+
);
46+
server.await?;
47+
Ok(())
5648
}
57-
5849
pub(crate) struct MetricService {
5950
reg: Arc<Mutex<Registry>>,
6051
metrics_path: String,

misc/server/src/main.rs

Lines changed: 4 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,5 @@
11
use base64::Engine;
22
use clap::Parser;
3-
use futures::executor::block_on;
43
use futures::future::Either;
54
use futures::stream::StreamExt;
65
use futures_timer::Delay;
@@ -26,7 +25,6 @@ use std::io;
2625
use std::path::PathBuf;
2726
use std::str::FromStr;
2827
use std::task::Poll;
29-
use std::thread;
3028
use std::time::Duration;
3129
use zeroize::Zeroizing;
3230

@@ -141,11 +139,10 @@ async fn main() -> Result<(), Box<dyn Error>> {
141139
"A metric with a constant '1' value labeled by version",
142140
build_info,
143141
);
144-
thread::spawn(move || {
145-
block_on(http_service::metrics_server(
146-
metric_registry,
147-
opt.metrics_path,
148-
))
142+
tokio::spawn(async move {
143+
if let Err(e) = http_service::metrics_server(metric_registry, opt.metrics_path).await {
144+
log::error!("Metrics server failed: {e}");
145+
}
149146
});
150147

151148
let mut bootstrap_timer = Delay::new(BOOTSTRAP_INTERVAL);

0 commit comments

Comments
 (0)