KTOR-6761 Add Apache5EngineConfig.configureConnectionManager to simplify configuration#5094
Conversation
…plify configuration
WalkthroughAdds a new DSL hook Changes
Estimated code review effort🎯 3 (Moderate) | ⏱️ ~20 minutes Suggested reviewers
Pre-merge checks and finishing touches❌ Failed checks (1 warning)
✅ Passed checks (2 passed)
✨ Finishing touches
🧪 Generate unit tests
Tip 👮 Agentic pre-merge checks are now available in preview!Pro plan users can now enable pre-merge checks in their settings to enforce checklists before merging PRs.
Please see the documentation for more information. Example: reviews:
pre_merge_checks:
custom_checks:
- name: "Undocumented Breaking Changes"
mode: "warning"
instructions: |
Pass/fail criteria: All breaking changes to public APIs, CLI flags, environment variables, configuration keys, database schemas, or HTTP/GraphQL endpoints must be documented in the "Breaking Change" section of the PR description and in CHANGELOG.md. Exclude purely internal or private changes (e.g., code not exported from package entry points or explicitly marked as internal).Please share your feedback with us on this Discord post. Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out. Comment |
…uration-Connection-Manager
bjhham
left a comment
There was a problem hiding this comment.
Looks like it will need an ABI dump, and you can point it to release/3.x if you want to include it in 3.3.1 but otherwise all good nice to see some older ones go 👍
There was a problem hiding this comment.
Actionable comments posted: 2
🧹 Nitpick comments (3)
ktor-client/ktor-client-tests/jvm/src/io/ktor/client/tests/HttpClientTest.kt (1)
77-91: Clamp negative delay to avoid IllegalArgumentException in delay()If a client passes a negative
{time},delay(time)will throw. Clamp to non‑negative.Apply this diff:
- get("delay/{time}") { - val time = call.parameters["time"]?.toLongOrNull() ?: 0L + get("delay/{time}") { + val time = (call.parameters["time"]?.toLongOrNull() ?: 0L).coerceAtLeast(0L)ktor-client/ktor-client-apache5/jvm/test/io/ktor/client/engine/apache5/Apache5HttpClientTest.kt (2)
28-39: Close clients in tests to avoid leaked threads/resourcesWrap client usage in
use { ... }or callclient.close()in afinallyblock.- val client = HttpClient(Apache5) { + val client = HttpClient(Apache5) { engine { configureConnectionManager = { setMaxConnPerRoute(1_000) setMaxConnTotal(2_000) } } install(HttpTimeout) { socketTimeoutMillis = 500 } - } + }.also { /* consider client.use { ... } pattern below */ }Or:
HttpClient(Apache5) { /* config */ }.use { client -> // test body }
50-51: Remove redundantUnitThe last
Unitis unnecessary in arunBlockingtest.- Unit
📜 Review details
Configuration used: CodeRabbit UI
Review profile: CHILL
Plan: Pro
📒 Files selected for processing (4)
ktor-client/ktor-client-apache5/jvm/src/io/ktor/client/engine/apache5/Apache5Engine.kt(1 hunks)ktor-client/ktor-client-apache5/jvm/src/io/ktor/client/engine/apache5/Apache5EngineConfig.kt(2 hunks)ktor-client/ktor-client-apache5/jvm/test/io/ktor/client/engine/apache5/Apache5HttpClientTest.kt(1 hunks)ktor-client/ktor-client-tests/jvm/src/io/ktor/client/tests/HttpClientTest.kt(2 hunks)
🧰 Additional context used
📓 Path-based instructions (2)
**/*.{kt,kts}
📄 CodeRabbit inference engine (CLAUDE.md)
**/*.{kt,kts}: Follow Kotlin official style guide for all Kotlin source and build scripts
Use star imports for io.ktor.* packages
Max line length is 120 characters
Indent with 4 spaces in Kotlin code
Include a copyright header in new Kotlin files
Files:
ktor-client/ktor-client-apache5/jvm/src/io/ktor/client/engine/apache5/Apache5Engine.ktktor-client/ktor-client-tests/jvm/src/io/ktor/client/tests/HttpClientTest.ktktor-client/ktor-client-apache5/jvm/src/io/ktor/client/engine/apache5/Apache5EngineConfig.ktktor-client/ktor-client-apache5/jvm/test/io/ktor/client/engine/apache5/Apache5HttpClientTest.kt
**/*.kt
📄 CodeRabbit inference engine (CLAUDE.md)
**/*.kt: Document all public Kotlin APIs, including parameters, return types, and exceptions
Annotate internal APIs with @internalapi
Follow Kotlin error-handling conventions and use specific Ktor exceptions
Files:
ktor-client/ktor-client-apache5/jvm/src/io/ktor/client/engine/apache5/Apache5Engine.ktktor-client/ktor-client-tests/jvm/src/io/ktor/client/tests/HttpClientTest.ktktor-client/ktor-client-apache5/jvm/src/io/ktor/client/engine/apache5/Apache5EngineConfig.ktktor-client/ktor-client-apache5/jvm/test/io/ktor/client/engine/apache5/Apache5HttpClientTest.kt
🧠 Learnings (1)
📚 Learning: 2025-09-05T12:46:14.074Z
Learnt from: bjhham
PR: ktorio/ktor#4887
File: ktor-server/ktor-server-jetty-jakarta/jvm/src/io/ktor/server/jetty/jakarta/JettyWebsocketConnection.kt:90-100
Timestamp: 2025-09-05T12:46:14.074Z
Learning: The ByteReadChannel.readAvailable(ByteBuffer) method in Ktor IO automatically calls awaitContent() internally when the read buffer is exhausted. When it returns 0, it has already suspended and waited for data to become available, so adding explicit awaitContent() calls is redundant and incorrect.
Applied to files:
ktor-client/ktor-client-tests/jvm/src/io/ktor/client/tests/HttpClientTest.kt
🧬 Code graph analysis (2)
ktor-client/ktor-client-tests/jvm/src/io/ktor/client/tests/HttpClientTest.kt (1)
ktor-client/ktor-client-core/common/src/io/ktor/client/plugins/HttpRequestRetry.kt (1)
delay(243-245)
ktor-client/ktor-client-apache5/jvm/test/io/ktor/client/engine/apache5/Apache5HttpClientTest.kt (1)
ktor-client/ktor-client-apache5/jvm/src/io/ktor/client/engine/apache5/Apache5Engine.kt (1)
engine(57-121)
🔇 Additional comments (1)
ktor-client/ktor-client-apache5/jvm/test/io/ktor/client/engine/apache5/Apache5HttpClientTest.kt (1)
56-69: This usage will keep working after the API tweakYour lambda returns the builder, but assigning it to a
Unitconfigurator remains source‑compatible; the return value is simply ignored.Please re-run the Apache5 tests after applying the API change to confirm no source breakages.
Isn't it prohibited to add new APIs in patch versions? While this change is forward-compatible it breaks backward compatibility. |
|
I have a similar question 😅 I want to include it in |
… to simplify configuration
There was a problem hiding this comment.
Actionable comments posted: 1
🧹 Nitpick comments (1)
ktor-client/ktor-client-apache5/api/ktor-client-apache5.api (1)
11-11: Optional: avoid exposing Apache builder type directly to future‑proof ABI.The Kotlin signature is a Function1 with a receiver of PoolingAsyncClientConnectionManagerBuilder. That type is baked into Kotlin metadata; if the underlying Apache builder type changes in the future, client code compiled against today’s metadata can face runtime ClassCastException. Consider introducing a Ktor-owned scope (thin wrapper) and accepting that instead, delegating to the Apache builder under the hood.
Example sketch:
// new public API surface you control interface Apache5ConnectionManagerBuilderScope { val delegate: org.apache.hc.client5.http.impl.nio.PoolingAsyncClientConnectionManagerBuilder // optionally expose only the knobs you want stable } // public API fun configureConnectionManager(block: Apache5ConnectionManagerBuilderScope.() -> Unit) // internal impl adapts the scope to the real builder
📜 Review details
Configuration used: CodeRabbit UI
Review profile: CHILL
Plan: Pro
📒 Files selected for processing (4)
ktor-client/ktor-client-apache5/api/ktor-client-apache5.api(1 hunks)ktor-client/ktor-client-apache5/jvm/src/io/ktor/client/engine/apache5/Apache5Engine.kt(1 hunks)ktor-client/ktor-client-apache5/jvm/src/io/ktor/client/engine/apache5/Apache5EngineConfig.kt(3 hunks)ktor-client/ktor-client-apache5/jvm/test/io/ktor/client/engine/apache5/Apache5HttpClientTest.kt(1 hunks)
🚧 Files skipped from review as they are similar to previous changes (3)
- ktor-client/ktor-client-apache5/jvm/src/io/ktor/client/engine/apache5/Apache5Engine.kt
- ktor-client/ktor-client-apache5/jvm/test/io/ktor/client/engine/apache5/Apache5HttpClientTest.kt
- ktor-client/ktor-client-apache5/jvm/src/io/ktor/client/engine/apache5/Apache5EngineConfig.kt
🧰 Additional context used
📓 Path-based instructions (1)
**/api/**
📄 CodeRabbit inference engine (CLAUDE.md)
Track all public API changes in the /api/ directories
Files:
ktor-client/ktor-client-apache5/api/ktor-client-apache5.api
Subsystem
Client, Apache5
Motivation
KTOR-6761 Apache5: Simplify configuration
Solution
Initially, the configuration looked like:
But after upgrading to
Apache5, the configuration started to look like:Because users need to create and set
ConnectionManagernow, it overrides the Ktor-managed connection manager here. For example, if theHttpTimeoutplugin is used, its settings will be ignored.The idea is to provide a
configureConnectionManagerfunction, so users won't overrideConnectionManagerbut still can configure it.