Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Connection management? #16

Closed
rfdickerson opened this issue Nov 28, 2016 · 1 comment
Closed

Connection management? #16

rfdickerson opened this issue Nov 28, 2016 · 1 comment

Comments

@rfdickerson
Copy link
Contributor

Right now I have for each Kitura handler, creating a new PostgreSQL connection, connecting to it, then servicing the request. I would like to perhaps use a single connection object that can do a reconnect only if needed (in case the connection has been lost for some reason). Do do that, I need connection status. Perhaps the result of running:

ConnStatusType PQstatus(const PGconn *conn);

https://www.postgresql.org/docs/9.1/static/libpq-status.html

Do we have plans for these find of connection management policies?

@irar2
Copy link
Contributor

irar2 commented Mar 9, 2017

Here is a proposed API for connection pooling:

(Pool will be a slightly modified @djones6's implementation of object pool).

public class ConnectionPool {
    private let pool: Pool<Connection> 
    
    public init(initialCapacity: Int, maxCapacity: Int = 0, timeout: Int = 0, connectionGenerator: @escaping () -> Connection?, connectionReleaser: @escaping (Connection) -> ()) { }
    
    deinit {  disconnect()  }
   
    public func getConnection() -> Connection? {}
    func release(connection: Connection) { }
    public func disconnect() {}
}

The plugins will create an instance of ConnectionPool with database specific connection generators and releasers.

getConnection() will return a ConnectionPoolConnection - a wrapper around the actual connection. The main purpose of this is to automatically release connection using ConnectionPoolConnection.deinit:

public class ConnectionPoolConnection: Connection {
   private var connection: Connection?
   private weak var pool: ConnectionPool?
    
   init(connection: Connection, pool: ConnectionPool) { }
    
   deinit {
        if let connection = connection {
            pool?.release(connection: connection)
        }
    }
   
   public func connect(onCompletion: (QueryError?) -> ()) { }
    
    public func closeConnection() {
        if let connection = connection {
            pool?.release(connection: connection)
            self.connection = nil
        }
    }
    
    public var isConnected: Bool {
        return connection != nil
    }
    
    public func execute(query: Query, onCompletion: @escaping ((QueryResult) -> ())) {
        connection?.execute(query: query, onCompletion: onCompletion)
    }
  
    // Other Connection functions.
}

We have to decide what to do if the connection is released and e.g. connection.execute() is called. I'll open a separate issue to discuss this.

irar2 added a commit that referenced this issue Mar 14, 2017
irar2 added a commit that referenced this issue Mar 14, 2017
irar2 added a commit that referenced this issue Mar 14, 2017
* #16 Connection pooling

* #16 Connection pooling - fix documentation

* #16 Connection pooling - fix the tests
irar2 added a commit that referenced this issue Mar 14, 2017
irar2 added a commit to Kitura/Swift-Kuery-PostgreSQL that referenced this issue Mar 14, 2017
irar2 added a commit to Kitura/Swift-Kuery-PostgreSQL that referenced this issue Mar 14, 2017
irar2 added a commit to Kitura/Swift-Kuery-PostgreSQL that referenced this issue Mar 21, 2017
@irar2 irar2 closed this as completed Apr 18, 2017
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants