vapor / async-kit

Sugary extensions for the SwiftNIO library
MIT License
71 stars 25 forks source link

generic ConnectionPool #19

Closed tanner0101 closed 5 years ago

tanner0101 commented 5 years ago

Vapor's DatabaseKit package contains a connection pool class for managing database connections. I think it could be useful to make that class more generic and move it here. That would allow for other connections, like HTTP client connections, to easily take advantage of pooling.

Here's an idea of what this might entail:

public protocol ConnectionPoolItem {
    var isClosed: Bool { get }
}
public protocol ConnectionPoolSource {
    associatedtype Connection: ConnectionPoolItem
    var eventLoop: EventLoop { get }
    func makeConnection() -> EventLoopFuture<Connection>
}

public struct ConnectionPoolConfig {
    var maxConnections: Int
}

public final class ConnectionPool<Source> where Source: ConnectionPoolSource  {
    init(config: ConnectionPoolConfig, source: Source) { ... }
    func requestConnection() -> EventLoopFuture<Source.Connection> { ... }
    func releaseConnection(_: Source.Connection)
}

extension ConnectionPool {
     func withConnection(_: (Source.Connection) -> EventLoopFuture<Void>) -> EventLoopFuture<Void> { ... }
}
Mordil commented 5 years ago

Do we know the SwiftNIO team's thoughts on this being baked in somehow to NIO itself?

Either way, I think it's extremely useful and might be one of the core features that drives adoption of this library