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
This commit is contained in:
Nathan Harris
2020-10-06 20:06:29 -07:00
parent 3e28e75b6a
commit c8cb256b59
+3 -7
View File
@@ -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) }
}