Skip to content

Commit cc3b2d2

Browse files
committed
feat: runtime agnostic
1 parent 0abbb08 commit cc3b2d2

File tree

4 files changed

+73
-6
lines changed

4 files changed

+73
-6
lines changed

Cargo.toml

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,8 @@ edition = "2018"
1414
[features]
1515
default = []
1616
compression = ["libflate"]
17+
tokio-comp = ["tokio"]
18+
async-std-comp = ["async-std"]
1719

1820
# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html
1921
[dependencies]
@@ -23,7 +25,6 @@ tracing = "0.1.*"
2325
tracing-futures = { version = "0.2.5", default-features = false, features = ["tokio", "futures-03", "std"] }
2426
futures = "0.3.*" # An implementation of futures and streams featuring zero allocations, composability, and itera…
2527
chrono = "0.4.15"
26-
tokio = { version = "1", features = ["full"] }
2728
reqwest = { version = "0.11.*", features = ["json"] }
2829
async-trait = "0.1.*"
2930
serde_json = "1.0.*" # A JSON serialization file format
@@ -33,3 +34,5 @@ uuid = { version = "0.8.*", features = ["v4"] } # A library to
3334

3435
# Non-feature optional dependencies
3536
libflate = { version = "1", optional = true }
37+
tokio = { version = "1", features = ["full"], optional = true }
38+
async-std = { version = "1.9", optional = true }

README.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -49,6 +49,8 @@ _Tested at Rust version: `rustc 1.53.0 (53cb7b09b 2021-06-17)`_
4949
This crate offers the following features, all of which are not activated by default:
5050

5151
- `compression`: Enable the GZIP Compression when sending traces.
52+
- `tokio-comp`: Enable the Tokio compatibility when you have a tokio-runtime
53+
- `async-std-comp`: Enable the async-std compatibility when you have a async-std-runtime
5254

5355
## Examples
5456

src/lib.rs

Lines changed: 10 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,7 @@ mod compression;
2929
mod packages;
3030
mod proto;
3131
pub mod register;
32+
mod runtime;
3233

3334
#[macro_use]
3435
extern crate tracing;
@@ -53,9 +54,8 @@ use proto::{
5354
Report, ReportHeader, Trace, Trace_Details, Trace_Error, Trace_HTTP, Trace_HTTP_Method,
5455
Trace_Location, Trace_Node, Trace_Node_oneof_id, TracesAndStats,
5556
};
57+
use runtime::{channel, Runtime, RwLock, Sender};
5658
use std::convert::TryInto;
57-
use tokio::sync::mpsc::{channel, Sender};
58-
use tokio::sync::RwLock;
5959

6060
/// Apollo Tracing Extension to send traces to Apollo Studio
6161
/// The extension to include to your `async_graphql` instance to connect with Apollo Studio.
@@ -199,11 +199,16 @@ impl ApolloTracing {
199199

200200
let header_tokio = Arc::clone(&header);
201201

202-
tokio::spawn(async move {
202+
Runtime::locate().spawn(async move {
203203
let mut hashmap: HashMap<String, TracesAndStats> =
204204
HashMap::with_capacity(batch_target + 1);
205205
let mut count = 0;
206-
while let Some((name, trace)) = receiver.recv().await {
206+
while let Some((name, trace)) = match Runtime::locate() {
207+
#[cfg(feature = "tokio-comp")]
208+
Runtime::Tokio => receiver.recv().await,
209+
#[cfg(feature = "async-std-comp")]
210+
Runtime::AsyncStd => receiver.recv().await.ok(),
211+
} {
207212
trace!(target: TARGET_LOG, message = "Trace registered", trace = ?trace, name = ?name);
208213

209214
// We bufferize traces and create a Full Report every X
@@ -450,7 +455,7 @@ impl Extension for ApolloTracingExtension {
450455
let sender = self.sender.clone();
451456

452457
let operation_name = self.operation_name.read().await.clone();
453-
tokio::spawn(async move {
458+
Runtime::locate().spawn(async move {
454459
if let Err(e) = sender.send((operation_name, trace)).await {
455460
error!(error = ?e);
456461
}

src/runtime.rs

Lines changed: 57 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,57 @@
1+
use futures::Future;
2+
3+
#[cfg(feature = "async-std-comp")]
4+
pub use async_std::channel::{bounded as channel, Receiver, Sender};
5+
#[cfg(feature = "async-std-comp")]
6+
pub use async_std::sync::RwLock;
7+
#[cfg(feature = "tokio-comp")]
8+
pub use tokio::sync::mpsc::{channel, Receiver, Sender};
9+
#[cfg(feature = "tokio-comp")]
10+
pub use tokio::sync::RwLock;
11+
12+
// From https://github.com/mitsuhiko/redis-rs/blob/99a97e8876c99df5a0cf5536fbff21b5d9cae14c/src/aio.rs
13+
#[derive(Clone, Debug)]
14+
pub(crate) enum Runtime {
15+
#[cfg(feature = "tokio-comp")]
16+
Tokio,
17+
#[cfg(feature = "async-std-comp")]
18+
AsyncStd,
19+
}
20+
21+
impl Runtime {
22+
pub fn locate() -> Self {
23+
#[cfg(all(feature = "tokio-comp", not(feature = "async-std-comp")))]
24+
{
25+
Runtime::Tokio
26+
}
27+
28+
#[cfg(all(not(feature = "tokio-comp"), feature = "async-std-comp"))]
29+
{
30+
Runtime::AsyncStd
31+
}
32+
33+
#[cfg(all(feature = "tokio-comp", feature = "async-std-comp"))]
34+
{
35+
if ::tokio::runtime::Handle::try_current().is_ok() {
36+
Runtime::Tokio
37+
} else {
38+
Runtime::AsyncStd
39+
}
40+
}
41+
42+
#[cfg(all(not(feature = "tokio-comp"), not(feature = "async-std-comp")))]
43+
{
44+
compile_error!("tokio-comp or async-std-comp features required")
45+
}
46+
}
47+
48+
#[allow(dead_code)]
49+
pub fn spawn(&self, f: impl Future<Output = ()> + Send + 'static) {
50+
match self {
51+
#[cfg(feature = "tokio-comp")]
52+
Runtime::Tokio => tokio::spawn(f),
53+
#[cfg(feature = "async-std-comp")]
54+
Runtime::AsyncStd => async_std::task::spawn(f),
55+
};
56+
}
57+
}

0 commit comments

Comments
 (0)