mirror of
https://github.com/fluencelabs/rust-libp2p
synced 2025-05-29 10:41:21 +00:00
refactor(server): remove spawning of 2nd tokio runtime
Closes #4448. Pull-Request: #4454.
This commit is contained in:
parent
411a0495c5
commit
4ea42c99eb
@ -21,7 +21,7 @@
|
|||||||
use hyper::http::StatusCode;
|
use hyper::http::StatusCode;
|
||||||
use hyper::service::Service;
|
use hyper::service::Service;
|
||||||
use hyper::{Body, Method, Request, Response, Server};
|
use hyper::{Body, Method, Request, Response, Server};
|
||||||
use log::{error, info};
|
use log::info;
|
||||||
use prometheus_client::encoding::text::encode;
|
use prometheus_client::encoding::text::encode;
|
||||||
use prometheus_client::registry::Registry;
|
use prometheus_client::registry::Registry;
|
||||||
use std::future::Future;
|
use std::future::Future;
|
||||||
@ -30,31 +30,22 @@ use std::sync::{Arc, Mutex};
|
|||||||
use std::task::{Context, Poll};
|
use std::task::{Context, Poll};
|
||||||
|
|
||||||
const METRICS_CONTENT_TYPE: &str = "application/openmetrics-text;charset=utf-8;version=1.0.0";
|
const METRICS_CONTENT_TYPE: &str = "application/openmetrics-text;charset=utf-8;version=1.0.0";
|
||||||
|
|
||||||
pub(crate) async fn metrics_server(
|
pub(crate) async fn metrics_server(
|
||||||
registry: Registry,
|
registry: Registry,
|
||||||
metrics_path: String,
|
metrics_path: String,
|
||||||
) -> Result<(), std::io::Error> {
|
) -> Result<(), hyper::Error> {
|
||||||
// Serve on localhost.
|
// Serve on localhost.
|
||||||
let addr = ([0, 0, 0, 0], 8888).into();
|
let addr = ([0, 0, 0, 0], 8888).into();
|
||||||
|
|
||||||
// Use the tokio runtime to run the hyper server.
|
let server = Server::bind(&addr).serve(MakeMetricService::new(registry, metrics_path.clone()));
|
||||||
let rt = tokio::runtime::Runtime::new()?;
|
info!(
|
||||||
rt.block_on(async {
|
"Metrics server on http://{}{}",
|
||||||
let server =
|
server.local_addr(),
|
||||||
Server::bind(&addr).serve(MakeMetricService::new(registry, metrics_path.clone()));
|
metrics_path
|
||||||
info!(
|
);
|
||||||
"Metrics server on http://{}{}",
|
server.await?;
|
||||||
server.local_addr(),
|
Ok(())
|
||||||
metrics_path
|
|
||||||
);
|
|
||||||
if let Err(e) = server.await {
|
|
||||||
error!("server error: {}", e);
|
|
||||||
}
|
|
||||||
Ok(())
|
|
||||||
})
|
|
||||||
}
|
}
|
||||||
|
|
||||||
pub(crate) struct MetricService {
|
pub(crate) struct MetricService {
|
||||||
reg: Arc<Mutex<Registry>>,
|
reg: Arc<Mutex<Registry>>,
|
||||||
metrics_path: String,
|
metrics_path: String,
|
||||||
|
@ -1,6 +1,5 @@
|
|||||||
use base64::Engine;
|
use base64::Engine;
|
||||||
use clap::Parser;
|
use clap::Parser;
|
||||||
use futures::executor::block_on;
|
|
||||||
use futures::future::Either;
|
use futures::future::Either;
|
||||||
use futures::stream::StreamExt;
|
use futures::stream::StreamExt;
|
||||||
use futures_timer::Delay;
|
use futures_timer::Delay;
|
||||||
@ -26,7 +25,6 @@ use std::io;
|
|||||||
use std::path::PathBuf;
|
use std::path::PathBuf;
|
||||||
use std::str::FromStr;
|
use std::str::FromStr;
|
||||||
use std::task::Poll;
|
use std::task::Poll;
|
||||||
use std::thread;
|
|
||||||
use std::time::Duration;
|
use std::time::Duration;
|
||||||
use zeroize::Zeroizing;
|
use zeroize::Zeroizing;
|
||||||
|
|
||||||
@ -141,11 +139,10 @@ async fn main() -> Result<(), Box<dyn Error>> {
|
|||||||
"A metric with a constant '1' value labeled by version",
|
"A metric with a constant '1' value labeled by version",
|
||||||
build_info,
|
build_info,
|
||||||
);
|
);
|
||||||
thread::spawn(move || {
|
tokio::spawn(async move {
|
||||||
block_on(http_service::metrics_server(
|
if let Err(e) = http_service::metrics_server(metric_registry, opt.metrics_path).await {
|
||||||
metric_registry,
|
log::error!("Metrics server failed: {e}");
|
||||||
opt.metrics_path,
|
}
|
||||||
))
|
|
||||||
});
|
});
|
||||||
|
|
||||||
let mut bootstrap_timer = Delay::new(BOOTSTRAP_INTERVAL);
|
let mut bootstrap_timer = Delay::new(BOOTSTRAP_INTERVAL);
|
||||||
|
Loading…
x
Reference in New Issue
Block a user