Skip to content

KTOR-9554 Add dns config to OkHttp engine#5570

Merged
bjhham merged 1 commit intoktorio:mainfrom
fru1tworld:feat/okhttp-dns-config
May 5, 2026
Merged

KTOR-9554 Add dns config to OkHttp engine#5570
bjhham merged 1 commit intoktorio:mainfrom
fru1tworld:feat/okhttp-dns-config

Conversation

@fru1tworld
Copy link
Copy Markdown
Contributor

Subsystem
Client / OkHttp engine

Motivation
KTOR-462 / #1678 / #1924

OkHttp exposes a Dns interface but OkHttpConfig does not surface it as a first-class option. Users currently have to drop down to engine { config { dns(...) } } to inject a custom resolver (DNS-over-HTTPS, host overrides for tests, etc.).

Solution
Add OkHttpConfig.dns: Dns? and apply it in OkHttpEngine.createOkHttpClient right after proxy, mirroring the existing pattern. Defaults to nullDns.SYSTEM is used as before.

HttpClient(OkHttp) {
    engine {
        dns = Dns { hostname -> listOf(InetAddress.getByName("127.0.0.1")) }
    }
}

Scoped to the OkHttp engine; the broader unified resolver abstraction (KTOR-462) is out of scope here. The same per-engine pattern can follow for Apache5 in a separate PR.

@coderabbitai
Copy link
Copy Markdown
Contributor

coderabbitai Bot commented May 1, 2026

📝 Walkthrough

Walkthrough

Adds an optional dns property to OkHttpConfig (with public getters/setters), applies it to the OkHttp client builder when set, and adds tests verifying custom and default DNS behavior.

Changes

OkHttp DNS configuration

Layer / File(s) Summary
API Exposure
ktor-client/ktor-client-okhttp/api/ktor-client-okhttp.api
Adds public accessors getDns(): Lokhttp3/Dns and setDns(Lokhttp3/Dns): void for OkHttpConfig.
Configuration
ktor-client/ktor-client-okhttp/jvm/src/io/ktor/client/engine/okhttp/OkHttpConfig.kt
Introduces public var dns: Dns? = null with KDoc documenting behavior and defaulting to Dns.SYSTEM when null.
Core Wiring
ktor-client/ktor-client-okhttp/jvm/src/io/ktor/client/engine/okhttp/OkHttpEngine.kt
When config.dns is non-null, calls builder.dns(it) while building the OkHttpClient.
Tests
ktor-client/ktor-client-okhttp/jvm/test/io/ktor/client/engine/okhttp/OkHttpEngineTest.kt
Adds two tests: one asserting a provided custom Dns instance is applied to the cached OkHttpClient, another asserting default Dns.SYSTEM is preserved when dns is not set.

Estimated code review effort

🎯 3 (Moderate) | ⏱️ ~20 minutes

🚥 Pre-merge checks | ✅ 4 | ❌ 1

❌ Failed checks (1 warning)

Check name Status Explanation Resolution
Docstring Coverage ⚠️ Warning Docstring coverage is 0.00% which is insufficient. The required threshold is 80.00%. Write docstrings for the functions missing them to satisfy the coverage threshold.
✅ Passed checks (4 passed)
Check name Status Explanation
Description check ✅ Passed The description follows the required template with all sections properly filled: Subsystem (Client / OkHttp engine), Motivation (issue references and problem statement), and Solution (implementation approach with code example).
Linked Issues check ✅ Passed Check skipped because no linked issues were found for this pull request.
Out of Scope Changes check ✅ Passed Check skipped because no linked issues were found for this pull request.
Title check ✅ Passed The title clearly and accurately summarizes the main change: adding DNS configuration support to the OkHttp engine.

✏️ Tip: You can configure your own custom pre-merge checks in the settings.

✨ Finishing Touches
🧪 Generate unit tests (beta)
  • Create PR with unit tests

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.

❤️ Share

Comment @coderabbitai help to get the list of available commands and usage tips.

Copy link
Copy Markdown
Contributor

@coderabbitai coderabbitai Bot left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Actionable comments posted: 1

🤖 Prompt for all review comments with AI agents
Verify each finding against the current code and only fix it if needed.

Inline comments:
In
`@ktor-client/ktor-client-okhttp/jvm/test/io/ktor/client/engine/okhttp/OkHttpEngineTest.kt`:
- Around line 36-53: Tests create OkHttpEngine instances
(OkHttpEngine(OkHttpConfig()...) in the two test methods but never close them,
leaking dispatcher/executor resources; update both tests (`default dns is
preserved when dns config is not set` and the earlier test using customDns) to
ensure the engine is closed when the test finishes — either wrap engine creation
in a try/finally and call engine.close() in finally or use a resource-safe
construct so that the OkHttpEngine instance is closed after extracting
clientCache and performing assertions; reference the OkHttpEngine constructor,
OkHttpConfig usage and the clientCache reflection access when making the change.
🪄 Autofix (Beta)

Fix all unresolved CodeRabbit comments on this PR:

  • Push a commit to this branch (recommended)
  • Create a new PR with the fixes

ℹ️ Review info
⚙️ Run configuration

Configuration used: Path: .coderabbit.yaml

Review profile: CHILL

Plan: Pro

Run ID: 725984e3-f7fe-47d8-8a1a-b0f5488b7c79

📥 Commits

Reviewing files that changed from the base of the PR and between 295e688 and f6c88d9.

📒 Files selected for processing (4)
  • ktor-client/ktor-client-okhttp/api/ktor-client-okhttp.api
  • ktor-client/ktor-client-okhttp/jvm/src/io/ktor/client/engine/okhttp/OkHttpConfig.kt
  • ktor-client/ktor-client-okhttp/jvm/src/io/ktor/client/engine/okhttp/OkHttpEngine.kt
  • ktor-client/ktor-client-okhttp/jvm/test/io/ktor/client/engine/okhttp/OkHttpEngineTest.kt

Copy link
Copy Markdown
Contributor

@bjhham bjhham left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks for the contribution!

I wouldn't say it covers the ticket, however. The ticket refers to the address resolution in ktor-network which is used in the CIO engine. I'll create a new ticket with reference to supporting DNS configuration in OkHttp that better reflects the work done here.

Copy link
Copy Markdown
Contributor

@bjhham bjhham left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks for the contribution!

I wouldn't say it covers the ticket, however. The ticket refers to the address resolution in ktor-network which is used in the CIO engine. I'll create a new ticket with reference to supporting DNS configuration in OkHttp that better reflects the work done here.

@bjhham
Copy link
Copy Markdown
Contributor

bjhham commented May 4, 2026

Here's the new ticket to reflect the update to the OkHttp engine:

KTOR-9554 DNS configuration for OkHttp client engine

@fru1tworld fru1tworld force-pushed the feat/okhttp-dns-config branch from f6c88d9 to 12bca6c Compare May 5, 2026 05:57
@fru1tworld fru1tworld changed the title KTOR-462 Add dns config to OkHttp engine KTOR-9554 Add dns config to OkHttp engine May 5, 2026
@fru1tworld
Copy link
Copy Markdown
Contributor Author

Thanks for the review!
Updated to reference KTOR-9554 in the commit/title and addressed the resource-leak feedback

@bjhham bjhham merged commit 724f559 into ktorio:main May 5, 2026
15 of 17 checks passed
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