graph: Use tokio block_on instead of futures block_on
-use futures03::executor::block_on;
+//! The functions in this module should be used to execute futures, serving as a facade to the
+//! underlying executor implementation which currently is tokio. This serves a few purposes:
+//! - Avoid depending directly on tokio APIs, making upgrades or a potential switch easier.
+//! - Reflect our chosen default semantics of aborting on task panic, offering `*_allow_panic`
+//! functoins to opt out of that.
+//! - Reflect that historically we've used blocking futures due to making DB calls directly within
+//! futures. This point should go away once https://github.com/graphprotocol/graph-node/issues/905
+//! is resolved. Then the blocking flavors should no longer accept futures but closures.
+//!
+//! These should not be called from within executors other than tokio, particularly the blocking
+//! functions will panic in that case. We should generally avoid mixing executors whenever possible.
+
use futures03::future::{FutureExt, TryFutureExt};
use std::future::Future as Future03;
use std::panic::AssertUnwindSafe;
@@ -14,6 +25,11 @@ fn abort_on_panic<T: Send + 'static>(
})
}
+/// Runs the future on the current thread. Panics if not within a tokio runtime.
+fn block_on<T>(f: impl Future03<Output = T>) -> T {
+ tokio::runtime::Handle::current().block_on(f)
+}
+
/// Aborts on panic.
pub fn spawn<T: Send + 'static>(f: impl Future03<Output = T> + Send + 'static) -> JoinHandle<T> {
tokio::spawn(abort_on_panic(f))
