From dddf65e60fba4ce4293ff832cd5dbc9610f732cf Mon Sep 17 00:00:00 2001 From: davotoula Date: Wed, 17 Sep 2025 11:10:10 +0200 Subject: [PATCH 1/4] Added test for emulator to set maxRequests to 32 to prevent crashing emulator Increased maxRequestsPerHost in release version to 20 from default 5 to allow for more concurrent connections to a relay (subscribing to timeline + mentions + DMs + profile updates + notifications, etc..) --- .../service/okhttp/OkHttpClientFactory.kt | 31 ++++++++++++++++++- 1 file changed, 30 insertions(+), 1 deletion(-) 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 9489f8a06..b031362d8 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,8 @@ */ package com.vitorpamplona.amethyst.service.okhttp +import android.os.Build +import android.util.Log import okhttp3.Dispatcher import okhttp3.OkHttpClient import java.net.InetSocketAddress @@ -35,6 +37,26 @@ class OkHttpClientFactory( const val DEFAULT_IS_MOBILE: Boolean = false const val DEFAULT_TIMEOUT_ON_WIFI_SECS: Int = 10 const val DEFAULT_TIMEOUT_ON_MOBILE_SECS: Int = 30 + const val MAX_REQUESTS_EMULATOR: Int = 32 + const val MAX_REQUESTS_DEVICE: Int = 512 + const val MAX_REQUESTS_PER_HOST_EMULATOR: Int = 5 + const val MAX_REQUESTS_PER_HOST_DEVICE: Int = 20 + + private fun isEmulator(): Boolean = + Build.FINGERPRINT.startsWith("generic") || + Build.FINGERPRINT.lowercase().contains("emulator") || + Build.MODEL.contains("google_sdk") || + Build.MODEL.lowercase().contains("droid4x") || + Build.MODEL.contains("Emulator") || + Build.MODEL.contains("Android SDK built for x86") || + Build.MANUFACTURER.contains("Genymotion") || + (Build.BRAND.startsWith("generic") && Build.DEVICE.startsWith("generic")) || + "google_sdk" == Build.PRODUCT || + Build.HARDWARE.contains("goldfish") || + Build.HARDWARE.contains("ranchu") || + Build.HARDWARE.contains("vbox86") || + Build.HARDWARE.contains("nox") || + Build.HARDWARE.contains("cuttlefish") } val logging = LoggingInterceptor() @@ -42,7 +64,14 @@ class OkHttpClientFactory( val myDispatcher = Dispatcher().apply { - maxRequests = 512 + if (isEmulator()) { + maxRequests = MAX_REQUESTS_EMULATOR + maxRequestsPerHost = MAX_REQUESTS_PER_HOST_EMULATOR + Log.i("OkHttpClientFactory", "Emulator detected, using reduced maxRequests: $MAX_REQUESTS_EMULATOR, maxRequestsPerHost: $MAX_REQUESTS_PER_HOST_EMULATOR") + } else { + maxRequests = MAX_REQUESTS_DEVICE + maxRequestsPerHost = MAX_REQUESTS_PER_HOST_DEVICE + } } /* From 6440b86a5e873fe9febc76c058e98b8a5c98b045 Mon Sep 17 00:00:00 2001 From: davotoula Date: Wed, 17 Sep 2025 11:10:40 +0200 Subject: [PATCH 2/4] remove commented out code --- .../amethyst/service/okhttp/OkHttpClientFactory.kt | 14 -------------- 1 file changed, 14 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 b031362d8..aca48eff6 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 @@ -74,20 +74,6 @@ class OkHttpClientFactory( } } - /* - DEBUG OK HTTP connections here. - init { - if (isDebug) { - GlobalScope.launch(Dispatchers.IO) { - while (true) { - Log.d("OkHttpClientFactory", "Active threads ${myDispatcher.runningCallsCount()}") - delay(5000) - } - } - } - } - */ - private val rootClient = OkHttpClient .Builder() From c1012bd859eeee4ec574ca3f2f12fb2040df1f2f Mon Sep 17 00:00:00 2001 From: davotoula Date: Wed, 17 Sep 2025 11:22:28 +0200 Subject: [PATCH 3/4] let's remove the maxRequestsPerHost increase, can be done in a separate PR --- .../amethyst/service/okhttp/OkHttpClientFactory.kt | 6 +----- 1 file changed, 1 insertion(+), 5 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 aca48eff6..498cffe54 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 @@ -39,8 +39,6 @@ class OkHttpClientFactory( const val DEFAULT_TIMEOUT_ON_MOBILE_SECS: Int = 30 const val MAX_REQUESTS_EMULATOR: Int = 32 const val MAX_REQUESTS_DEVICE: Int = 512 - const val MAX_REQUESTS_PER_HOST_EMULATOR: Int = 5 - const val MAX_REQUESTS_PER_HOST_DEVICE: Int = 20 private fun isEmulator(): Boolean = Build.FINGERPRINT.startsWith("generic") || @@ -66,11 +64,9 @@ class OkHttpClientFactory( Dispatcher().apply { if (isEmulator()) { maxRequests = MAX_REQUESTS_EMULATOR - maxRequestsPerHost = MAX_REQUESTS_PER_HOST_EMULATOR - Log.i("OkHttpClientFactory", "Emulator detected, using reduced maxRequests: $MAX_REQUESTS_EMULATOR, maxRequestsPerHost: $MAX_REQUESTS_PER_HOST_EMULATOR") + Log.i("OkHttpClientFactory", "Emulator detected, using reduced maxRequests: $MAX_REQUESTS_EMULATOR.") } else { maxRequests = MAX_REQUESTS_DEVICE - maxRequestsPerHost = MAX_REQUESTS_PER_HOST_DEVICE } } From 2027c7d77dd876442d4bb700706ea62fac3f0fc7 Mon Sep 17 00:00:00 2001 From: davotoula Date: Wed, 17 Sep 2025 15:01:46 +0200 Subject: [PATCH 4/4] use default settings (64) when in emulator --- .../amethyst/service/okhttp/OkHttpClientFactory.kt | 9 +++------ 1 file changed, 3 insertions(+), 6 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 498cffe54..ec055e448 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 @@ -37,8 +37,6 @@ class OkHttpClientFactory( const val DEFAULT_IS_MOBILE: Boolean = false const val DEFAULT_TIMEOUT_ON_WIFI_SECS: Int = 10 const val DEFAULT_TIMEOUT_ON_MOBILE_SECS: Int = 30 - const val MAX_REQUESTS_EMULATOR: Int = 32 - const val MAX_REQUESTS_DEVICE: Int = 512 private fun isEmulator(): Boolean = Build.FINGERPRINT.startsWith("generic") || @@ -62,11 +60,10 @@ class OkHttpClientFactory( val myDispatcher = Dispatcher().apply { - if (isEmulator()) { - maxRequests = MAX_REQUESTS_EMULATOR - Log.i("OkHttpClientFactory", "Emulator detected, using reduced maxRequests: $MAX_REQUESTS_EMULATOR.") + if (!isEmulator()) { + maxRequests = 512 } else { - maxRequests = MAX_REQUESTS_DEVICE + Log.i("OkHttpClientFactory", "Emulator detected, using default maxRequests: 64.") } }