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
use std::sync::Arc;
use tokio::{signal, sync::watch};
use tracing::{debug, info, span, Level};
use crate::{
config::Config,
filters::{DynFilterFactory, FilterRegistry, FilterSet},
proxy::Builder,
Result,
};
#[cfg(doc)]
use crate::filters::FilterFactory;
pub async fn run(
config: Config,
filter_factories: impl IntoIterator<Item = DynFilterFactory>,
) -> Result<()> {
let span = span!(Level::INFO, "source::run");
let _enter = span.enter();
let server = Builder::from(Arc::new(config))
.with_filter_registry(FilterRegistry::new(FilterSet::default_with(
filter_factories.into_iter(),
)))
.validate()?
.build();
#[cfg(target_os = "linux")]
let mut sig_term_fut = signal::unix::signal(signal::unix::SignalKind::terminate())?;
let (shutdown_tx, shutdown_rx) = watch::channel::<()>(());
tokio::spawn(async move {
#[cfg(target_os = "linux")]
let sig_term = sig_term_fut.recv();
#[cfg(not(target_os = "linux"))]
let sig_term = std::future::pending();
tokio::select! {
_ = signal::ctrl_c() => {
debug!("Received SIGINT")
}
_ = sig_term => {
debug!("Received SIGTERM")
}
}
info!("Shutting down");
shutdown_tx.send(()).ok();
});
if let Err(err) = server.run(shutdown_rx).await {
info! (error = %err, "Shutting down with error");
Err(err)
} else {
Ok(())
}
}