From c8cb256b596910bd6cf152f62059e7da8815e38d Mon Sep 17 00:00:00 2001 From: Nathan Harris Date: Tue, 6 Oct 2020 20:06:29 -0700 Subject: [PATCH] Add default buffer connectionRetryTimeout to avoid literal immediate timeouts Motivation: When trying to allow users to configure the connection retry timeout offset, not having a value provided (deadline of `now`) caused all attempts to use the pool to fail. Modifications: - Change: RedisConnectionPool to always have a timeout offset defined Result: If users don't specify any value, then the default of 60 seconds will be used. If users specify "nil" (or `.none`) as the timeout, then a minimum of 10 milliseconds will be used to avoid immediate timeouts Otherwise, use the user's specified `TimeAmount` as the offset of the timeout --- Sources/RediStack/RedisConnectionPool.swift | 10 +++------- 1 file changed, 3 insertions(+), 7 deletions(-) diff --git a/Sources/RediStack/RedisConnectionPool.swift b/Sources/RediStack/RedisConnectionPool.swift index 7964aed..fd976ce 100644 --- a/Sources/RediStack/RedisConnectionPool.swift +++ b/Sources/RediStack/RedisConnectionPool.swift @@ -45,7 +45,7 @@ public class RedisConnectionPool { /// This needs to be a var because we reuse the same connection private var pubsubConnection: RedisConnection? - private let connectionRetryTimeout: TimeAmount? + private let connectionRetryTimeout: TimeAmount private let connectionPassword: String? private let connectionSystemContext: Logger private let poolSystemContext: Context @@ -88,7 +88,7 @@ public class RedisConnectionPool { self.loop = loop self.serverConnectionAddresses = ConnectionAddresses(initialAddresses: serverConnectionAddresses) self.connectionPassword = connectionPassword - self.connectionRetryTimeout = connectionRetryTimeout + self.connectionRetryTimeout = connectionRetryTimeout ?? .milliseconds(10) // mix of terminology here with the loggers // as we're being "forward thinking" in terms of the 'baggage context' future type @@ -392,11 +392,7 @@ extension RedisConnectionPool: RedisClientWithUserContext { let logger = self.prepareLoggerForUse(context) guard let connection = preferredConnection else { - return pool - .leaseConnection( - deadline: self.connectionRetryTimeout.map({ .now() + $0 }) ?? .now(), - logger: logger - ) + return pool.leaseConnection(deadline: .now() + self.connectionRetryTimeout, logger: logger) .flatMap { operation($0, pool.returnConnection(_:logger:), logger) } }