Skip to content

Commit

Permalink
Handle signals to avoid use of tini
Browse files Browse the repository at this point in the history
  • Loading branch information
aveyrenc committed Jul 31, 2024
1 parent a6ed57d commit 7444a99
Show file tree
Hide file tree
Showing 3 changed files with 51 additions and 7 deletions.
7 changes: 3 additions & 4 deletions Dockerfile
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
# syntax=docker/dockerfile:1
FROM rust:alpine3.20 as builder
FROM rust:alpine3.20 AS builder
RUN apk add --no-cache build-base

# Don't download the entire crates.io package index. Fetch only the index
Expand All @@ -15,8 +15,7 @@ RUN cargo build --release

FROM alpine:3.20
RUN addgroup -S merino && \
adduser -S -G merino merino && \
apk add --no-cache tini
adduser -S -G merino merino
USER merino
COPY --from=builder /app/target/release/merino /usr/local/bin/merino
ENTRYPOINT ["/sbin/tini", "--", "/usr/local/bin/merino"]
ENTRYPOINT ["/usr/local/bin/merino"]
44 changes: 44 additions & 0 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@ extern crate serde_derive;
extern crate log;
use snafu::Snafu;

use std::error::Error;
use std::future::Future;
use std::io;
use std::net::{Ipv4Addr, Ipv6Addr, SocketAddr, SocketAddrV4, SocketAddrV6};
use std::sync::Arc;
Expand Down Expand Up @@ -670,3 +672,45 @@ impl SOCKSReq {
})
}
}

// Signal handlers
#[cfg(target_family = "unix")]
async fn wait_for_shutdown_impl(server: impl Future<Output = ()>) -> Result<(), Box<dyn Error>> {
use tokio::signal::unix::{signal, SignalKind};

let mut sigint = signal(SignalKind::interrupt())?;
let mut sigterm = signal(SignalKind::terminate())?;

tokio::select! {
_ = server=>debug!("Server stopped"),
_ = sigint.recv()=>debug!("Received SIGINT"),
_ = sigterm.recv()=>debug!("Received SIGTERM"),
};

Ok(())
}

#[cfg(target_family = "windows")]
async fn wait_for_shutdown_impl(server: impl Future<Output = ()>) -> Result<(), Box<dyn Error>> {
use tokio::signal::windows;

let mut sig_ctrl_c = windows::ctrl_c()?;
let mut sig_ctrl_break = windows::ctrl_break()?;
let mut sig_ctrl_close = windows::ctrl_close()?;
let mut sig_ctrl_shutdown = windows::ctrl_shutdown()?;

tokio::select! {
_ = server=>debug!("Server stopped"),
_ = sig_ctrl_c.recv()=>debug!("Received CTRL_C"),
_ = sig_ctrl_break.recv()=>debug!("Received CTRL_BREAK"),
_ = sig_ctrl_close.recv()=>debug!("Received CTRL_CLOSE"),
_ = sig_ctrl_shutdown.recv()=>debug!("Received CTRL_SHUTDOWN"),
};

Ok(())
}

// Dispatch to platform specific signal handler
pub async fn wait_for_shutdown(server: impl Future<Output = ()>) -> Result<(), Box<dyn Error>> {
wait_for_shutdown_impl(server).await
}
7 changes: 4 additions & 3 deletions src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -192,13 +192,14 @@ async fn main() -> Result<(), Box<dyn Error>> {
let mut merino = Merino::new(opt.port, &opt.ip, auth_methods, authed_users, timeout).await?;

// Start Proxies
merino.serve().await;
let server = merino.serve();

Ok(())
// Wait for shutdown
merino::wait_for_shutdown(server).await
}

#[test]
fn verify_opt(){
fn verify_opt() {
use clap::CommandFactory;
Opt::command().debug_assert()
}

0 comments on commit 7444a99

Please sign in to comment.