6
6
//! [Tokio]: https://crates.io/crates/tokio
7
7
//! [async-std]: https://crates.io/crates/async-std
8
8
9
- use futures_util:: { future:: BoxFuture , stream:: Stream } ;
10
9
use std:: { fmt:: Debug , future:: Future , time:: Duration } ;
11
- use futures_util:: stream:: unfold;
10
+ use futures_util:: stream:: { unfold, Stream } ;
12
11
use thiserror:: Error ;
13
12
14
13
/// A runtime is an abstraction of an async runtime like [Tokio] or [async-std]. It allows
@@ -19,7 +18,7 @@ use thiserror::Error;
19
18
///
20
19
/// # Note
21
20
///
22
- /// OpenTelemetry expects a *multi-threaded * runtime because its types can move across threads.
21
+ /// OpenTelemetry expects a *multithreaded * runtime because its types can move across threads.
23
22
/// For this reason, this trait requires the `Send` and `Sync` bounds. Single-threaded runtimes
24
23
/// can implement this trait in a way that spawns the tasks on the same thread as the calling code.
25
24
#[ cfg( feature = "experimental_async_runtime" ) ]
@@ -30,13 +29,15 @@ pub trait Runtime: Clone + Send + Sync + 'static {
30
29
///
31
30
/// This is mainly used to run batch span processing in the background. Note, that the function
32
31
/// does not return a handle. OpenTelemetry will use a different way to wait for the future to
33
- /// finish when `TracerProvider` gets shutdown. At the moment this happens by blocking the
32
+ /// finish when the caller shuts down.
33
+ ///
34
+ /// At the moment, the shutdown happens by blocking the
34
35
/// current thread. This means runtime implementations need to make sure they can still execute
35
36
/// the given future even if the main thread is blocked.
36
- fn spawn ( & self , future : BoxFuture < ' static , ( ) > ) ;
37
+ fn spawn < F > ( & self , future : F ) where F : Future < Output = ( ) > + Send + ' static ;
37
38
38
- /// Return a new future, which resolves after the specified [std::time:: Duration].
39
- fn delay ( & self , duration : Duration ) -> impl Future < Output = ( ) > + Send + Sync + ' static ;
39
+ /// Return a future that resolves after the specified [Duration].
40
+ fn delay ( & self , duration : Duration ) -> impl Future < Output = ( ) > + Send + ' static ;
40
41
}
41
42
42
43
/// Uses the given runtime to produce an interval stream.
@@ -67,13 +68,16 @@ pub struct Tokio;
67
68
doc( cfg( all( feature = "experimental_async_runtime" , feature = "rt-tokio" ) ) )
68
69
) ]
69
70
impl Runtime for Tokio {
70
- fn spawn ( & self , future : BoxFuture < ' static , ( ) > ) {
71
+ fn spawn < F > ( & self , future : F )
72
+ where
73
+ F : Future < Output = ( ) > + Send + ' static
74
+ {
71
75
#[ allow( clippy:: let_underscore_future) ]
72
76
// we don't have to await on the returned future to execute
73
77
let _ = tokio:: spawn ( future) ;
74
78
}
75
79
76
- fn delay ( & self , duration : Duration ) -> impl Future < Output = ( ) > + Send + Sync + ' static {
80
+ fn delay ( & self , duration : Duration ) -> impl Future < Output = ( ) > + Send + ' static {
77
81
tokio:: time:: sleep ( duration)
78
82
}
79
83
}
@@ -105,7 +109,10 @@ pub struct TokioCurrentThread;
105
109
) ) )
106
110
) ]
107
111
impl Runtime for TokioCurrentThread {
108
- fn spawn ( & self , future : BoxFuture < ' static , ( ) > ) {
112
+ fn spawn < F > ( & self , future : F )
113
+ where
114
+ F : Future < Output = ( ) > + Send + ' static
115
+ {
109
116
// We cannot force push tracing in current thread tokio scheduler because we rely on
110
117
// BatchSpanProcessor to export spans in a background task, meanwhile we need to block the
111
118
// shutdown function so that the runtime will not finish the blocked task and kill any
@@ -121,7 +128,7 @@ impl Runtime for TokioCurrentThread {
121
128
} ) ;
122
129
}
123
130
124
- fn delay ( & self , duration : Duration ) -> impl Future < Output = ( ) > + Send + Sync + ' static {
131
+ fn delay ( & self , duration : Duration ) -> impl Future < Output = ( ) > + Send + ' static {
125
132
tokio:: time:: sleep ( duration)
126
133
}
127
134
}
@@ -141,12 +148,15 @@ pub struct AsyncStd;
141
148
doc( cfg( all( feature = "experimental_async_runtime" , feature = "rt-async-std" ) ) )
142
149
) ]
143
150
impl Runtime for AsyncStd {
144
- fn spawn ( & self , future : BoxFuture < ' static , ( ) > ) {
151
+ fn spawn < F > ( & self , future : F )
152
+ where
153
+ F : Future < Output = ( ) > + Send + ' static
154
+ {
145
155
#[ allow( clippy:: let_underscore_future) ]
146
156
let _ = async_std:: task:: spawn ( future) ;
147
157
}
148
158
149
- fn delay ( & self , duration : Duration ) -> impl Future < Output = ( ) > + Send + Sync + ' static {
159
+ fn delay ( & self , duration : Duration ) -> impl Future < Output = ( ) > + Send + ' static {
150
160
async_std:: task:: sleep ( duration)
151
161
}
152
162
}
0 commit comments