Skip to content

Commit

Permalink
Increase network timeouts and retries (#226)
Browse files Browse the repository at this point in the history
* add potential timeout fixes

* a few clean ups
  • Loading branch information
nplasterer authored Apr 15, 2024
1 parent fe5b6e0 commit f0f702f
Show file tree
Hide file tree
Showing 2 changed files with 32 additions and 38 deletions.
41 changes: 31 additions & 10 deletions library/src/main/java/org/xmtp/android/library/ApiClient.kt
Original file line number Diff line number Diff line change
@@ -1,10 +1,8 @@
package org.xmtp.android.library

import io.grpc.InsecureChannelCredentials
import io.grpc.ManagedChannel
import io.grpc.ManagedChannelBuilder
import io.grpc.Metadata
import io.grpc.TlsChannelCredentials
import io.grpc.okhttp.OkHttpChannelBuilder
import kotlinx.coroutines.flow.Flow
import org.xmtp.android.library.messages.Pagination
import org.xmtp.android.library.messages.Topic
Expand Down Expand Up @@ -87,16 +85,39 @@ data class GRPCApiClient(
): SubscribeRequest = SubscribeRequest.newBuilder().addAllContentTopics(topics).build()
}

private val retryPolicy = mapOf(
"methodConfig" to listOf(
mapOf(
"retryPolicy" to mapOf(
"maxAttempts" to 4.0,
"initialBackoff" to "0.5s",
"maxBackoff" to "30s",
"backoffMultiplier" to 2.0,
"retryableStatusCodes" to listOf(
"UNAVAILABLE",
"CANCELLED",
)
)
)
)
)

private val channel: ManagedChannel =
OkHttpChannelBuilder.forAddress(
ManagedChannelBuilder.forAddress(
environment.getValue(),
if (environment == XMTPEnvironment.LOCAL) 5556 else 443,
if (secure) {
TlsChannelCredentials.create()
if (environment == XMTPEnvironment.LOCAL) 5556 else 443
).apply {
keepAliveTime(30L, TimeUnit.SECONDS)
keepAliveTimeout(20L, TimeUnit.SECONDS)
keepAliveWithoutCalls(true)
if (environment != XMTPEnvironment.LOCAL) {
useTransportSecurity()
} else {
InsecureChannelCredentials.create()
},
).build()
usePlaintext()
}
defaultServiceConfig(retryPolicy)
enableRetry()
}.build()

private val client: MessageApiGrpcKt.MessageApiCoroutineStub =
MessageApiGrpcKt.MessageApiCoroutineStub(channel)
Expand Down
29 changes: 1 addition & 28 deletions library/src/main/java/org/xmtp/android/library/Client.kt
Original file line number Diff line number Diff line change
Expand Up @@ -183,34 +183,7 @@ class Client() {
options: ClientOptions? = null,
account: SigningKey? = null,
): Client {
val address = bundle.identityKey.publicKey.recoverWalletSignerPublicKey().walletAddress
val clientOptions = options ?: ClientOptions()
val apiClient =
GRPCApiClient(
environment = clientOptions.api.env,
secure = clientOptions.api.isSecure,
)
val (v3Client, dbPath) = if (isAlphaMlsEnabled(options)) {
runBlocking {
ffiXmtpClient(
options,
account,
options?.appContext,
bundle,
LegacyIdentitySource.STATIC,
address
)
}
} else Pair(null, " ")

return Client(
address = address,
privateKeyBundleV1 = bundle,
apiClient = apiClient,
libXMTPClient = v3Client,
dbPath = dbPath,
installationId = v3Client?.installationId()?.toHex() ?: ""
)
return buildFromV1Bundle(bundle, options, account)
}

fun create(
Expand Down

0 comments on commit f0f702f

Please sign in to comment.