-
Notifications
You must be signed in to change notification settings - Fork 28
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
fix:add support for idle connection monitoring (opt-in for now)
- Loading branch information
Showing
8 changed files
with
255 additions
and
2 deletions.
There are no files selected for viewing
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,8 @@ | ||
{ | ||
"id": "386353e6-e3cc-4561-bfd6-9763d3ac033b", | ||
"type": "bugfix", | ||
"description": "Add support for connection idle monitoring for OkHttp via the engine config parameter `connectionIdlePollingInterval`. Monitoring is disabled by default to match previous behavior. This monitoring will switch to enabled by default in an upcoming minor version release.", | ||
"issues": [ | ||
"awslabs/aws-sdk-kotlin#1214" | ||
] | ||
} |
This file contains 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
119 changes: 119 additions & 0 deletions
119
...gine-okhttp/jvm/src/aws/smithy/kotlin/runtime/http/engine/okhttp/ConnectionIdleMonitor.kt
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,119 @@ | ||
/* | ||
* Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved. | ||
* SPDX-License-Identifier: Apache-2.0 | ||
*/ | ||
package aws.smithy.kotlin.runtime.http.engine.okhttp | ||
|
||
import aws.smithy.kotlin.runtime.telemetry.logging.Logger | ||
import aws.smithy.kotlin.runtime.telemetry.logging.logger | ||
import kotlinx.coroutines.CoroutineName | ||
import kotlinx.coroutines.CoroutineScope | ||
import kotlinx.coroutines.Dispatchers | ||
import kotlinx.coroutines.Job | ||
import kotlinx.coroutines.cancelAndJoin | ||
import kotlinx.coroutines.isActive | ||
import kotlinx.coroutines.launch | ||
import kotlinx.coroutines.runBlocking | ||
import okhttp3.Call | ||
import okhttp3.Connection | ||
import okhttp3.ConnectionListener | ||
import okhttp3.ExperimentalOkHttpApi | ||
import okhttp3.internal.closeQuietly | ||
import okio.EOFException | ||
import okio.buffer | ||
import okio.source | ||
import java.net.SocketException | ||
import java.net.SocketTimeoutException | ||
import java.util.concurrent.ConcurrentHashMap | ||
import kotlin.coroutines.coroutineContext | ||
import kotlin.time.Duration | ||
import kotlin.time.Duration.Companion.seconds | ||
import kotlin.time.measureTime | ||
|
||
@OptIn(ExperimentalOkHttpApi::class) | ||
internal class ConnectionIdleMonitor(val pollInterval: Duration) : ConnectionListener() { | ||
private val monitors = ConcurrentHashMap<Connection, Job>() | ||
|
||
private fun Call.callContext() = | ||
request() | ||
.tag(SdkRequestTag::class.java) | ||
?.callContext | ||
?: Dispatchers.IO | ||
|
||
override fun connectionAcquired(connection: Connection, call: Call) { | ||
// Non-locking map access is okay here because this code will only execute synchronously as part of a | ||
// `connectionAcquired` event and will be complete before any future `connectionReleased` event could fire for | ||
// the same connection. | ||
monitors.remove(connection)?.let { monitor -> | ||
5.seconds | ||
val context = call.callContext() | ||
val logger = context.logger<ConnectionIdleMonitor>() | ||
logger.trace { "Cancel monitoring for $connection" } | ||
|
||
// Use `runBlocking` because this _must_ finish before OkHttp goes to use the connection | ||
val cancelTime = measureTime { | ||
runBlocking(context) { monitor.cancelAndJoin() } | ||
} | ||
|
||
logger.trace { "Monitoring canceled for $connection in $cancelTime" } | ||
} | ||
} | ||
|
||
override fun connectionReleased(connection: Connection, call: Call) { | ||
val connId = System.identityHashCode(connection) | ||
val context = call.callContext() | ||
val scope = CoroutineScope(context) | ||
val logger = context.logger<ConnectionIdleMonitor>() | ||
val monitor = scope.launch(CoroutineName("okhttp-conn-monitor-for-$connId")) { | ||
doMonitor(connection, logger) | ||
} | ||
logger.trace { "Launched coroutine $monitor to monitor $connection" } | ||
|
||
// Non-locking map access is okay here because this code will only execute synchronously as part of a | ||
// `connectionReleased` event and will be complete before any future `connectionAcquired` event could fire for | ||
// the same connection. | ||
monitors[connection] = monitor | ||
} | ||
|
||
private suspend fun doMonitor(conn: Connection, logger: Logger) { | ||
val socket = conn.socket() | ||
val source = try { | ||
socket.source() | ||
} catch (_: SocketException) { | ||
logger.trace { "Socket for $conn closed before monitoring started. Skipping polling loop." } | ||
return | ||
}.buffer().peek() | ||
|
||
logger.trace { "Commence socket monitoring for $conn" } | ||
var resetTimeout = true | ||
val oldTimeout = socket.soTimeout | ||
|
||
try { | ||
socket.soTimeout = pollInterval.inWholeMilliseconds.toInt() | ||
|
||
while (coroutineContext.isActive) { | ||
try { | ||
source.readByte() // Blocking read; will take up to READ_TIMEOUT_MS to complete | ||
} catch (_: SocketTimeoutException) { | ||
// Socket is still alive | ||
} catch (_: EOFException) { | ||
logger.trace { "Socket for $conn was closed remotely" } | ||
socket.closeQuietly() | ||
resetTimeout = false | ||
return | ||
} | ||
} | ||
} catch (e: Throwable) { | ||
logger.warn(e) { "Failed to poll $conn. Ending polling loop. Connection may be unstable now." } | ||
} finally { | ||
if (resetTimeout) { | ||
logger.trace { "Attempting to reset soTimeout..." } | ||
try { | ||
conn.socket().soTimeout = oldTimeout | ||
} catch (e: Throwable) { | ||
logger.warn(e) { "Failed to reset socket timeout on $conn. Connection may be unstable now." } | ||
} | ||
} | ||
} | ||
} | ||
} |
This file contains 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
This file contains 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
38 changes: 38 additions & 0 deletions
38
...lient-engines/test-suite/jvm/src/aws/smithy/kotlin/runtime/http/test/suite/Connections.kt
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,38 @@ | ||
/* | ||
* Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved. | ||
* SPDX-License-Identifier: Apache-2.0 | ||
*/ | ||
package aws.smithy.kotlin.runtime.http.test.suite | ||
|
||
import io.ktor.server.application.Application | ||
import io.ktor.server.application.call | ||
import io.ktor.server.jetty.JettyApplicationCall | ||
import io.ktor.server.response.respondText | ||
import io.ktor.server.routing.RoutingApplicationCall | ||
import io.ktor.server.routing.post | ||
import io.ktor.server.routing.route | ||
import io.ktor.server.routing.routing | ||
import io.ktor.util.InternalAPI | ||
import io.ktor.utils.io.close | ||
import kotlinx.coroutines.delay | ||
import kotlinx.coroutines.launch | ||
import kotlin.time.Duration.Companion.seconds | ||
|
||
internal fun Application.connectionTests() { | ||
routing { | ||
route("connectionDrop") { | ||
@OptIn(InternalAPI::class) | ||
post { | ||
val routingCall = call as RoutingApplicationCall | ||
val jettyCall = routingCall.engineCall as JettyApplicationCall | ||
|
||
launch { | ||
delay(4.seconds) | ||
jettyCall.response.responseChannel().close() | ||
} | ||
|
||
jettyCall.respondText("Bar") | ||
} | ||
} | ||
} | ||
} |
This file contains 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
This file contains 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