Repository: svix/svix-webhooks (language: rust)

Issue:
Fix `wait-for` to have a short timeout (e.g 2s) for each connection attempt
The problem that we are facing now (without it), is that if the first attempt hangs for whatever reason, the whole wait for would fail, even though trying again would have worked just fine.

It's a simple fix.


Discussion / hints:

Fix patch:
diff --git a/server/svix-server/src/wait_for.rs b/server/svix-server/src/wait_for.rs
index 4c4637c14..2a434e313 100644
--- a/server/svix-server/src/wait_for.rs
+++ b/server/svix-server/src/wait_for.rs
@@ -16,13 +16,22 @@ pub async fn wait_for_dsn(
     let sleep = tokio::time::sleep(Duration::from_secs(wait_for_seconds));
     tokio::pin!(sleep);
 
+    const ATTEMPT_TIMEOUT: Duration = Duration::from_secs(3);
+    const RETRY_WAIT: Duration = Duration::from_millis(500);
+
     loop {
         tokio::select! {
-            res = tokio::net::TcpStream::connect((host.to_string(), port)) => {
-                if res.is_err() {
-                    continue;
-                } else {
-                    break;
+            // Attempt the connection with a timeout
+            res = tokio::time::timeout(ATTEMPT_TIMEOUT, tokio::net::TcpStream::connect((host.to_string(), port))) => {
+                match res {
+                    // Connection attempt succeeded
+                    Ok(Ok(_)) => break,
+                    // Connection failed before the timeout was reached _or_ timed out
+                    Ok(Err(_)) | Err(_) => {
+                        tracing::debug!("{dependency_name} connection attempt failed, retrying in {RETRY_WAIT:?}...");
+                        tokio::time::sleep(RETRY_WAIT).await;
+                        continue;
+                    },
                 }
             }
 


Test patch: