HTTP Client Lifetime (ARCH8083)
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.
Incorrect
Section titled “Incorrect”fun fetch(url: String) = OkHttpClient().newCall(Request.Builder().url(url).build()).execute()Correct
Section titled “Correct”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.