Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix connection refused error from udp client test #175

Closed
mickvandijke opened this issue Feb 4, 2023 · 2 comments
Closed

Fix connection refused error from udp client test #175

mickvandijke opened this issue Feb 4, 2023 · 2 comments
Labels
Bug Incorrect Behavior

Comments

@mickvandijke
Copy link
Member

When running the udp integration tests, the udp client often returns a connection refused error.

@mickvandijke mickvandijke added the Bug Incorrect Behavior label Feb 4, 2023
@josecelano
Copy link
Member

josecelano commented Feb 6, 2023

@WarmBeer I've had that error in the past, and sometimes the problem is the server is not ready to receive requests yet.

I fixed it for the API and HTTP tracker by using a channel. The server notifies back to the launcher when it's ready, and the launcher waits (blocking) until the server is ready.

I suppose we could implement something similar for the UPD tracker.

For the HTTP tracker:

/// # Panics
///
/// It would panic if the `config::HttpTracker` struct would contain an inappropriate values.
pub async fn start_job(config: &HttpTracker, tracker: Arc<tracker::Tracker>) -> JoinHandle<()> {
    let bind_addr = config
        .bind_address
        .parse::<SocketAddr>()
        .expect("HTTP tracker server bind_address invalid.");
    let ssl_enabled = config.ssl_enabled;
    let ssl_cert_path = config.ssl_cert_path.clone();
    let ssl_key_path = config.ssl_key_path.clone();

    let (tx, rx) = oneshot::channel::<ServerJobStarted>();

    // Run the HTTP tracker server
    let join_handle = tokio::spawn(async move {
        let http_tracker = Http::new(tracker);

        if !ssl_enabled {
            info!("Starting HTTP tracker server on: http://{}", bind_addr);

            let handle = http_tracker.start(bind_addr);

            tx.send(ServerJobStarted())
                .expect("HTTP tracker server should not be dropped");

            handle.await;

            info!("HTTP tracker server on http://{} stopped", bind_addr);
        } else if ssl_enabled && ssl_cert_path.is_some() && ssl_key_path.is_some() {
            info!("Starting HTTPS server on: https://{}", bind_addr);

            let handle = http_tracker.start_tls(bind_addr, ssl_cert_path.unwrap(), ssl_key_path.unwrap());

            tx.send(ServerJobStarted())
                .expect("HTTP tracker server should not be dropped");

            handle.await;

            info!("HTTP tracker server on https://{} stopped", bind_addr);
        } else {
            warn!(
                "Could not start HTTPS tracker server on: {}, missing SSL Cert or Key!",
                bind_addr
            );
        }
    });

    // Wait until the HTTPS tracker server job is running
    match rx.await {
        Ok(_msg) => info!("HTTP tracker server started"),
        Err(e) => panic!("HTTP tracker server was dropped: {e}"),
    }

    join_handle
}

For the API:

/// # Panics
///
/// It would panic if unable to send the  `ApiServerJobStarted` notice.
pub async fn start_job(config: &HttpApi, tracker: Arc<tracker::Tracker>) -> JoinHandle<()> {
    let bind_addr = config
        .bind_address
        .parse::<std::net::SocketAddr>()
        .expect("Tracker API bind_address invalid.");
    let ssl_enabled = config.ssl_enabled;
    let ssl_cert_path = config.ssl_cert_path.clone();
    let ssl_key_path = config.ssl_key_path.clone();

    let (tx, rx) = oneshot::channel::<ApiServerJobStarted>();

    // Run the API server
    let join_handle = tokio::spawn(async move {
        if !ssl_enabled {
            info!("Starting Torrust APIs server on: http://{}", bind_addr);

            let handle = server::start(bind_addr, &tracker);

            tx.send(ApiServerJobStarted()).expect("the API server should not be dropped");

            if let Ok(()) = handle.await {
                info!("Torrust APIs server on http://{} stopped", bind_addr);
            }
        } else if ssl_enabled && ssl_cert_path.is_some() && ssl_key_path.is_some() {
            info!("Starting Torrust APIs server on: https://{}", bind_addr);

            let ssl_config = RustlsConfig::from_pem_file(ssl_cert_path.unwrap(), ssl_key_path.unwrap())
                .await
                .unwrap();

            let handle = server::start_tls(bind_addr, ssl_config, &tracker);

            tx.send(ApiServerJobStarted()).expect("the API server should not be dropped");

            if let Ok(()) = handle.await {
                info!("Torrust APIs server on https://{} stopped", bind_addr);
            }
        }
    });

    // Wait until the APIs server job is running
    match rx.await {
        Ok(_msg) => info!("Torrust APIs server started"),
        Err(e) => panic!("the API server was dropped: {e}"),
    }

    join_handle
}

@josecelano
Copy link
Member

This should be fixed when #156 is finished.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Bug Incorrect Behavior
Projects
Archived in project
Development

No branches or pull requests

2 participants