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
-29
View File
@@ -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",
+1 -1
View File
@@ -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 = []
-1
View File
@@ -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"] }
+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)
}
}