|
| 1 | +// Licensed to the Apache Software Foundation (ASF) under one |
| 2 | +// or more contributor license agreements. See the NOTICE file |
| 3 | +// distributed with this work for additional information |
| 4 | +// regarding copyright ownership. The ASF licenses this file |
| 5 | +// to you under the Apache License, Version 2.0 (the |
| 6 | +// "License"); you may not use this file except in compliance |
| 7 | +// with the License. You may obtain a copy of the License at |
| 8 | +// |
| 9 | +// http://www.apache.org/licenses/LICENSE-2.0 |
| 10 | +// |
| 11 | +// Unless required by applicable law or agreed to in writing, |
| 12 | +// software distributed under the License is distributed on an |
| 13 | +// "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY |
| 14 | +// KIND, either express or implied. See the License for the |
| 15 | +// specific language governing permissions and limitations |
| 16 | +// under the License. |
| 17 | + |
| 18 | +use std::future::Future; |
| 19 | +use std::task::{Context, Poll}; |
| 20 | +use tokio::runtime::Handle; |
| 21 | +use tokio::task::JoinSet as TokioJoinSet; |
| 22 | +use tokio::task::{AbortHandle, Id, JoinError, LocalSet}; |
| 23 | + |
| 24 | +#[cfg(feature = "tracing")] |
| 25 | +mod trace_utils { |
| 26 | + use std::future::Future; |
| 27 | + use tracing_futures::Instrument; |
| 28 | + |
| 29 | + /// Instruments the provided future with the current tracing span. |
| 30 | + pub fn trace_future<F, T>(future: F) -> impl Future<Output = T> + Send |
| 31 | + where |
| 32 | + F: Future<Output = T> + Send + 'static, |
| 33 | + T: Send, |
| 34 | + { |
| 35 | + future.in_current_span() |
| 36 | + } |
| 37 | + |
| 38 | + /// Wraps the provided blocking function to execute within the current tracing span. |
| 39 | + pub fn trace_block<F, T>(f: F) -> impl FnOnce() -> T + Send + 'static |
| 40 | + where |
| 41 | + F: FnOnce() -> T + Send + 'static, |
| 42 | + T: Send, |
| 43 | + { |
| 44 | + let span = tracing::Span::current(); |
| 45 | + move || span.in_scope(|| f()) |
| 46 | + } |
| 47 | +} |
| 48 | + |
| 49 | +/// A wrapper around Tokio's [`JoinSet`](tokio::task::JoinSet) that transparently forwards all public API calls |
| 50 | +/// while optionally instrumenting spawned tasks and blocking closures with the current tracing span |
| 51 | +/// when the `trace` feature is enabled. |
| 52 | +#[derive(Debug)] |
| 53 | +pub struct JoinSet<T> { |
| 54 | + inner: TokioJoinSet<T>, |
| 55 | +} |
| 56 | + |
| 57 | +impl<T> JoinSet<T> { |
| 58 | + /// [JoinSet::new](tokio::task::JoinSet::new) - Create a new JoinSet. |
| 59 | + pub fn new() -> Self { |
| 60 | + Self { |
| 61 | + inner: TokioJoinSet::new(), |
| 62 | + } |
| 63 | + } |
| 64 | + |
| 65 | + /// [JoinSet::len](tokio::task::JoinSet::len) - Return the number of tasks. |
| 66 | + pub fn len(&self) -> usize { |
| 67 | + self.inner.len() |
| 68 | + } |
| 69 | + |
| 70 | + /// [JoinSet::is_empty](tokio::task::JoinSet::is_empty) - Check if the JoinSet is empty. |
| 71 | + pub fn is_empty(&self) -> bool { |
| 72 | + self.inner.is_empty() |
| 73 | + } |
| 74 | +} |
| 75 | +impl<T: 'static> JoinSet<T> { |
| 76 | + /// [JoinSet::spawn](tokio::task::JoinSet::spawn) - Spawn a new task. |
| 77 | + pub fn spawn<F>(&mut self, task: F) -> AbortHandle |
| 78 | + where |
| 79 | + F: Future<Output = T>, |
| 80 | + F: Send + 'static, |
| 81 | + T: Send, |
| 82 | + { |
| 83 | + #[cfg(feature = "tracing")] |
| 84 | + let task = trace_utils::trace_future(task); |
| 85 | + |
| 86 | + self.inner.spawn(task) |
| 87 | + } |
| 88 | + |
| 89 | + /// [JoinSet::spawn_on](tokio::task::JoinSet::spawn_on) - Spawn a task on a provided runtime. |
| 90 | + pub fn spawn_on<F>(&mut self, task: F, handle: &Handle) -> AbortHandle |
| 91 | + where |
| 92 | + F: Future<Output = T>, |
| 93 | + F: Send + 'static, |
| 94 | + T: Send, |
| 95 | + { |
| 96 | + #[cfg(feature = "tracing")] |
| 97 | + let task = trace_utils::trace_future(task); |
| 98 | + |
| 99 | + self.inner.spawn_on(task, handle) |
| 100 | + } |
| 101 | + |
| 102 | + /// [JoinSet::spawn_local](tokio::task::JoinSet::spawn_local) - Spawn a local task. |
| 103 | + pub fn spawn_local<F>(&mut self, task: F) -> AbortHandle |
| 104 | + where |
| 105 | + F: Future<Output = T>, |
| 106 | + F: 'static, |
| 107 | + { |
| 108 | + self.inner.spawn_local(task) |
| 109 | + } |
| 110 | + |
| 111 | + /// [JoinSet::spawn_local_on](tokio::task::JoinSet::spawn_local_on) - Spawn a local task on a provided LocalSet. |
| 112 | + pub fn spawn_local_on<F>(&mut self, task: F, local_set: &LocalSet) -> AbortHandle |
| 113 | + where |
| 114 | + F: Future<Output = T>, |
| 115 | + F: 'static, |
| 116 | + { |
| 117 | + self.inner.spawn_local_on(task, local_set) |
| 118 | + } |
| 119 | + |
| 120 | + /// [JoinSet::spawn_blocking](tokio::task::JoinSet::spawn_blocking) - Spawn a blocking task. |
| 121 | + pub fn spawn_blocking<F>(&mut self, f: F) -> AbortHandle |
| 122 | + where |
| 123 | + F: FnOnce() -> T, |
| 124 | + F: Send + 'static, |
| 125 | + T: Send, |
| 126 | + { |
| 127 | + #[cfg(feature = "tracing")] |
| 128 | + let f = trace_utils::trace_block(f); |
| 129 | + |
| 130 | + self.inner.spawn_blocking(f) |
| 131 | + } |
| 132 | + |
| 133 | + /// [JoinSet::spawn_blocking_on](tokio::task::JoinSet::spawn_blocking_on) - Spawn a blocking task on a provided runtime. |
| 134 | + pub fn spawn_blocking_on<F>(&mut self, f: F, handle: &Handle) -> AbortHandle |
| 135 | + where |
| 136 | + F: FnOnce() -> T, |
| 137 | + F: Send + 'static, |
| 138 | + T: Send, |
| 139 | + { |
| 140 | + #[cfg(feature = "tracing")] |
| 141 | + let f = trace_utils::trace_block(f); |
| 142 | + |
| 143 | + self.inner.spawn_blocking_on(f, handle) |
| 144 | + } |
| 145 | + |
| 146 | + /// [JoinSet::join_next](tokio::task::JoinSet::join_next) - Await the next completed task. |
| 147 | + pub async fn join_next(&mut self) -> Option<Result<T, JoinError>> { |
| 148 | + self.inner.join_next().await |
| 149 | + } |
| 150 | + |
| 151 | + /// [JoinSet::try_join_next](tokio::task::JoinSet::try_join_next) - Try to join the next completed task. |
| 152 | + pub fn try_join_next(&mut self) -> Option<Result<T, JoinError>> { |
| 153 | + self.inner.try_join_next() |
| 154 | + } |
| 155 | + |
| 156 | + /// [JoinSet::abort_all](tokio::task::JoinSet::abort_all) - Abort all tasks. |
| 157 | + pub fn abort_all(&mut self) { |
| 158 | + self.inner.abort_all() |
| 159 | + } |
| 160 | + |
| 161 | + /// [JoinSet::detach_all](tokio::task::JoinSet::detach_all) - Detach all tasks. |
| 162 | + pub fn detach_all(&mut self) { |
| 163 | + self.inner.detach_all() |
| 164 | + } |
| 165 | + |
| 166 | + /// [JoinSet::poll_join_next](tokio::task::JoinSet::poll_join_next) - Poll for the next completed task. |
| 167 | + pub fn poll_join_next( |
| 168 | + &mut self, |
| 169 | + cx: &mut Context<'_>, |
| 170 | + ) -> Poll<Option<Result<T, JoinError>>> { |
| 171 | + self.inner.poll_join_next(cx) |
| 172 | + } |
| 173 | + |
| 174 | + /// [JoinSet::join_next_with_id](tokio::task::JoinSet::join_next_with_id) - Await the next completed task with its ID. |
| 175 | + pub async fn join_next_with_id(&mut self) -> Option<Result<(Id, T), JoinError>> { |
| 176 | + self.inner.join_next_with_id().await |
| 177 | + } |
| 178 | + |
| 179 | + /// [JoinSet::try_join_next_with_id](tokio::task::JoinSet::try_join_next_with_id) - Try to join the next completed task with its ID. |
| 180 | + pub fn try_join_next_with_id(&mut self) -> Option<Result<(Id, T), JoinError>> { |
| 181 | + self.inner.try_join_next_with_id() |
| 182 | + } |
| 183 | + |
| 184 | + /// [JoinSet::poll_join_next_with_id](tokio::task::JoinSet::poll_join_next_with_id) - Poll for the next completed task with its ID. |
| 185 | + pub fn poll_join_next_with_id( |
| 186 | + &mut self, |
| 187 | + cx: &mut Context<'_>, |
| 188 | + ) -> Poll<Option<Result<(Id, T), JoinError>>> { |
| 189 | + self.inner.poll_join_next_with_id(cx) |
| 190 | + } |
| 191 | + |
| 192 | + /// [JoinSet::shutdown](tokio::task::JoinSet::shutdown) - Abort all tasks and wait for shutdown. |
| 193 | + pub async fn shutdown(&mut self) { |
| 194 | + self.inner.shutdown().await |
| 195 | + } |
| 196 | + |
| 197 | + /// [JoinSet::join_all](tokio::task::JoinSet::join_all) - Await all tasks. |
| 198 | + pub async fn join_all(self) -> Vec<T> { |
| 199 | + self.inner.join_all().await |
| 200 | + } |
| 201 | +} |
0 commit comments