Ir al contenido

HTTP Client Lifetime (ARCH8083)

Esta página aún no está disponible en tu idioma.

Key kotlin.performance.http-client-lifetime · Severity error · Category performance

Flags an HTTP client (OkHttpClient, HttpClient, Retrofit) constructed inside a function body - each call pays connection pool and TLS setup again.

fun fetch(url: String) = OkHttpClient().newCall(Request.Builder().url(url).build()).execute()
private val client = OkHttpClient()
fun fetch(url: String) = client.newCall(Request.Builder().url(url).build()).execute()

Create the client once (a property, a DI singleton) and reuse it.