From 453012c9bf5db2a13aa3395514c52d60ad7dd7c4 Mon Sep 17 00:00:00 2001 From: John-John Tedro Date: Wed, 15 Jan 2020 14:49:46 +0100 Subject: [PATCH 1/3] Only create tokio runtime once for block_on --- Cargo.lock | 1 + sqlx-macros/Cargo.toml | 3 ++- sqlx-macros/src/lib.rs | 10 ++++++++-- 3 files changed, 11 insertions(+), 3 deletions(-) diff --git a/Cargo.lock b/Cargo.lock index 314f9375f4..22f15aee31 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -1420,6 +1420,7 @@ dependencies = [ "async-std 1.4.0 (registry+https://github.com/rust-lang/crates.io-index)", "dotenv 0.15.0 (registry+https://github.com/rust-lang/crates.io-index)", "futures 0.3.1 (registry+https://github.com/rust-lang/crates.io-index)", + "lazy_static 1.4.0 (registry+https://github.com/rust-lang/crates.io-index)", "once_cell 1.3.0 (registry+https://github.com/rust-lang/crates.io-index)", "proc-macro2 1.0.7 (registry+https://github.com/rust-lang/crates.io-index)", "quote 1.0.2 (registry+https://github.com/rust-lang/crates.io-index)", diff --git a/sqlx-macros/Cargo.toml b/sqlx-macros/Cargo.toml index 8aefc6ce37..106a68c63d 100644 --- a/sqlx-macros/Cargo.toml +++ b/sqlx-macros/Cargo.toml @@ -34,9 +34,10 @@ async-std = { version = "1.4.0", default-features = false, optional = true } tokio = { version = "0.2", optional = true } once_cell = { version = "1.3", optional = true } dotenv = { version = "0.15.0", default-features = false } -futures = { version = "0.3.1", default-features = false } +futures = { version = "0.3.1", default-features = false, features = ["executor"] } proc-macro2 = { version = "1.0.6", default-features = false } sqlx = { version = "0.2.0", default-features = false, path = "../sqlx-core", package = "sqlx-core" } syn = { version = "1.0.11", default-features = false, features = [ "full" ] } quote = { version = "1.0.2", default-features = false } url = { version = "2.1.0", default-features = false } +lazy_static = "1.4.0" diff --git a/sqlx-macros/src/lib.rs b/sqlx-macros/src/lib.rs index 16f8055bbc..5d54610701 100644 --- a/sqlx-macros/src/lib.rs +++ b/sqlx-macros/src/lib.rs @@ -25,10 +25,16 @@ mod query_macros; use query_macros::*; +#[cfg(feature = "runtime-tokio")] +lazy_static::lazy_static! { + static ref BASIC_RUNTIME: tokio::runtime::Runtime = { + tokio::runtime::Builder::new().basic_scheduler().enable_all().build().expect("failed to build tokio runtime") + }; +} + #[cfg(feature = "runtime-tokio")] fn block_on(future: F) -> F::Output { - // TODO: Someone think of something better for async proc macros + tokio - tokio::runtime::Runtime::new().unwrap().block_on(future) + BASIC_RUNTIME.enter(|| futures::executor::block_on(future)) } fn macro_result(tokens: proc_macro2::TokenStream) -> TokenStream { From 68937221f3ad75e6137639ceb66761dd8ecb5773 Mon Sep 17 00:00:00 2001 From: John-John Tedro Date: Wed, 15 Jan 2020 15:51:03 +0100 Subject: [PATCH 2/3] Use the threaded scheduler in the runtime --- sqlx-macros/src/lib.rs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/sqlx-macros/src/lib.rs b/sqlx-macros/src/lib.rs index 5d54610701..90981635e1 100644 --- a/sqlx-macros/src/lib.rs +++ b/sqlx-macros/src/lib.rs @@ -28,7 +28,7 @@ use query_macros::*; #[cfg(feature = "runtime-tokio")] lazy_static::lazy_static! { static ref BASIC_RUNTIME: tokio::runtime::Runtime = { - tokio::runtime::Builder::new().basic_scheduler().enable_all().build().expect("failed to build tokio runtime") + tokio::runtime::Builder::new().threaded_scheduler().enable_all().build().expect("failed to build tokio runtime") }; } From 4b2267233fa28e4c0402b98e323c71df7a4b2d6b Mon Sep 17 00:00:00 2001 From: John-John Tedro Date: Wed, 15 Jan 2020 15:57:54 +0100 Subject: [PATCH 3/3] Be explicit about runtime features used in static runtime --- sqlx-macros/src/lib.rs | 7 ++++++- 1 file changed, 6 insertions(+), 1 deletion(-) diff --git a/sqlx-macros/src/lib.rs b/sqlx-macros/src/lib.rs index 90981635e1..4fa5e5a199 100644 --- a/sqlx-macros/src/lib.rs +++ b/sqlx-macros/src/lib.rs @@ -28,7 +28,12 @@ use query_macros::*; #[cfg(feature = "runtime-tokio")] lazy_static::lazy_static! { static ref BASIC_RUNTIME: tokio::runtime::Runtime = { - tokio::runtime::Builder::new().threaded_scheduler().enable_all().build().expect("failed to build tokio runtime") + tokio::runtime::Builder::new() + .threaded_scheduler() + .enable_io() + .enable_time() + .build() + .expect("failed to build tokio runtime") }; }