https://kotlinlang.org logo
Title
g

Guilherme Delgado

05/31/2021, 12:54 PM
Hello, This is my KMM Ktor client engine setup:
actual fun getHttpEngine(): HttpClientEngine = OkHttp.create {
        OkHttpConfig().also {
            addInterceptor(chuckerInterceptor)
            it.config {
                readTimeout(60, TimeUnit.SECONDS)
                connectTimeout(60, TimeUnit.SECONDS)
            }
        }
    }
but for some reason I’m receiving
failed with exception: io.ktor.network.sockets.SocketTimeoutException: Socket timeout has expired [url=..., socket_timeout=unknown] ms
after 10 seconds. Should I add this timeout settings in HttpClient? Thanks.
r

russhwolf

05/31/2021, 2:03 PM
OkHttp.create { }
supplies its own config object as a
this
reference in the builder lambda. Try this instead
actual fun getHttpEngine(): HttpClientEngine = OkHttp.create {
        addInterceptor(chuckerInterceptor)
        config {
            readTimeout(60, TimeUnit.SECONDS)
            connectTimeout(60, TimeUnit.SECONDS)
        }
    }
g

Guilherme Delgado

05/31/2021, 3:31 PM
still getting
SocketTimeoutException
after 10s 😞
@russhwolf any other tip or check?
private fun createHttpClient(
    engine: HttpClientEngine,
    networkLogger: co.touchlab.kermit.Logger? = null,
    tokenProvider: TokenProvider,
    customModule: SerializersModule? = null
): HttpClient {
    return HttpClient(engine) {
        if (networkLogger != null) {
            install(Logging) {
                logger = object : Logger {
                    override fun log(message: String) {
                        networkLogger.v("HTTP Client", message)
                    }
                }
                level = LogLevel.ALL
            }
        }
        install(JsonFeature) {
            serializer = KotlinxSerializer(kotlinx.serialization.json.Json {
                isLenient = true
                ignoreUnknownKeys = true
                if (customModule != null) {
                    serializersModule = customModule
                }
            })
        }
        defaultRequest {
            header(HttpHeaders.Authorization, "Bearer ${tokenProvider.getAccessToken()}")
            header(HttpHeaders.Connection, "Close")
        }
    }
}
my full client setup ^
r

russhwolf

06/01/2021, 6:44 PM
not sure off-hand but there might be a timeout in the ktor configuration (rather than the okhttp configuration) that you need to set.
g

Guilherme Delgado

06/01/2021, 10:48 PM
install(HttpTimeout) {
    requestTimeoutMillis = 60000
    connectTimeoutMillis = 60000
    socketTimeoutMillis = 60000
}
well, this solved it ^^