From 46f9f44d414dcbcbdb935b7dddba3f69600c16b5 Mon Sep 17 00:00:00 2001 From: Jakob Truelsen Date: Mon, 21 Dec 2020 20:45:22 +0100 Subject: [PATCH] Do not call connect it a bussy loop on connection refused. If the connection to the database is refused, it is not helpfull to hammer it with connection attempts, instead we should wait a bit before connecting again. Here we start waiting 10ms, doubling the wait time for every attempt before setteling out at 2s --- sqlx-core/src/pool/inner.rs | 10 +++++++++- 1 file changed, 9 insertions(+), 1 deletion(-) diff --git a/sqlx-core/src/pool/inner.rs b/sqlx-core/src/pool/inner.rs index bbf6ed81..a595582b 100644 --- a/sqlx-core/src/pool/inner.rs +++ b/sqlx-core/src/pool/inner.rs @@ -182,6 +182,7 @@ impl SharedPool { let start = Instant::now(); let deadline = start + self.options.connect_timeout; let mut waited = !self.options.fair; + let mut backoff = 0.01; // Unless the pool has been closed ... while !self.is_closed() { @@ -201,7 +202,14 @@ impl SharedPool { match self.connection(deadline, guard).await { Ok(Some(conn)) => return Ok(conn), // [size] is internally decremented on _retry_ and _error_ - Ok(None) => continue, + Ok(None) => { + // If the connection is refused wait in exponentially + // increasing steps for the server to come up, capped by + // two seconds. + sqlx_rt::sleep(std::time::Duration::from_secs_f64(backoff)).await; + backoff = f64::min(backoff * 2.0, 2.0); + continue; + } Err(e) => return Err(e), } }