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.

  • Constructs a RedisConnectionPool that updates its addresses based on information from service discovery.

    This constructor behaves similarly to the regular constructor. However, it also activates the connection pool before returning it to the user. This is necessary because the act of subscribing to service discovery forms a reference cycle between the service discovery instance and the RedisConnectionPool. Pools constructed via this constructor must always have close called on them.

    Pools created via this constructor will be auto-closed when the service discovery instance is completed for any reason, including on error. Users should still always call close in their own code during teardown.

    Declaration

    Swift

    public static func activatedServiceDiscoveryPool<Discovery: ServiceDiscovery>(
        service: Discovery.Service,
        discovery: Discovery,
        configuration: Configuration,
        boundEventLoop: EventLoop,
        logger: Logger? = nil
    ) -> RedisConnectionPool where Discovery.Instance == SocketAddress
  • 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 {
        let client = $0.logging(to: myLogger)
        return client.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.

    Warning

    This will replace any previously set list of 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.