Ktor: Option to set custom timeout for request

Created on 15 May 2019  路  7Comments  路  Source: ktorio/ktor

Ktor Version

1.2.0

Ktor Engine Used(client or server and name)

CIO client

Feedback

Is it possible to set a custom timeout for _request_, not _client_? Something like

client.call("url", timeout = 20) { ... }

For now, I see only feature to set timeout for client:

HttpClient(CIO) {
    engine { requestTimeout = 20 }
}

My use case

I want to use the same client for long polling and common requests.

duplicate enhancement

Most helpful comment

All 7 comments

Yes, this is sorely needed. A workaround for now appears to be to set a very high client timeout, and then use withTimeout around all calls to specify a lower specific value. This is not very elegant though.

@rocketraman Is it possible to set a high timeout in a mpp way ? I've seen platform specific solution such as:

val client = HttpClient(Apache) {
    engine {
        /**
         * Max time between TCP packets - default 10 seconds.
         */
        socketTimeout = 10_000
    }
}

But I'd really like to keep my code platform agnostic as much as possible

Sure! It'll be possible to do with the HttpTimeout feature.

Sure! It'll be possible to do with the HttpTimeout feature.

Being a feature, that will presumably not allow per-request timeout specs. Will using withTimeout at the coroutine level then be the preferred approach for specifying per-request timeouts?

not allow per-request timeout

Not extact. The feature will add the extensions to the request builder.

Is there any plan when this feature will be implemented? We have single ktor client instance and we need shorter timeout than 10seconds for some requests. Currently we use withTimeout at coroutine level but it works only in android and not in iOS which prevents us to use it in production.

Was this page helpful?
0 / 5 - 0 ratings