evgenyigumnov / cblt

Safe and fast minimalistic web server, written in Rust, that serves files from a directory and proxies requests to another server.
MIT License
39 stars 7 forks source link

--reload param update configuration but not close removed ports #24

Open evgenyigumnov opened 5 days ago

evgenyigumnov commented 5 days ago
  1. change logic how to work with sockets in server.rs
use std::sync::{Arc, atomic::{AtomicBool, Ordering}};
use tokio::net::TcpListener;
use tokio::sync::Notify;
use tokio::task;
use tokio::time::{sleep, Duration};

#[tokio::main]
async fn main() -> tokio::io::Result<()> {
    let addr = "127.0.0.1:8080";
    let listener = TcpListener::bind(&addr).await?;

    let is_running = Arc::new(AtomicBool::new(true));
    let is_running_clone = is_running.clone();

    let notify = Arc::new(Notify::new());
    let notify_clone = notify.clone();

    // Launch an asynchronous task to listen for connections
    let listener_task = task::spawn(async move {
        while is_running_clone.load(Ordering::SeqCst) {
            tokio::select! {
                // Waiting for incoming connections
                Ok((stream, _)) = listener.accept() => {
                    println!("Connection accepted: {:?}", stream);
                    // Here you can handle the connection
                },
                // Waiting for the termination signal
                _ = notify_clone.notified() => {
                    println!("Termination signal received");
                    break;
                },
            }
        }
        println!("TcpListener stopped.");
    });

    // Simulate some working time
    sleep(Duration::from_secs(10)).await;

    // Set the termination flag and notify the task
    is_running.store(false, Ordering::SeqCst);
    notify.notify_one();

    // Wait for the task to complete
    listener_task.await?;

    println!("Program completed.");
    Ok(())
}
  1. send signals for stop listerning for removed ports in Cbltfile (process_workers function need to modify)
evgenyigumnov commented 52 minutes ago

related with https://github.com/evgenyigumnov/cblt/issues/32