Library for managing a pool of connection.
- Round Robin
- Failed Connection Retry
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)
}
}
}
Join us on Slack.
ConnectionPool is released under the MIT license. See LICENSE for details.