From ff556199e6823432b07a20cb4816bf2a626ed52d Mon Sep 17 00:00:00 2001 From: Joe Wilm Date: Wed, 12 Oct 2016 16:39:41 -0700 Subject: [PATCH] fix(client): Improve keep-alive reuse strategy The previous keep-alive strategy was to cycle connections in a round-robin style. However, that will always keep more connections around than are needed. This new strategy will allow extra connections to expire when only a few are needed. This is accomplished by prefering to reuse a connection that was just released to the pool over one that has been there for a long time. --- src/client/mod.rs | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-) diff --git a/src/client/mod.rs b/src/client/mod.rs index 90e66579..a779c29d 100644 --- a/src/client/mod.rs +++ b/src/client/mod.rs @@ -541,6 +541,7 @@ where C: Connect, // Check all idle connections regardless of origin for (key, idle) in scope.idle_conns.iter_mut() { + // Pop from the front since those are lease recently used while let Some(ctrl) = idle.pop_front() { // Signal connection to close. An err here means the // socket is already dead can should be tossed. @@ -667,7 +668,9 @@ where C: Connect, let mut remove_idle = false; let mut woke_up = false; if let Some(mut idle) = scope.idle_conns.get_mut(&key) { - while let Some(ctrl) = idle.pop_front() { + // Pop from back since those are most recently used. Connections + // at the front are allowed to expire. + while let Some(ctrl) = idle.pop_back() { // err means the socket has since died if ctrl.ready(Next::write()).is_ok() { woke_up = true;