vapor / async-kit

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

Added EventLoopConnectionPool.closeAllConnections(logger:) Method #53

Closed calebkleveter closed 4 years ago

calebkleveter commented 4 years ago

Allows you to force all the available connections in an EventLoopFuturePool to close, so when connections are requested they must be freshly created.

Checklist

calebkleveter commented 4 years ago

@tanner0101 I have a situation in which I am renaming an SQLite database file, and then creating a new database with the original name that I want to start using instead. What this means is that the filehandles that previously connected to the database are no good and thus so are the connections. What I need to do is get the database driver to close all the existing connections so when we try to query the database again, we have to get a new connection.

tanner0101 commented 4 years ago

I see. Maybe it would be better then to have a way to "invalidate" the currently available connections. That way when they are next requested by requestConnection, they are seen to be invalidated and a new one is opened instead. That would prevent currently in-use connections from being closed as well as potentially reducing some code duplication here around checking waiters.

To implement this we'd need to be holding references to in-use connections (which I think would be needed anyway for this PR as is to work correctly) and some additional metadata, probably just a bool isValid.

tanner0101 commented 4 years ago

Another approach, which might be better and easier, would be to allow the user to close and replace an instance of Databases using the already available close() method purely within FluentKit.

calebkleveter commented 4 years ago

@tanner0101 Good point. And now that I think about it, that might already be possible:

extension Databases {
    func reset(_ id: DatabaseID? = nil) {
        guard let driver = self.driver(id) else { return }

        driver.shutdown()
        self.use(database: driver, as: id)
    }
}
tanner0101 commented 4 years ago

@calebkleveter yeah that's exactly what I was thinking. 👍