Testing the use of just one HttpClient for the entire app.

This commit is contained in:
Vitor Pamplona
2023-12-02 13:55:55 -05:00
parent 61449774a1
commit 1c0ec93cbb
5 changed files with 28 additions and 23 deletions

View File

@ -1772,7 +1772,7 @@ class Account(
?: Constants.defaultRelays.filter { defaultRelay -> defaultRelay.url == it.key }.firstOrNull()?.feedTypes ?: Constants.defaultRelays.filter { defaultRelay -> defaultRelay.url == it.key }.firstOrNull()?.feedTypes
?: FeedType.values().toSet() ?: FeedType.values().toSet()
Relay(it.key, it.value.read, it.value.write, localFeedTypes, proxy) Relay(it.key, it.value.read, it.value.write, localFeedTypes)
} ?: return null } ?: return null
// Ugly, but forces nostr.band as the only search-supporting relay today. // Ugly, but forces nostr.band as the only search-supporting relay today.
@ -1785,8 +1785,7 @@ class Account(
it.url, it.url,
it.read, it.read,
it.write, it.write,
it.feedTypes, it.feedTypes
proxy
) )
} }
} }
@ -1796,7 +1795,7 @@ class Account(
fun convertLocalRelays(): Array<Relay> { fun convertLocalRelays(): Array<Relay> {
return localRelays.map { return localRelays.map {
Relay(it.url, it.read, it.write, it.feedTypes, proxy) Relay(it.url, it.read, it.write, it.feedTypes)
}.toTypedArray() }.toTypedArray()
} }

View File

@ -14,6 +14,8 @@ object HttpClient {
var proxyChangeListeners = ArrayList<() -> Unit>() var proxyChangeListeners = ArrayList<() -> Unit>()
var defaultTimeout = DEFAULT_TIMEOUT_ON_WIFI var defaultTimeout = DEFAULT_TIMEOUT_ON_WIFI
var defaultHttpClient: OkHttpClient? = null
// fires off every time value of the property changes // fires off every time value of the property changes
private var internalProxy: Proxy? by Delegates.observable(null) { _, oldValue, newValue -> private var internalProxy: Proxy? by Delegates.observable(null) { _, oldValue, newValue ->
if (oldValue != newValue) { if (oldValue != newValue) {
@ -24,13 +26,17 @@ object HttpClient {
} }
fun start(proxy: Proxy?) { fun start(proxy: Proxy?) {
this.internalProxy = proxy if (internalProxy != proxy) {
this.internalProxy = proxy
this.defaultHttpClient = getHttpClient()
}
} }
fun changeTimeouts(timeout: Duration) { fun changeTimeouts(timeout: Duration) {
Log.d("HttpClient", "Changing timeout to: $timeout") Log.d("HttpClient", "Changing timeout to: $timeout")
if (this.defaultTimeout.seconds != timeout.seconds) { if (this.defaultTimeout.seconds != timeout.seconds) {
this.defaultTimeout = timeout this.defaultTimeout = timeout
this.defaultHttpClient = getHttpClient()
} }
} }
@ -42,11 +48,23 @@ object HttpClient {
.readTimeout(duration) .readTimeout(duration)
.connectTimeout(duration) .connectTimeout(duration)
.writeTimeout(duration) .writeTimeout(duration)
.followRedirects(true)
.followSslRedirects(true)
.build() .build()
} }
fun getHttpClientForRelays(): OkHttpClient {
if (this.defaultHttpClient == null) {
this.defaultHttpClient = getHttpClient(defaultTimeout)
}
return defaultHttpClient!!
}
fun getHttpClient(): OkHttpClient { fun getHttpClient(): OkHttpClient {
return getHttpClient(defaultTimeout) if (this.defaultHttpClient == null) {
this.defaultHttpClient = getHttpClient(defaultTimeout)
}
return defaultHttpClient!!
} }
fun getProxy(): Proxy? { fun getProxy(): Proxy? {

View File

@ -1,7 +1,6 @@
package com.vitorpamplona.amethyst.service.relays package com.vitorpamplona.amethyst.service.relays
import android.util.Log import android.util.Log
import com.vitorpamplona.amethyst.service.HttpClient
import com.vitorpamplona.amethyst.service.checkNotInMainThread import com.vitorpamplona.amethyst.service.checkNotInMainThread
import com.vitorpamplona.quartz.events.Event import com.vitorpamplona.quartz.events.Event
import com.vitorpamplona.quartz.events.EventInterface import com.vitorpamplona.quartz.events.EventInterface
@ -126,7 +125,7 @@ object Client : RelayPool.Listener {
@OptIn(DelicateCoroutinesApi::class) @OptIn(DelicateCoroutinesApi::class)
private fun newSporadicRelay(url: String, feedTypes: Set<FeedType>?, onConnected: (Relay) -> Unit, onDone: (() -> Unit)?) { private fun newSporadicRelay(url: String, feedTypes: Set<FeedType>?, onConnected: (Relay) -> Unit, onDone: (() -> Unit)?) {
val relay = Relay(url, true, true, feedTypes ?: emptySet(), HttpClient.getProxy()) val relay = Relay(url, true, true, feedTypes ?: emptySet())
RelayPool.addRelay(relay) RelayPool.addRelay(relay)
relay.connectAndRun { relay.connectAndRun {

View File

@ -1,7 +1,6 @@
package com.vitorpamplona.amethyst.service.relays package com.vitorpamplona.amethyst.service.relays
import com.vitorpamplona.amethyst.model.RelaySetupInfo import com.vitorpamplona.amethyst.model.RelaySetupInfo
import com.vitorpamplona.amethyst.service.HttpClient
object Constants { object Constants {
val activeTypes = setOf(FeedType.FOLLOWS, FeedType.PRIVATE_DMS) val activeTypes = setOf(FeedType.FOLLOWS, FeedType.PRIVATE_DMS)
@ -11,7 +10,7 @@ object Constants {
fun convertDefaultRelays(): Array<Relay> { fun convertDefaultRelays(): Array<Relay> {
return defaultRelays.map { return defaultRelays.map {
Relay(it.url, it.read, it.write, it.feedTypes, HttpClient.getProxy()) Relay(it.url, it.read, it.write, it.feedTypes)
}.toTypedArray() }.toTypedArray()
} }

View File

@ -2,6 +2,7 @@ package com.vitorpamplona.amethyst.service.relays
import android.util.Log import android.util.Log
import com.vitorpamplona.amethyst.BuildConfig import com.vitorpamplona.amethyst.BuildConfig
import com.vitorpamplona.amethyst.service.HttpClient
import com.vitorpamplona.amethyst.service.checkNotInMainThread import com.vitorpamplona.amethyst.service.checkNotInMainThread
import com.vitorpamplona.quartz.encoders.HexKey import com.vitorpamplona.quartz.encoders.HexKey
import com.vitorpamplona.quartz.events.Event import com.vitorpamplona.quartz.events.Event
@ -9,13 +10,10 @@ import com.vitorpamplona.quartz.events.EventInterface
import com.vitorpamplona.quartz.events.RelayAuthEvent import com.vitorpamplona.quartz.events.RelayAuthEvent
import com.vitorpamplona.quartz.events.bytesUsedInMemory import com.vitorpamplona.quartz.events.bytesUsedInMemory
import com.vitorpamplona.quartz.utils.TimeUtils import com.vitorpamplona.quartz.utils.TimeUtils
import okhttp3.OkHttpClient
import okhttp3.Request import okhttp3.Request
import okhttp3.Response import okhttp3.Response
import okhttp3.WebSocket import okhttp3.WebSocket
import okhttp3.WebSocketListener import okhttp3.WebSocketListener
import java.net.Proxy
import java.time.Duration
import java.util.concurrent.atomic.AtomicBoolean import java.util.concurrent.atomic.AtomicBoolean
enum class FeedType { enum class FeedType {
@ -28,22 +26,14 @@ class Relay(
var url: String, var url: String,
var read: Boolean = true, var read: Boolean = true,
var write: Boolean = true, var write: Boolean = true,
var activeTypes: Set<FeedType> = FeedType.values().toSet(), var activeTypes: Set<FeedType> = FeedType.values().toSet()
proxy: Proxy?
) { ) {
companion object { companion object {
// waits 3 minutes to reconnect once things fail // waits 3 minutes to reconnect once things fail
const val RECONNECTING_IN_SECONDS = 60 * 3 const val RECONNECTING_IN_SECONDS = 60 * 3
} }
private val httpClient = OkHttpClient.Builder() private val httpClient = HttpClient.getHttpClientForRelays()
.proxy(proxy)
.readTimeout(Duration.ofSeconds(if (proxy != null) 20L else 10L))
.connectTimeout(Duration.ofSeconds(if (proxy != null) 20L else 10L))
.writeTimeout(Duration.ofSeconds(if (proxy != null) 20L else 10L))
.followRedirects(true)
.followSslRedirects(true)
.build()
private var listeners = setOf<Listener>() private var listeners = setOf<Listener>()
private var socket: WebSocket? = null private var socket: WebSocket? = null