https://kotlinlang.org logo
#ktor
Title
# ktor
a

André Martins

02/22/2022, 10:51 AM
Hey, I’m using ktor-client-cio and trying to figure out if there is anyway to change dns resolver instead of using java network, I need to setup a timeout for the resolution and a retry count. Any ideias on where to look? (Seen this)
a

Aleksei Tirman [JB]

02/22/2022, 2:21 PM
I think it's impossible for CIO engine.
a

André Martins

02/22/2022, 3:19 PM
I’ve worked it out by creating a feature that intercepts before Engine phase on the sendPipeline and performs the resolution
a

Aleksei Tirman [JB]

02/22/2022, 4:59 PM
Could you please share a code snippet?
a

André Martins

02/22/2022, 5:11 PM
Ya sure
Copy code
override fun install(feature: DnsResolverFeature, scope: HttpClient) {
    scope.sendPipeline.insertPhaseBefore(HttpSendPipeline.Engine, DNSResolve)
    scope.sendPipeline.intercept(DNSResolve) {
        val host = context.host
        repeat(retries) {
            try {
                val addr = withTimeout(timeout) {
                    withContext(<http://Dispatchers.IO|Dispatchers.IO>) {
                        InetAddress.getByName(host)
                    }
                }

                context.host = addr.hostAddress
                return@repeat
            } catch (ex: TimeoutCancellationException) {
                <http://logger.info|logger.info>("Timeout occurred while resolving the IP address for host: $host")
                throw UnknownHostException(host)
            }
        }
    }
}
Do you know how can I make sure ktor-client checks for stale connections?
a

Aleksei Tirman [JB]

02/22/2022, 5:16 PM
While using your code the
Host
header in a request will contain an original hostname or an IP-address?
a

André Martins

02/22/2022, 5:22 PM
I mean let me check
Ya it takes the ip address, not sure in which phase it generates the headers
a

Aleksei Tirman [JB]

02/23/2022, 7:59 AM
As I can see from the CIO engine implementation, you can manually pass a Host header.
a

André Martins

02/23/2022, 9:49 AM
Ya doing that now
9 Views