@@ -26,20 +26,33 @@ pub struct ShutdownGuard(ManuallyDrop<WeakShutdownGuard>);
26
26
#[ derive( Debug , Clone ) ]
27
27
pub struct WeakShutdownGuard {
28
28
pub ( crate ) trigger_rx : Receiver ,
29
+ pub ( crate ) shutdown_signal_trigger_rx : Option < Receiver > ,
29
30
pub ( crate ) zero_tx : Sender ,
30
31
pub ( crate ) ref_count : Arc < AtomicUsize > ,
31
32
}
32
33
33
34
impl ShutdownGuard {
34
- pub ( crate ) fn new ( trigger_rx : Receiver , zero_tx : Sender , ref_count : Arc < AtomicUsize > ) -> Self {
35
+ pub ( crate ) fn new (
36
+ trigger_rx : Receiver ,
37
+ shutdown_signal_trigger_rx : Option < Receiver > ,
38
+ zero_tx : Sender ,
39
+ ref_count : Arc < AtomicUsize > ,
40
+ ) -> Self {
35
41
let value = ref_count. fetch_add ( 1 , Ordering :: SeqCst ) ;
36
42
tracing:: trace!( "new shutdown guard: ref_count+1: {}" , value + 1 ) ;
37
43
Self ( ManuallyDrop :: new ( WeakShutdownGuard :: new (
38
- trigger_rx, zero_tx, ref_count,
44
+ trigger_rx,
45
+ shutdown_signal_trigger_rx,
46
+ zero_tx,
47
+ ref_count,
39
48
) ) )
40
49
}
41
50
42
- /// Returns a Future that gets fulfilled when cancellation (shutdown) is requested.
51
+ /// Returns a Future that gets fulfilled when cancellation (shutdown) is requested
52
+ /// and the delay (if any) duration has been awaited.
53
+ ///
54
+ /// Use [`Self::shutdown_signal_triggered`] for tasks that do not
55
+ /// require this opt-in delay buffer duration.
43
56
///
44
57
/// The future will complete immediately if the token is already cancelled when this method is called.
45
58
///
@@ -56,6 +69,29 @@ impl ShutdownGuard {
56
69
self . 0 . cancelled ( ) . await
57
70
}
58
71
72
+ /// Returns a Future that gets fulfilled when cancellation (shutdown) is requested.
73
+ ///
74
+ /// Use [`Self::cancelled`] if you want to make sure the future
75
+ /// only completes when the buffer delay has been awaited.
76
+ ///
77
+ /// In case no delay has been configured for the parent `Shutdown`,
78
+ /// this function will be equal in behaviour to [`Self::cancelled`].
79
+ ///
80
+ /// The future will complete immediately if the token is already cancelled when this method is called.
81
+ ///
82
+ /// # Cancel safety
83
+ ///
84
+ /// This method is cancel safe.
85
+ ///
86
+ /// # Panics
87
+ ///
88
+ /// This method panics if the iternal mutex
89
+ /// is poisoned while being used.
90
+ #[ inline]
91
+ pub async fn shutdown_signal_triggered ( & self ) {
92
+ self . 0 . shutdown_signal_triggered ( ) . await
93
+ }
94
+
59
95
/// Returns a [`crate::sync::JoinHandle`] that can be awaited on
60
96
/// to wait for the spawned task to complete. See
61
97
/// [`crate::sync::spawn`] for more information.
@@ -166,15 +202,26 @@ impl Drop for ShutdownGuard {
166
202
}
167
203
168
204
impl WeakShutdownGuard {
169
- pub ( crate ) fn new ( trigger_rx : Receiver , zero_tx : Sender , ref_count : Arc < AtomicUsize > ) -> Self {
205
+ pub ( crate ) fn new (
206
+ trigger_rx : Receiver ,
207
+ shutdown_signal_trigger_rx : Option < Receiver > ,
208
+ zero_tx : Sender ,
209
+ ref_count : Arc < AtomicUsize > ,
210
+ ) -> Self {
170
211
Self {
171
212
trigger_rx,
213
+ shutdown_signal_trigger_rx,
172
214
zero_tx,
173
215
ref_count,
174
216
}
175
217
}
176
218
177
- /// Returns a Future that gets fulfilled when cancellation (shutdown) is requested.
219
+ /// Returns a Future that gets fulfilled when cancellation (shutdown) is requested
220
+ /// and the delay (buffer) duration has been awaited on.
221
+ ///
222
+ /// Use [`Self::shutdown_signal_triggered`] in case you want to get
223
+ /// a future which is triggered immediately when the shutdown signal is received,
224
+ /// without waiting for the delay duration first.
178
225
///
179
226
/// The future will complete immediately if the token is already cancelled when this method is called.
180
227
///
@@ -191,6 +238,34 @@ impl WeakShutdownGuard {
191
238
self . trigger_rx . clone ( ) . await ;
192
239
}
193
240
241
+ /// Returns a Future that gets fulfilled when cancellation (shutdown) is requested
242
+ /// without awaiting the delay duration first, if one is set.
243
+ ///
244
+ /// In case no delay has been configured for the parent `Shutdown`,
245
+ /// this function will be equal in behaviour to [`Self::cancelled`].
246
+ ///
247
+ /// Use [`Self::cancelled`] in case you want to get
248
+ /// a future which is triggered when the shutdown signal is received
249
+ /// and thethe delay duration is awaited.
250
+ ///
251
+ /// The future will complete immediately if the token is already cancelled when this method is called.
252
+ ///
253
+ /// # Cancel safety
254
+ ///
255
+ /// This method is cancel safe.
256
+ ///
257
+ /// # Panics
258
+ ///
259
+ /// This method panics if the iternal mutex
260
+ /// is poisoned while being used.
261
+ #[ inline]
262
+ pub async fn shutdown_signal_triggered ( & self ) {
263
+ self . shutdown_signal_trigger_rx
264
+ . clone ( )
265
+ . unwrap_or_else ( || self . trigger_rx . clone ( ) )
266
+ . await
267
+ }
268
+
194
269
/// Returns a Future that gets fulfilled when cancellation (shutdown) is requested.
195
270
///
196
271
/// In contrast to [`ShutdownGuard::cancelled`] this method consumes the guard,
0 commit comments