mirror of
https://github.com/vitorpamplona/amethyst.git
synced 2025-11-10 13:17:34 +01:00
Speeding up OkHttp startup
This commit is contained in:
@@ -43,25 +43,25 @@ class DualHttpClientManager(
|
||||
keyCache: EncryptionKeyCache,
|
||||
scope: CoroutineScope,
|
||||
) : IHttpClientManager {
|
||||
val factory = OkHttpClientFactory(keyCache)
|
||||
val factory = OkHttpClientFactory(keyCache, userAgent)
|
||||
|
||||
val defaultHttpClient: StateFlow<OkHttpClient> =
|
||||
combine(proxyPortProvider, isMobileDataProvider) { proxy, mobile ->
|
||||
factory.buildHttpClient(proxy, mobile, userAgent)
|
||||
factory.buildHttpClient(proxy, mobile)
|
||||
}.stateIn(
|
||||
scope,
|
||||
SharingStarted.WhileSubscribed(1000),
|
||||
factory.buildHttpClient(proxyPortProvider.value, isMobileDataProvider.value, userAgent),
|
||||
factory.buildHttpClient(proxyPortProvider.value, isMobileDataProvider.value),
|
||||
)
|
||||
|
||||
val defaultHttpClientWithoutProxy: StateFlow<OkHttpClient> =
|
||||
isMobileDataProvider
|
||||
.map { mobile ->
|
||||
factory.buildHttpClient(mobile, userAgent)
|
||||
factory.buildHttpClient(mobile)
|
||||
}.stateIn(
|
||||
scope,
|
||||
SharingStarted.WhileSubscribed(1000),
|
||||
factory.buildHttpClient(isMobileDataProvider.value, userAgent),
|
||||
factory.buildHttpClient(isMobileDataProvider.value),
|
||||
)
|
||||
|
||||
fun getCurrentProxy(): Proxy? = defaultHttpClient.value.proxy
|
||||
|
||||
@@ -30,6 +30,7 @@ import java.time.Duration
|
||||
|
||||
class OkHttpClientFactory(
|
||||
keyCache: EncryptionKeyCache,
|
||||
val userAgent: String,
|
||||
) {
|
||||
companion object {
|
||||
// by picking a random proxy port, the connection will fail as it should.
|
||||
@@ -74,12 +75,14 @@ class OkHttpClientFactory(
|
||||
.dispatcher(myDispatcher)
|
||||
.followRedirects(true)
|
||||
.followSslRedirects(true)
|
||||
.addInterceptor(DefaultContentTypeInterceptor(userAgent))
|
||||
.addNetworkInterceptor(logging)
|
||||
.addNetworkInterceptor(keyDecryptor)
|
||||
.build()
|
||||
|
||||
fun buildHttpClient(
|
||||
proxy: Proxy?,
|
||||
timeoutSeconds: Int,
|
||||
userAgent: String,
|
||||
): OkHttpClient {
|
||||
val seconds = if (proxy != null) timeoutSeconds * 3 else timeoutSeconds
|
||||
return rootClient
|
||||
@@ -88,31 +91,22 @@ class OkHttpClientFactory(
|
||||
.connectTimeout(Duration.ofSeconds(seconds.toLong()))
|
||||
.readTimeout(Duration.ofSeconds(seconds.toLong() * 3))
|
||||
.writeTimeout(Duration.ofSeconds(seconds.toLong() * 3))
|
||||
.addInterceptor(DefaultContentTypeInterceptor(userAgent))
|
||||
.addNetworkInterceptor(logging)
|
||||
.addNetworkInterceptor(keyDecryptor)
|
||||
.build()
|
||||
}
|
||||
|
||||
fun buildHttpClient(
|
||||
localSocksProxyPort: Int?,
|
||||
isMobile: Boolean?,
|
||||
userAgent: String,
|
||||
): OkHttpClient =
|
||||
buildHttpClient(
|
||||
buildLocalSocksProxy(localSocksProxyPort),
|
||||
buildTimeout(isMobile ?: DEFAULT_IS_MOBILE),
|
||||
userAgent,
|
||||
)
|
||||
|
||||
fun buildHttpClient(
|
||||
isMobile: Boolean?,
|
||||
userAgent: String,
|
||||
): OkHttpClient =
|
||||
fun buildHttpClient(isMobile: Boolean?): OkHttpClient =
|
||||
buildHttpClient(
|
||||
null,
|
||||
buildTimeout(isMobile ?: DEFAULT_IS_MOBILE),
|
||||
userAgent,
|
||||
)
|
||||
|
||||
fun buildTimeout(isMobile: Boolean): Int =
|
||||
|
||||
Reference in New Issue
Block a user