forked from vectordotdev/vector
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathsignal.rs
More file actions
155 lines (133 loc) · 4.9 KB
/
signal.rs
File metadata and controls
155 lines (133 loc) · 4.9 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
#![allow(missing_docs)]
use tokio::sync::broadcast;
use tokio_stream::{Stream, StreamExt};
use super::config::ConfigBuilder;
pub type ShutdownTx = broadcast::Sender<()>;
pub type SignalTx = broadcast::Sender<SignalTo>;
pub type SignalRx = broadcast::Receiver<SignalTo>;
#[derive(Debug, Clone)]
/// Control messages used by Vector to drive topology and shutdown events.
#[allow(clippy::large_enum_variant)] // discovered during Rust upgrade to 1.57; just allowing for now since we did previously
pub enum SignalTo {
/// Signal to reload config from a string.
ReloadFromConfigBuilder(ConfigBuilder),
/// Signal to reload config from the filesystem.
ReloadFromDisk,
/// Signal to shutdown process.
Shutdown,
/// Shutdown process immediately.
Quit,
}
/// SignalHandler is a general `ControlTo` message receiver and transmitter. It's used by
/// OS signals and providers to surface control events to the root of the application.
pub struct SignalHandler {
tx: SignalTx,
shutdown_txs: Vec<ShutdownTx>,
}
impl SignalHandler {
/// Create a new signal handler with space for 128 control messages at a time, to
/// ensure the channel doesn't overflow and drop signals.
pub fn new() -> (Self, SignalRx) {
let (tx, rx) = broadcast::channel(128);
let handler = Self {
tx,
shutdown_txs: vec![],
};
(handler, rx)
}
/// Clones the transmitter.
pub fn clone_tx(&self) -> SignalTx {
self.tx.clone()
}
/// Subscribe to the stream, and return a new receiver.
pub fn subscribe(&self) -> SignalRx {
self.tx.subscribe()
}
/// Takes a stream who's elements are convertible to `SignalTo`, and spawns a permanent
/// task for transmitting to the receiver.
pub fn forever<T, S>(&mut self, stream: S)
where
T: Into<SignalTo> + Send + Sync,
S: Stream<Item = T> + 'static + Send,
{
let tx = self.tx.clone();
tokio::spawn(async move {
tokio::pin!(stream);
while let Some(value) = stream.next().await {
if tx.send(value.into()).is_err() {
error!(message = "Couldn't send signal.");
break;
}
}
});
}
/// Takes a stream, sending to the underlying signal receiver. Returns a broadcast tx
/// channel which can be used by the caller to either subscribe to cancellation, or trigger
/// it. Useful for providers that may need to do both.
pub fn add<T, S>(&mut self, stream: S)
where
T: Into<SignalTo> + Send,
S: Stream<Item = T> + 'static + Send,
{
let (shutdown_tx, mut shutdown_rx) = broadcast::channel::<()>(2);
let tx = self.tx.clone();
self.shutdown_txs.push(shutdown_tx);
tokio::spawn(async move {
tokio::pin!(stream);
loop {
tokio::select! {
biased;
_ = shutdown_rx.recv() => break,
Some(value) = stream.next() => {
if tx.send(value.into()).is_err() {
error!(message = "Couldn't send signal.");
break;
}
}
else => {
error!(message = "Underlying stream is closed.");
break;
}
}
}
});
}
/// Shutdown active signal handlers.
pub fn clear(&mut self) {
for shutdown_tx in self.shutdown_txs.drain(..) {
// An error just means the channel was already shut down; safe to ignore.
let _ = shutdown_tx.send(());
}
}
}
/// Signals from OS/user.
#[cfg(unix)]
pub fn os_signals() -> impl Stream<Item = SignalTo> {
use tokio::signal::unix::{signal, SignalKind};
let mut sigint = signal(SignalKind::interrupt()).expect("Signal handlers should not panic.");
let mut sigterm = signal(SignalKind::terminate()).expect("Signal handlers should not panic.");
let mut sigquit = signal(SignalKind::quit()).expect("Signal handlers should not panic.");
let mut sighup = signal(SignalKind::hangup()).expect("Signal handlers should not panic.");
async_stream::stream! {
loop {
let signal = tokio::select! {
_ = sigint.recv() => SignalTo::Shutdown,
_ = sigterm.recv() => SignalTo::Shutdown,
_ = sigquit.recv() => SignalTo::Quit,
_ = sighup.recv() => SignalTo::ReloadFromDisk,
};
yield signal;
}
}
}
/// Signals from OS/user.
#[cfg(windows)]
pub(crate) fn os_signals() -> impl Stream<Item = SignalTo> {
use futures::future::FutureExt;
async_stream::stream! {
loop {
let signal = tokio::signal::ctrl_c().map(|_| SignalTo::Shutdown).await;
yield signal;
}
}
}