From 5525bc4bd9e421289e5d9816e8b46dafb4331dad Mon Sep 17 00:00:00 2001 From: Vitor Pamplona Date: Wed, 2 Jul 2025 17:42:46 -0400 Subject: [PATCH] Increasing the thread pool for OkHttp in order to sustain more connections. --- .../service/okhttp/OkHttpClientFactory.kt | 31 +++++++++++++++++-- 1 file changed, 29 insertions(+), 2 deletions(-) diff --git a/amethyst/src/main/java/com/vitorpamplona/amethyst/service/okhttp/OkHttpClientFactory.kt b/amethyst/src/main/java/com/vitorpamplona/amethyst/service/okhttp/OkHttpClientFactory.kt index bd3e2aec8..769d2372e 100644 --- a/amethyst/src/main/java/com/vitorpamplona/amethyst/service/okhttp/OkHttpClientFactory.kt +++ b/amethyst/src/main/java/com/vitorpamplona/amethyst/service/okhttp/OkHttpClientFactory.kt @@ -20,6 +20,13 @@ */ package com.vitorpamplona.amethyst.service.okhttp +import android.util.Log +import com.vitorpamplona.amethyst.isDebug +import kotlinx.coroutines.Dispatchers +import kotlinx.coroutines.GlobalScope +import kotlinx.coroutines.delay +import kotlinx.coroutines.launch +import okhttp3.Dispatcher import okhttp3.OkHttpClient import java.net.InetSocketAddress import java.net.Proxy @@ -36,9 +43,29 @@ class OkHttpClientFactory( const val DEFAULT_TIMEOUT_ON_MOBILE_SECS: Int = 30 } + val logging = LoggingInterceptor() + val keyDecryptor = EncryptedBlobInterceptor(keyCache) + + val myDispatcher = + Dispatcher().apply { + maxRequests = 512 + } + + init { + if (isDebug) { + GlobalScope.launch(Dispatchers.IO) { + while (true) { + Log.d("OkHttpClientFactory", "Active threads ${myDispatcher.runningCallsCount()}") + delay(5000) + } + } + } + } + private val rootClient = OkHttpClient .Builder() + .dispatcher(myDispatcher) .followRedirects(true) .followSslRedirects(true) .build() @@ -57,8 +84,8 @@ class OkHttpClientFactory( .connectTimeout(duration) .writeTimeout(duration) .addInterceptor(DefaultContentTypeInterceptor(userAgent)) - .addNetworkInterceptor(LoggingInterceptor()) - .addNetworkInterceptor(EncryptedBlobInterceptor(keyCache)) + .addNetworkInterceptor(logging) + .addNetworkInterceptor(keyDecryptor) .build() }