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

WIP Add connection pooling for CIO client #4407

Draft
wants to merge 1 commit into
base: main
Choose a base branch
from
Draft

Conversation

e5l
Copy link
Member

@e5l e5l commented Oct 17, 2024

No description provided.

@e5l e5l self-assigned this Oct 17, 2024
@e5l e5l requested a review from osipxd October 17, 2024 09:48
Copy link
Member

@osipxd osipxd left a 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
Copy link
Member

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
Copy link
Member

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
Copy link
Member

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?

Copy link
Member

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}")
Copy link
Member

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) {
Copy link
Member

@osipxd osipxd Oct 17, 2024

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
Copy link
Member

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?

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

Successfully merging this pull request may close these issues.

2 participants