Skip to content

scottbyrns/ConnectionPool

 
 

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

17 Commits
 
 
 
 
 
 
 
 
 
 

Repository files navigation

ConnectionPool

Swift Platform License Slack

Library for managing a pool of connection.

Features

  • Round Robin
  • Failed Connection Retry

Example

final class PooledSockets: PoolConfiguration {

    // Set how long a pool can suffer a continued series of errors before it is removed from the pool.
    var maxErrorDuration: Duration = 1.minute
    
    // Set how long to wait before trying to issue a connection to a consumer after finding none available.
    var retryDelay: Duration = 10.milliseconds
    
    // How long to wait for a connection to be available before giving up.
    var connectionWait: Duration = 30.milliseconds
    
    // How long to keep trying to reconnect a closed socket before removing it from the pool.
    var maxReconnectDuration: Duration = 5.minutes

    init(connections : [TCPConnection]) throws {
        let pool = ConnectionPool<TCPConnection>(pool: connections, using: self)
        
        // Get a connection from the pool to use.
        try pool.with { connection in
            // Use the connection as needed.
            connection.send(Data("Hello Zewo"))
        }
        
        // Borrow a connection from the pool.
        // While borrowed the connection will not be used by the pool.
        // The pool will begin to use connection when it is returned.
        if let borrowedConnection = pool.borrow() {
            // Return a borrowed connection to the pool.
            pool.takeBack(borrowedConnection)
        }
        
        if let borrowedConnection = pool.borrow() {
            // Remove a connection from the pool.
            pool.remove(borrowedConnection)
        }
    }
}

Community

Slack

Join us on Slack.

License

ConnectionPool is released under the MIT license. See LICENSE for details.

About

Tools for managing a pool of connection.

Resources

License

Stars

Watchers

Forks

Packages

No packages published

Languages

  • Swift 100.0%