From ba81ba8048d0e0ff70396ff1a938c72e9517c6ae Mon Sep 17 00:00:00 2001 From: Koute Date: Thu, 24 Feb 2022 20:30:25 +0900 Subject: [PATCH] Get rid of unnecessary use of `async-std` in non-test code (#10891) --- substrate/Cargo.lock | 29 -------- substrate/client/network/Cargo.toml | 2 +- substrate/utils/prometheus/Cargo.toml | 1 - substrate/utils/prometheus/src/lib.rs | 28 ++------ substrate/utils/prometheus/src/networking.rs | 71 -------------------- 5 files changed, 7 insertions(+), 124 deletions(-) delete mode 100644 substrate/utils/prometheus/src/networking.rs diff --git a/substrate/Cargo.lock b/substrate/Cargo.lock index 73b56aa6a3..b7e4210efd 100644 --- a/substrate/Cargo.lock +++ b/substrate/Cargo.lock @@ -254,23 +254,6 @@ dependencies = [ "event-listener", ] -[[package]] -name = "async-process" -version = "1.2.0" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "b21b63ab5a0db0369deb913540af2892750e42d949faacc7a61495ac418a1692" -dependencies = [ - "async-io", - "blocking", - "cfg-if 1.0.0", - "event-listener", - "futures-lite", - "libc", - "once_cell", - "signal-hook", - "winapi 0.3.9", -] - [[package]] name = "async-std" version = "1.10.0" @@ -282,7 +265,6 @@ dependencies = [ "async-global-executor", "async-io", "async-lock", - "async-process", "crossbeam-utils 0.8.5", "futures-channel", "futures-core", @@ -9389,16 +9371,6 @@ version = "1.0.0" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "42a568c8f2cd051a4d283bd6eb0343ac214c1b0f1ac19f93e1175b2dee38c73d" -[[package]] -name = "signal-hook" -version = "0.3.8" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "ef33d6d0cd06e0840fba9985aab098c147e67e05cee14d412d3345ed14ff30ac" -dependencies = [ - "libc", - "signal-hook-registry", -] - [[package]] name = "signal-hook-registry" version = "1.3.0" @@ -10515,7 +10487,6 @@ dependencies = [ name = "substrate-prometheus-endpoint" version = "0.10.0-dev" dependencies = [ - "async-std", "futures-util", "hyper 0.14.16", "log 0.4.14", diff --git a/substrate/client/network/Cargo.toml b/substrate/client/network/Cargo.toml index 86a0380280..7a978d0740 100644 --- a/substrate/client/network/Cargo.toml +++ b/substrate/client/network/Cargo.toml @@ -18,7 +18,6 @@ prost-build = "0.9" [dependencies] async-trait = "0.1" -async-std = "1.10.0" bitflags = "1.3.2" cid = "0.6.0" bytes = "1" @@ -75,6 +74,7 @@ sp-tracing = { version = "4.0.0", path = "../../primitives/tracing" } substrate-test-runtime = { version = "2.0.0", path = "../../test-utils/runtime" } substrate-test-runtime-client = { version = "2.0.0", path = "../../test-utils/runtime/client" } tempfile = "3.1.0" +async-std = "1.10.0" [features] default = [] diff --git a/substrate/utils/prometheus/Cargo.toml b/substrate/utils/prometheus/Cargo.toml index 264cc44d16..fb998d2809 100644 --- a/substrate/utils/prometheus/Cargo.toml +++ b/substrate/utils/prometheus/Cargo.toml @@ -17,7 +17,6 @@ log = "0.4.8" prometheus = { version = "0.13.0", default-features = false } futures-util = { version = "0.3.19", default-features = false, features = ["io"] } thiserror = "1.0" -async-std = { version = "1.10.0", features = ["unstable"] } tokio = { version = "1.15", features = ["parking_lot"] } hyper = { version = "0.14.16", default-features = false, features = ["http1", "server", "tcp"] } diff --git a/substrate/utils/prometheus/src/lib.rs b/substrate/utils/prometheus/src/lib.rs index 3d28d9cd6f..1892741eff 100644 --- a/substrate/utils/prometheus/src/lib.rs +++ b/substrate/utils/prometheus/src/lib.rs @@ -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, registry: Registry) -> Result hyper::rt::Executor 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"); diff --git a/substrate/utils/prometheus/src/networking.rs b/substrate/utils/prometheus/src/networking.rs deleted file mode 100644 index fda20b7574..0000000000 --- a/substrate/utils/prometheus/src/networking.rs +++ /dev/null @@ -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>> { - 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> { - 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> { - Pin::new(&mut Pin::into_inner(self).0).poll_write(cx, buf) - } - - fn poll_flush(self: Pin<&mut Self>, cx: &mut Context) -> Poll> { - Pin::new(&mut Pin::into_inner(self).0).poll_flush(cx) - } - - fn poll_shutdown(self: Pin<&mut Self>, cx: &mut Context) -> Poll> { - Pin::new(&mut Pin::into_inner(self).0).poll_close(cx) - } -}