Skip to content

Commit 40f60ea

Browse files
authored
feat: rework max connections for OkHttp engine (#795)
1 parent ed2195b commit 40f60ea

File tree

4 files changed

+30
-2
lines changed

4 files changed

+30
-2
lines changed
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
{
2+
"id": "73e8719a-3ed4-4316-a1a6-d673f1c85282",
3+
"type": "bugfix",
4+
"description": "Correctly apply `maxConnections` configuration setting to OkHttp engines"
5+
}
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
{
2+
"id": "f71de744-44c0-4404-8d93-04ecda57650b",
3+
"type": "feature",
4+
"description": "Add new `maxConnectionsPerHost` configuration setting for OkHttp engines"
5+
}

runtime/protocol/http-client-engines/http-client-engine-okhttp/jvm/src/aws/smithy/kotlin/runtime/http/engine/okhttp/OkHttpEngine.kt

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -70,20 +70,29 @@ private fun OkHttpEngineConfig.buildClient(): OkHttpClient {
7070
followSslRedirects(false)
7171

7272
// see: https://github.com/ktorio/ktor/issues/1708#issuecomment-609988128
73+
// TODO disable this once better transient exception handling is in place
7374
retryOnConnectionFailure(true)
7475

7576
connectTimeout(config.connectTimeout.toJavaDuration())
7677
readTimeout(config.socketReadTimeout.toJavaDuration())
7778
writeTimeout(config.socketWriteTimeout.toJavaDuration())
7879

79-
// use our own pool configured with the settings taken from config
80+
// use our own pool configured with the timeout settings taken from config
8081
val pool = ConnectionPool(
81-
maxIdleConnections = config.maxConnections.toInt(),
82+
maxIdleConnections = 5, // The default from the no-arg ConnectionPool() constructor
8283
keepAliveDuration = config.connectionIdleTimeout.inWholeMilliseconds,
8384
TimeUnit.MILLISECONDS,
8485
)
8586
connectionPool(pool)
8687

88+
// Configure a dispatcher that uses maxConnections as a proxy for maxRequests. Note that this isn't precisely
89+
// the same since some protocols (e.g., HTTP2) may use a single connection for multiple requests.
90+
val dispatcher = Dispatcher().apply {
91+
maxRequests = config.maxConnections.toInt()
92+
maxRequestsPerHost = config.maxConnectionsPerHost.toInt()
93+
}
94+
dispatcher(dispatcher)
95+
8796
// Log events coming from okhttp. Allocate a new listener per-call to facilitate dedicated trace spans.
8897
eventListenerFactory { call -> HttpEngineEventListener(pool, config.hostResolver, call) }
8998

runtime/protocol/http-client-engines/http-client-engine-okhttp/jvm/src/aws/smithy/kotlin/runtime/http/engine/okhttp/OkHttpEngineConfig.kt

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,11 @@ package aws.smithy.kotlin.runtime.http.engine.okhttp
88
import aws.smithy.kotlin.runtime.http.engine.HttpClientEngineConfig
99

1010
public class OkHttpEngineConfig private constructor(builder: Builder) : HttpClientEngineConfig(builder) {
11+
/**
12+
* The maximum number of connections to open to a single host.
13+
*/
14+
public val maxConnectionsPerHost: UInt = builder.maxConnectionsPerHost ?: builder.maxConnections
15+
1116
public companion object {
1217
/**
1318
* The default engine config. Most clients should use this.
@@ -19,6 +24,10 @@ public class OkHttpEngineConfig private constructor(builder: Builder) : HttpClie
1924
}
2025

2126
public class Builder : HttpClientEngineConfig.Builder() {
27+
/**
28+
* The maximum number of connections to open to a single host. Defaults to [maxConnections].
29+
*/
30+
public var maxConnectionsPerHost: UInt? = null
2231

2332
internal fun build(): OkHttpEngineConfig = OkHttpEngineConfig(this)
2433
}

0 commit comments

Comments
 (0)