RedisConnectionPool

public class RedisConnectionPool
extension RedisConnectionPool: RedisClient

A RedisConnectionPool is an implementation of RedisClient backed by a pool of connections to Redis, rather than a single one.

RedisConnectionPool uses a pool of connections on a single EventLoop to manage its activity. This pool may vary in size and strategy, including how many active connections it tries to manage at any one time and how it responds to demand for connections beyond its upper limit.

Note that RedisConnectionPool is entirely thread-safe, even though all of its connections belong to a single EventLoop: if callers call the API from a different EventLoop (or from no EventLoop at all) RedisConnectionPool will ensure that the call is dispatched to the correct loop.

  • id

    A unique identifer to represent this connection.

    Declaration

    Swift

    public let id: UUID
  • The count of connections that are active and available for use.

    Declaration

    Swift

    public var availableConnectionCount: Int { get }
  • The number of connections that have been handed out and are in active use.

    Declaration

    Swift

    public var leasedConnectionCount: Int { get }
  • Create a new RedisConnectionPool.

    Declaration

    Swift

    public init(
        serverConnectionAddresses: [SocketAddress],
        loop: EventLoop,
        maximumConnectionCount: RedisConnectionPoolSize,
        minimumConnectionCount: Int = 1,
        connectionPassword: String? = nil,
        connectionLogger: Logger = .redisBaseConnectionLogger,
        connectionTCPClient: ClientBootstrap? = nil,
        poolLogger: Logger = .redisBaseConnectionPoolLogger,
        connectionBackoffFactor: Float32 = 2,
        initialConnectionBackoffDelay: TimeAmount = .milliseconds(100),
        connectionRetryTimeout: TimeAmount? = .seconds(60)
    )

    Parameters

    serverConnectionAddresses

    The set of Redis servers to which this pool is initially willing to connect. This set can be updated over time.

    loop

    The event loop to which this pooled client is tied.

    maximumConnectionCount

    The maximum number of connections to for this pool, either to be preserved or as a hard limit.

    minimumConnectionCount

    The minimum number of connections to preserve in the pool. If the pool is mostly idle and the Redis servers close these idle connections, the RedisConnectionPool will initiate new outbound connections proactively to avoid the number of available connections dropping below this number. Defaults to 1.

    connectionPassword

    The password to use to connect to the Redis servers in this pool.

    connectionLogger

    The Logger to pass to each connection in the pool.

    connectionTCPClient

    The base ClientBootstrap to use to create pool connections, if a custom one is in use.

    poolLogger

    The Logger used by the connection pool itself.

    connectionBackoffFactor

    Used when connection attempts fail to control the exponential backoff. This is a multiplicative factor, each connection attempt will be delayed by this amount times the previous delay.

    initialConnectionBackoffDelay

    If a TCP connection attempt fails, this is the first backoff value on the reconnection attempt. Subsequent backoffs are computed by compounding this value by connectionBackoffFactor.

    connectionRetryTimeout

    The max time to wait for a connection to be available before failing a particular command or connection operation. The default is 60 seconds.

  • Starts the connection pool.

    This method is safe to call multiple times.

    Declaration

    Swift

    public func activate(logger: Logger? = nil)

    Parameters

    logger

    An optional logger to use for any log statements generated while starting up the pool. If one is not provided, the pool will use its default logger.

  • Closes all connections in the pool and deactivates the pool from creating new connections.

    This method is safe to call multiple times.

    Important

    If the pool has connections in active use, the close process will not complete.

    Declaration

    Swift

    public func close(promise: EventLoopPromise<Void>? = nil, logger: Logger? = nil)

    Parameters

    promise

    A notification promise to resolve once the close process has completed.

    logger

    An optional logger to use for any log statements generated while closing the pool. If one is not provided, the pool will use its default logger.

  • Provides limited exclusive access to a connection to be used in a user-defined specialized closure of operations.

    Warning

    Attempting to create PubSub subscriptions with connections leased in the closure will result in a failed NIO.EventLoopFuture.

    RedisConnectionPool manages PubSub state and requires exclusive control over creating PubSub subscriptions.

    Important

    This connection MUST NOT be stored outside of the closure. It is only available exclusively within the closure.

    All operations should be done inside the closure as chained NIO.EventLoopFuture callbacks.

    For example:

    let countFuture = pool.leaseConnection {
        $0.logging(to: myLogger)
            .authorize(with: userPassword)
            .flatMap { connection.select(database: userDatabase) }
            .flatMap { connection.increment(counterKey) }
    }
    

    Warning

    Some commands change the state of the connection that are not tracked client-side, and will not be automatically reset when the connection is returned to the pool.

    When the connection is reused from the pool, it will retain this state and may affect future commands executed with it.

    For example, if select(database:) is used, all future commands made with this connection will be against the selected database.

    To protect against future issues, make sure the final commands executed are to reset the connection to it’s previous known state.

    Declaration

    Swift

    @inlinable
    public func leaseConnection<T>(_ operation: @escaping (RedisConnection) -> EventLoopFuture<T>) -> EventLoopFuture<T>

    Parameters

    operation

    A closure that receives exclusive access to the provided RedisConnection for the lifetime of the closure for specialized Redis command chains.

    Return Value

    A NIO.EventLoopFuture that resolves the value of the NIO.EventLoopFuture in the provided closure operation.

  • Updates the list of valid connection addresses.

    Note

    This does not invalidate existing connections: as long as those connections continue to stay up, they will be kept by this client.

    However, no new connections will be made to any endpoint that is not in newAddresses.

    Declaration

    Swift

    public func updateConnectionAddresses(_ newAddresses: [SocketAddress], logger: Logger? = nil)

    Parameters

    newAddresses

    The new addresses to connect to in future connections.

    logger

    An optional logger to use for any log statements generated while updating the target addresses. If one is not provided, the pool will use its default logger.