From a9ee87ffa056fb2e4cbcfe59add05d6dedc091cc Mon Sep 17 00:00:00 2001
From: Darius Auding <Darius.auding@gmx.de>
Date: Sun, 28 May 2023 17:30:38 +0200
Subject: [PATCH] Add instant graceful shutdown with SIGNIT

---
 core/http/src/setup.rs | 27 +++++++++++++++------------
 1 file changed, 15 insertions(+), 12 deletions(-)

diff --git a/core/http/src/setup.rs b/core/http/src/setup.rs
index a38b154..dfdb496 100644
--- a/core/http/src/setup.rs
+++ b/core/http/src/setup.rs
@@ -2,7 +2,7 @@ use std::{
     thread::available_parallelism, process::exit,
 };
 
-use tokio::{net::TcpListener, signal::unix::{SignalKind, signal}};
+use tokio::{net::TcpListener, signal::unix::{SignalKind, signal}, runtime, select};
 
 use crate::{
     handlers::handlers::handle_connection,
@@ -52,18 +52,21 @@ impl<'a> Config {
         self
     }
     pub async fn launch(self) {
-        // Set up a signal handler for SIGINT
-        tokio::spawn(async {
-            let mut sigint = signal(SignalKind::interrupt()).unwrap();
-            sigint.recv().await;
-            println!("Shutting down...");
-            exit(0)
-        });
-
+        let mut sigint = signal(SignalKind::interrupt()).unwrap();
         loop {
-            let (socket, _) = self.address.accept().await.unwrap();
-            let mountpoints = self.mountpoints.clone().unwrap();
-            tokio::spawn(async move { handle_connection(socket, mountpoints).await; });
+            select! {
+                _ = sigint.recv() => {
+                    println!("Shutting down...");
+                    break;
+                }
+
+                income = self.address.accept() => {
+                    let income = income.unwrap();
+                    let (socket, _) = income;
+                    let mountpoints = self.mountpoints.clone().unwrap();
+                    tokio::spawn(async move { handle_connection(socket, mountpoints).await; });
+                }
+            }
         }
     }
 }
-- 
GitLab