TLA Line data Source code
1 : use std::convert::Infallible;
2 :
3 : use anyhow::{bail, Context};
4 : use tokio::task::JoinError;
5 : use tokio_util::sync::CancellationToken;
6 : use tracing::warn;
7 :
8 : pub mod auth;
9 : pub mod cache;
10 : pub mod cancellation;
11 : pub mod compute;
12 : pub mod config;
13 : pub mod console;
14 : pub mod error;
15 : pub mod http;
16 : pub mod logging;
17 : pub mod metrics;
18 : pub mod parse;
19 : pub mod protocol2;
20 : pub mod proxy;
21 : pub mod sasl;
22 : pub mod scram;
23 : pub mod stream;
24 : pub mod url;
25 : pub mod waiters;
26 :
27 : /// Handle unix signals appropriately.
28 CBC 17 : pub async fn handle_signals(token: CancellationToken) -> anyhow::Result<Infallible> {
29 : use tokio::signal::unix::{signal, SignalKind};
30 :
31 17 : let mut hangup = signal(SignalKind::hangup())?;
32 17 : let mut interrupt = signal(SignalKind::interrupt())?;
33 17 : let mut terminate = signal(SignalKind::terminate())?;
34 :
35 : loop {
36 34 : tokio::select! {
37 51 : // Hangup is commonly used for config reload.
38 51 : _ = hangup.recv() => {
39 51 : warn!("received SIGHUP; config reload is not supported");
40 51 : }
41 51 : // Shut down the whole application.
42 51 : _ = interrupt.recv() => {
43 51 : warn!("received SIGINT, exiting immediately");
44 51 : bail!("interrupted");
45 51 : }
46 51 : _ = terminate.recv() => {
47 51 : warn!("received SIGTERM, shutting down once all existing connections have closed");
48 51 : token.cancel();
49 51 : }
50 51 : }
51 : }
52 UBC 0 : }
53 :
54 : /// Flattens `Result<Result<T>>` into `Result<T>`.
55 CBC 33 : pub fn flatten_err<T>(r: Result<anyhow::Result<T>, JoinError>) -> anyhow::Result<T> {
56 33 : r.context("join error").and_then(|x| x)
57 33 : }
|