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