diff --git a/library/src/main/java/org/xmtp/android/library/ApiClient.kt b/library/src/main/java/org/xmtp/android/library/ApiClient.kt index 283f15f5b..8f8f65233 100644 --- a/library/src/main/java/org/xmtp/android/library/ApiClient.kt +++ b/library/src/main/java/org/xmtp/android/library/ApiClient.kt @@ -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 @@ -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) diff --git a/library/src/main/java/org/xmtp/android/library/Client.kt b/library/src/main/java/org/xmtp/android/library/Client.kt index 11c1048c3..c1ddbd1b3 100644 --- a/library/src/main/java/org/xmtp/android/library/Client.kt +++ b/library/src/main/java/org/xmtp/android/library/Client.kt @@ -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(