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");
@@ -1,71 +0,0 @@
// This file is part of Substrate.
// Copyright (C) 2019-2022 Parity Technologies (UK) Ltd.
// SPDX-License-Identifier: Apache-2.0
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
use async_std::pin::Pin;
use futures_util::{
io::{AsyncRead, AsyncWrite},
stream::Stream,
};
use std::task::{Context, Poll};
pub struct Incoming<'a>(pub async_std::net::Incoming<'a>);
impl hyper::server::accept::Accept for Incoming<'_> {
type Conn = TcpStream;
type Error = async_std::io::Error;
fn poll_accept(
self: Pin<&mut Self>,
cx: &mut Context,
) -> Poll<Option<Result<Self::Conn, Self::Error>>> {
Pin::new(&mut Pin::into_inner(self).0)
.poll_next(cx)
.map(|opt| opt.map(|res| res.map(TcpStream)))
}
}
pub struct TcpStream(pub async_std::net::TcpStream);
impl tokio::io::AsyncRead for TcpStream {
fn poll_read(
self: Pin<&mut Self>,
cx: &mut Context,
buf: &mut tokio::io::ReadBuf<'_>,
) -> Poll<Result<(), std::io::Error>> {
Pin::new(&mut Pin::into_inner(self).0)
.poll_read(cx, buf.initialize_unfilled())
.map_ok(|s| buf.set_filled(s))
}
}
impl tokio::io::AsyncWrite for TcpStream {
fn poll_write(
self: Pin<&mut Self>,
cx: &mut Context,
buf: &[u8],
) -> Poll<Result<usize, std::io::Error>> {
Pin::new(&mut Pin::into_inner(self).0).poll_write(cx, buf)
}
fn poll_flush(self: Pin<&mut Self>, cx: &mut Context) -> Poll<Result<(), std::io::Error>> {
Pin::new(&mut Pin::into_inner(self).0).poll_flush(cx)
}
fn poll_shutdown(self: Pin<&mut Self>, cx: &mut Context) -> Poll<Result<(), std::io::Error>> {
Pin::new(&mut Pin::into_inner(self).0).poll_close(cx)
}
}