Skip to main content

rocketmq_rust/
shutdown.rs

1// Copyright 2023 The RocketMQ Rust Authors
2//
3// Licensed under the Apache License, Version 2.0 (the "License");
4// you may not use this file except in compliance with the License.
5// You may obtain a copy of the License at
6//
7//     http://www.apache.org/licenses/LICENSE-2.0
8//
9// Unless required by applicable law or agreed to in writing, software
10// distributed under the License is distributed on an "AS IS" BASIS,
11// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12// See the License for the specific language governing permissions and
13// limitations under the License.
14
15use tokio::sync::broadcast;
16use tracing::warn;
17
18pub struct Shutdown<T> {
19    /// `true` if the shutdown signal has been received
20    is_shutdown: bool,
21
22    /// The receiver half of the channel used to listen for shutdown.
23    notify: broadcast::Receiver<T>,
24}
25
26impl<T> Shutdown<T>
27where
28    T: Clone,
29{
30    /// Create a new `Shutdown` backed by the given `broadcast::Receiver`.
31    pub fn new(capacity: usize) -> (Shutdown<T>, broadcast::Sender<T>) {
32        let (tx, _) = broadcast::channel(capacity);
33        let shutdown = Shutdown {
34            is_shutdown: false,
35            notify: tx.subscribe(),
36        };
37        (shutdown, tx)
38    }
39
40    /// Returns `true` if the shutdown signal has been received.
41    pub fn is_shutdown(&self) -> bool {
42        self.is_shutdown
43    }
44
45    /// Receive the shutdown notice, waiting if necessary.
46    pub async fn recv(&mut self) {
47        // If the shutdown signal has already been received, then return
48        // immediately.
49        if self.is_shutdown {
50            return;
51        }
52
53        // Cannot receive a "lag error" as only one value is ever sent.
54        let result = self.notify.recv().await;
55        if result.is_err() {
56            warn!("Failed to receive shutdown signal");
57        }
58
59        // Remember that the signal has been received.
60        self.is_shutdown = true;
61    }
62}
63
64#[cfg(test)]
65mod tests {
66    use super::*;
67
68    #[tokio::test]
69    async fn shutdown_signal_received() {
70        let (mut shutdown, sender) = Shutdown::new(1);
71        sender.send(()).unwrap();
72        shutdown.recv().await;
73        assert!(shutdown.is_shutdown());
74    }
75
76    #[tokio::test]
77    async fn shutdown_signal_not_received() {
78        let (shutdown, _) = Shutdown::<()>::new(1);
79        assert!(!shutdown.is_shutdown());
80    }
81
82    #[tokio::test]
83    async fn shutdown_signal_multiple_receivers() {
84        let (mut shutdown1, sender) = Shutdown::new(1);
85        sender.send(()).unwrap();
86        shutdown1.recv().await;
87
88        assert!(shutdown1.is_shutdown());
89    }
90
91    #[tokio::test]
92    async fn shutdown_signal_already_received() {
93        let (mut shutdown, sender) = Shutdown::new(1);
94        sender.send(()).unwrap();
95        shutdown.recv().await;
96        shutdown.recv().await; // Call recv again
97        assert!(shutdown.is_shutdown());
98    }
99}