-
Notifications
You must be signed in to change notification settings - Fork 1.1k
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
WIP Add connection pooling for CIO client #4407
base: main
Are you sure you want to change the base?
Conversation
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Good idea! I have some questions and I see a lot of failing tests. I think the reason might be the inconsistency of socket.isClosed
with JVM sockets isOpen
flag.
@@ -288,9 +287,8 @@ internal class Endpoint( | |||
return connectTimeout to socketTimeout | |||
} | |||
|
|||
private fun releaseConnection() { | |||
val address = connectionAddress ?: return |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Can we remove the field connectionAddress
?
) { | ||
private val connectionsLimit: Int, | ||
private val addressConnectionsLimit: Int, | ||
private val keepAliveTime: Long = 30_000, // Default keep-alive time in milliseconds |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Shall we use Duration
here?
private fun getPooledConnection(address: InetSocketAddress): PooledConnection? { | ||
LOG.trace { "Checking for pooled connection for address: $address" } | ||
val connections = connectionPool[address] ?: return null | ||
val currentTime = GMTDate().timestamp |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Just wondering why we don't use kotlinx.datetime? Are we waiting for a stable version?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Found the issue: KTOR-2721 Migrate from GMTDate to kotlinx.datetime.Instant
LOG.trace { "Releasing connection for address: ${socket.remoteAddress}" } | ||
|
||
if (socket.isClosed) { | ||
LOG.warn("Attempted to release a closed connection for address: ${socket.remoteAddress}") |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
On JVM sun.nio.ch.SocketChannelImpl.getRemoteAddress
calls ensureOpen
under the hood, so here will be a crash
fun release(socket: Socket) { | ||
LOG.trace { "Releasing connection for address: ${socket.remoteAddress}" } | ||
|
||
if (socket.isClosed) { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This flag seems out of sync with SocketChannel.isOpen
on JVM. As a result, we put closed connections to the pool and get a lot of test failures with java.nio.channels.ClosedChannelException
it.close() | ||
return@removeAll true | ||
} | ||
false |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Should we also remove closed connections here? Is it possible to have a closed connection in the pool?
No description provided.