ConnectionPool

public class ConnectionPool

This class implements a first in, first out connection pool. The pool maintains a cache of ConnectionPoolConnection instances, which can be reused for future requests, to enhance performance.

Usage Example:

In most cases, you will create a connection pool using a plugin, such as SwiftKuery-postgreSQL, which implements this class. You would then call the specific plugin with the required options. If you wish to create the a ConnectionPool manually you can use this example. Two closures to generate and release Connection instances are defined. A connection pool is then initialized with the defined closures and options. A single Connection is then retrieved from the pool.

 let options = ConnectionPoolOptions(initialCapacity: 2, maxCapacity: 5, timeout: 1000)
 let connectionGenerator: () -> Connection? = {
    let connection = PostgreSQLConnection(host: "localhost", port: 5432, options: [.databaseName("ExampleDatabase")])
    return connection
 }
 let connectionReleaser: (_ connection: Connection) -> () = { connection in
    connection.closeConnection()
 }
 let connectionPool = ConnectionPool(options: options, connectionGenerator: connectionGenerator, connectionReleaser: connectionReleaser)
 guard let singleConnection = connectionPool.getConnection() else {
    // handle error
    return
 }

Initializer

  • Creates an instance of ConnectionPool containing ConnectionPoolOptions.initialCapacity connections. The connectionGenerator will be invoked ConnectionPoolOptions.initialCapacity times to fill the pool to the initial capacity.

    Usage Example:

    In this example, two closures to generate and release Connection instances are defined. A connection pool is then initialized with the given options, the connectionGenerator and the connectionReleaser.

    let options = ConnectionPoolOptions(initialCapacity: 2, maxCapacity: 5, timeout: 1000)
    let connectionGenerator: () -> Connection? = {
        let connection = PostgreSQLConnection(host: "localhost", port: 5432, options: [.databaseName("ExampleDatabase")])
        return connection
    }
    
    let connectionReleaser: (_ connection: Connection) -> () = { connection in
       connection.closeConnection()
    }
    
    let connectionPool = ConnectionPool(options: options, connectionGenerator: connectionGenerator, connectionReleaser: connectionReleaser)
    

    Declaration

    Swift

    public init(options: ConnectionPoolOptions, connectionGenerator: @escaping () -> Connection?, connectionReleaser: @escaping (Connection) -> ())

    Parameters

    options

    ConnectionPoolOptions describing the pool configuration.

    connectionGenerator

    A closure that returns a new connection for the pool.

    connectionReleaser

    A closure to be used to release a connection from the pool.

  • Get a connection from the pool. This function does not block, if a connection is not available the handler will be added to a queue to be process when a connection becomes available.

    Usage Example:

    A Connection instance is retrieved from a pool that was initialized previously.

    connectionPool.getConnection() { connection, error in
        guard let connection = connection else {
           guard let error = error else {
               // log unspecified error
           }
           // log returned error
        }
        // use connection
        connection.execute(.....) {}
    }
    

    Declaration

    Swift

    public func getConnection(poolTask: @escaping connectionPoolTask)

    Parameters

    poolTask

    A closure to execute when a connection is available.

    Return Value

    A Connection or nil if the wait for a free connection timed out.

Disconnect Pool

  • Removes all the connections from the pool and calls the connectionReleaser closure on each.

    Usage Example:

    A previously initialized ConnectionPool is disconnected, thereby removing all of its connections from the pool.

    connectionPool.disconnect()
    

    Declaration

    Swift

    public func disconnect()