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
use std::sync::Arc;
use slog::{debug, info, o};
use tokio::{signal, sync::watch};
use crate::{
config::Config,
filters::{DynFilterFactory, FilterRegistry, FilterSet},
proxy::Builder,
};
#[cfg(doc)]
use crate::filters::FilterFactory;
pub type Error = Box<dyn std::error::Error>;
pub async fn run(
filter_factories: impl IntoIterator<Item = DynFilterFactory>,
) -> Result<(), Error> {
let log = crate::proxy::logger();
run_with_config(
log.clone(),
Config::find(&log, None).map(Arc::new)?,
filter_factories,
)
.await
}
pub async fn run_with_config(
base_log: slog::Logger,
config: Arc<Config>,
filter_factories: impl IntoIterator<Item = DynFilterFactory>,
) -> Result<(), Error> {
let log = base_log.new(o!("source" => "run"));
let server = Builder::from(config)
.with_log(base_log)
.with_filter_registry(FilterRegistry::new(FilterSet::default_with(
&log,
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::<()>(());
let signal_log = log.clone();
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!(signal_log, "Received SIGINT")
}
_ = sig_term => {
debug!(signal_log, "Received SIGTERM")
}
}
info!(signal_log, "Shutting down");
shutdown_tx.send(()).ok();
});
if let Err(err) = server.run(shutdown_rx).await {
info!(log, "Shutting down with error"; "error" => %err);
Err(Error::from(err))
} else {
Ok(())
}
}