Get rid of unnecessary use of async-std in non-test code (#10891)

This commit is contained in:
Koute
2022-02-24 20:30:25 +09:00
committed by GitHub
parent 9552835ccd
commit ba81ba8048
5 changed files with 7 additions and 124 deletions
+6 -22
View File
@@ -15,7 +15,6 @@
// See the License for the specific language governing permissions and
// limitations under the License.
use futures_util::future::Future;
use hyper::{
http::StatusCode,
server::Server,
@@ -34,7 +33,6 @@ pub use prometheus::{
use prometheus::{core::Collector, Encoder, TextEncoder};
use std::net::SocketAddr;
mod networking;
mod sourced;
pub use sourced::{MetricSource, SourcedCounter, SourcedGauge, SourcedMetric};
@@ -85,23 +83,10 @@ async fn request_metrics(req: Request<Body>, registry: Registry) -> Result<Respo
}
}
#[derive(Clone)]
pub struct Executor;
impl<T> hyper::rt::Executor<T> for Executor
where
T: Future + Send + 'static,
T::Output: Send + 'static,
{
fn execute(&self, future: T) {
async_std::task::spawn(future);
}
}
/// Initializes the metrics context, and starts an HTTP server
/// to serve metrics.
pub async fn init_prometheus(prometheus_addr: SocketAddr, registry: Registry) -> Result<(), Error> {
let listener = async_std::net::TcpListener::bind(&prometheus_addr)
let listener = tokio::net::TcpListener::bind(&prometheus_addr)
.await
.map_err(|_| Error::PortInUse(prometheus_addr))?;
@@ -110,12 +95,11 @@ pub async fn init_prometheus(prometheus_addr: SocketAddr, registry: Registry) ->
/// Init prometheus using the given listener.
async fn init_prometheus_with_listener(
listener: async_std::net::TcpListener,
listener: tokio::net::TcpListener,
registry: Registry,
) -> Result<(), Error> {
use networking::Incoming;
log::info!("〽️ Prometheus exporter started at {}", listener.local_addr()?);
let listener = hyper::server::conn::AddrIncoming::from_listener(listener)?;
log::info!("〽️ Prometheus exporter started at {}", listener.local_addr());
let service = make_service_fn(move |_| {
let registry = registry.clone();
@@ -127,7 +111,7 @@ async fn init_prometheus_with_listener(
}
});
let server = Server::builder(Incoming(listener.incoming())).executor(Executor).serve(service);
let server = Server::builder(listener).serve(service);
let result = server.await.map_err(Into::into);
@@ -147,7 +131,7 @@ mod tests {
let runtime = tokio::runtime::Runtime::new().expect("Creates the runtime");
let listener = runtime
.block_on(async_std::net::TcpListener::bind("127.0.0.1:0"))
.block_on(tokio::net::TcpListener::bind("127.0.0.1:0"))
.expect("Creates listener");
let local_addr = listener.local_addr().expect("Returns the local addr");