From dddf65e60fba4ce4293ff832cd5dbc9610f732cf Mon Sep 17 00:00:00 2001 From: davotoula Date: Wed, 17 Sep 2025 11:10:10 +0200 Subject: [PATCH] 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 + } } /*