Run cargo fmt on the whole code base (#9394)

* Run cargo fmt on the whole code base

* Second run

* Add CI check

* Fix compilation

* More unnecessary braces

* Handle weights

* Use --all

* Use correct attributes...

* Fix UI tests

* AHHHHHHHHH

* 🤦

* Docs

* Fix compilation

* 🤷

* Please stop

* 🤦 x 2

* More

* make rustfmt.toml consistent with polkadot

Co-authored-by: André Silva <andrerfosilva@gmail.com>
This commit is contained in:
Bastian Köcher
2021-07-21 16:32:32 +02:00
committed by GitHub
parent d451c38c1c
commit 7b56ab15b4
1010 changed files with 53339 additions and 51208 deletions
@@ -18,22 +18,24 @@
//! Substrate service tasks management module.
use std::{panic, result::Result, pin::Pin};
use crate::{
config::{JoinFuture, TaskExecutor, TaskType},
Error,
};
use exit_future::Signal;
use log::{debug, error};
use futures::{
Future, FutureExt, StreamExt,
future::{select, Either, BoxFuture, join_all, try_join_all, pending},
future::{join_all, pending, select, try_join_all, BoxFuture, Either},
sink::SinkExt,
Future, FutureExt, StreamExt,
};
use log::{debug, error};
use prometheus_endpoint::{
exponential_buckets, register,
PrometheusError,
CounterVec, HistogramOpts, HistogramVec, Opts, Registry, U64
exponential_buckets, register, CounterVec, HistogramOpts, HistogramVec, Opts, PrometheusError,
Registry, U64,
};
use sp_utils::mpsc::{TracingUnboundedSender, TracingUnboundedReceiver, tracing_unbounded};
use sp_utils::mpsc::{tracing_unbounded, TracingUnboundedReceiver, TracingUnboundedSender};
use std::{panic, pin::Pin, result::Result};
use tracing_futures::Instrument;
use crate::{config::{TaskExecutor, TaskType, JoinFuture}, Error};
mod prometheus_future;
#[cfg(test)]
@@ -62,7 +64,11 @@ impl SpawnTaskHandle {
}
/// Spawns the blocking task with the given name. See also `spawn`.
pub fn spawn_blocking(&self, name: &'static str, task: impl Future<Output = ()> + Send + 'static) {
pub fn spawn_blocking(
&self,
name: &'static str,
task: impl Future<Output = ()> + Send + 'static,
) {
self.spawn_inner(name, task, TaskType::Blocking)
}
@@ -75,7 +81,7 @@ impl SpawnTaskHandle {
) {
if self.task_notifier.is_closed() {
debug!("Attempt to spawn a new task has been prevented: {}", name);
return;
return
}
let on_exit = self.on_exit.clone();
@@ -95,7 +101,8 @@ impl SpawnTaskHandle {
let task = {
let poll_duration = metrics.poll_duration.with_label_values(&[name]);
let poll_start = metrics.poll_start.with_label_values(&[name]);
let inner = prometheus_future::with_poll_durations(poll_duration, poll_start, task);
let inner =
prometheus_future::with_poll_durations(poll_duration, poll_start, task);
// The logic of `AssertUnwindSafe` here is ok considering that we throw
// away the `Future` after it has panicked.
panic::AssertUnwindSafe(inner).catch_unwind()
@@ -106,16 +113,15 @@ impl SpawnTaskHandle {
Either::Right((Err(payload), _)) => {
metrics.tasks_ended.with_label_values(&[name, "panic"]).inc();
panic::resume_unwind(payload)
}
},
Either::Right((Ok(()), _)) => {
metrics.tasks_ended.with_label_values(&[name, "finished"]).inc();
}
},
Either::Left(((), _)) => {
// The `on_exit` has triggered.
metrics.tasks_ended.with_label_values(&[name, "interrupted"]).inc();
}
},
}
} else {
futures::pin_mut!(task);
let _ = select(on_exit, task).await;
@@ -162,10 +168,7 @@ impl SpawnEssentialTaskHandle {
essential_failed_tx: TracingUnboundedSender<()>,
spawn_task_handle: SpawnTaskHandle,
) -> SpawnEssentialTaskHandle {
SpawnEssentialTaskHandle {
essential_failed_tx,
inner: spawn_task_handle,
}
SpawnEssentialTaskHandle { essential_failed_tx, inner: spawn_task_handle }
}
/// Spawns the given task with the given name.
@@ -193,12 +196,10 @@ impl SpawnEssentialTaskHandle {
task_type: TaskType,
) {
let essential_failed = self.essential_failed_tx.clone();
let essential_task = std::panic::AssertUnwindSafe(task)
.catch_unwind()
.map(move |_| {
log::error!("Essential task `{}` failed. Shutting down service.", name);
let _ = essential_failed.close_channel();
});
let essential_task = std::panic::AssertUnwindSafe(task).catch_unwind().map(move |_| {
log::error!("Essential task `{}` failed. Shutting down service.", name);
let _ = essential_failed.close_channel();
});
let _ = self.inner.spawn_inner(name, essential_task, task_type);
}
@@ -260,10 +261,8 @@ impl TaskManager {
// NOTE: for_each_concurrent will await on all the JoinHandle futures at the same time. It
// is possible to limit this but it's actually better for the memory foot print to await
// them all to not accumulate anything on that stream.
let completion_future = executor.spawn(
Box::pin(background_tasks.for_each_concurrent(None, |x| x)),
TaskType::Async,
);
let completion_future = executor
.spawn(Box::pin(background_tasks.for_each_concurrent(None, |x| x)), TaskType::Async);
Ok(Self {
on_exit,
@@ -323,16 +322,21 @@ impl TaskManager {
///
/// This function will not wait until the end of the remaining task. You must call and await
/// `clean_shutdown()` after this.
pub fn future<'a>(&'a mut self) -> Pin<Box<dyn Future<Output = Result<(), Error>> + Send + 'a>> {
pub fn future<'a>(
&'a mut self,
) -> Pin<Box<dyn Future<Output = Result<(), Error>> + Send + 'a>> {
Box::pin(async move {
let mut t1 = self.essential_failed_rx.next().fuse();
let mut t2 = self.on_exit.clone().fuse();
let mut t3 = try_join_all(
self.children.iter_mut().map(|x| x.future())
self.children
.iter_mut()
.map(|x| x.future())
// Never end this future if there is no error because if there is no children,
// it must not stop
.chain(std::iter::once(pending().boxed()))
).fuse();
.chain(std::iter::once(pending().boxed())),
)
.fuse();
futures::select! {
_ = t1 => Err(Error::Other("Essential task failed.".into())),
@@ -20,20 +20,20 @@
use futures::prelude::*;
use prometheus_endpoint::{Counter, Histogram, U64};
use std::{fmt, pin::Pin, task::{Context, Poll}};
use std::{
fmt,
pin::Pin,
task::{Context, Poll},
};
/// Wraps around a `Future`. Report the polling duration to the `Histogram` and when the polling
/// starts to the `Counter`.
pub fn with_poll_durations<T>(
poll_duration: Histogram,
poll_start: Counter<U64>,
inner: T
inner: T,
) -> PrometheusFuture<T> {
PrometheusFuture {
inner,
poll_duration,
poll_start,
}
PrometheusFuture { inner, poll_duration, poll_start }
}
/// Wraps around `Future` and adds diagnostics to it.
@@ -16,8 +16,7 @@
// You should have received a copy of the GNU General Public License
// along with this program. If not, see <https://www.gnu.org/licenses/>.
use crate::config::TaskExecutor;
use crate::task_manager::TaskManager;
use crate::{config::TaskExecutor, task_manager::TaskManager};
use futures::{future::FutureExt, pin_mut, select};
use parking_lot::Mutex;
use std::{any::Any, sync::Arc, time::Duration};
@@ -205,7 +204,9 @@ fn ensure_task_manager_future_ends_with_error_when_essential_task_fails() {
runtime.block_on(async { tokio::time::delay_for(Duration::from_secs(1)).await });
assert_eq!(drop_tester, 2);
spawn_essential_handle.spawn("task3", async { panic!("task failed") });
runtime.block_on(task_manager.future()).expect_err("future()'s Result must be Err");
runtime
.block_on(task_manager.future())
.expect_err("future()'s Result must be Err");
assert_eq!(drop_tester, 2);
runtime.block_on(task_manager.clean_shutdown());
assert_eq!(drop_tester, 0);
@@ -265,7 +266,9 @@ fn ensure_task_manager_future_ends_with_error_when_childs_essential_task_fails()
runtime.block_on(async { tokio::time::delay_for(Duration::from_secs(1)).await });
assert_eq!(drop_tester, 4);
spawn_essential_handle_child_1.spawn("task5", async { panic!("task failed") });
runtime.block_on(task_manager.future()).expect_err("future()'s Result must be Err");
runtime
.block_on(task_manager.future())
.expect_err("future()'s Result must be Err");
assert_eq!(drop_tester, 4);
runtime.block_on(task_manager.clean_shutdown());
assert_eq!(drop_tester, 0);