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..)
This commit is contained in:
davotoula
2025-09-17 11:10:10 +02:00
parent 80c884a4d1
commit dddf65e60f

View File

@@ -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
}
}
/*