vapor / postgres-nio

🐘 Non-blocking, event-driven Swift client for PostgreSQL.
https://api.vapor.codes/postgresnio/documentation/postgresnio/
MIT License
304 stars 70 forks source link

Fix Connection Pool crash when requests are waiting while timer is triggered #474

Open lovetodream opened 2 months ago

lovetodream commented 2 months ago

When connections are requested from the pool in bursts, the pool might have requests in the queue while a keep alive or idle timer is triggered. This should resolve the issue, closes #472.

Currently, I've only implemented a test case to reproduce the behaviour. (The test case is a bit verbose atm, but I wanted to include the full picture for now) One way to fix it would be to check the request queue in PoolStateMachine.connectionKeepAliveTimerTriggered(_:) and PoolStateMachine.connectionIdleTimerTriggered(_:) instead of a precondition. This can be archived by calling PoolStateMachine.handleAvailableConnection(index:availableContext:) if the queue is not empty. Maybe this should be handled in ConnectionPool instead?

Let me know what you think @fabianfett