Improve pool connection handling#145
Open
nils-borrmann-tacto wants to merge 5 commits intolong2ice:devfrom
Open
Improve pool connection handling#145nils-borrmann-tacto wants to merge 5 commits intolong2ice:devfrom
nils-borrmann-tacto wants to merge 5 commits intolong2ice:devfrom
Conversation
Comment on lines
+174
to
175
| conn = await self._create_connection() | ||
| self._acquired_connections.append(conn) |
Contributor
Author
There was a problem hiding this comment.
I think there was a race condition lingering here. Directly after our connection was created, it could be acquired by another coroutine. I fixed this by never putting the connection into the free pool.
Comment on lines
+64
to
+65
| if self.writer.is_closing(): | ||
| return |
Contributor
Author
There was a problem hiding this comment.
This avoid an exception when closing an already closed connection.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
I think that the current implementation of the connection pool is not ideal, which leads to some subtle problems.
The connection pool always "refreshes" every connection it takes from the pool, and thereby simply reconnects a connection that had been closed. This happens even though there might be another still alive connection in the pool.
Imagine a situation where the pool has two
free_connections. The first one is closed, the second on still alive. I would expect the connection pool to test all connections until it finds one that works and acquires that connection. This removes closed connections from the pool and avoids additional latency from unnecessarily re-establishing a new connection.I therefore changed the behaviour of the connection acquisition in the pool. The pool no longer "refreshes" connections, but only checks if the connection is still live (by sending a ping). It does so until it finds a live connection.
(There was actually another bug in the refreshing: Re-connecting the connection never actually worked, because the check for
if self._openedinconnect()always returned True, thus exiting the connect method. This only worked because theExecutionContextinsideexecute()finally re-established the connection.)I also added some debug logs, which helps understanding this behaviour.