Skip to content

Commit f0f702f

Browse files
authored
Increase network timeouts and retries (#226)
* add potential timeout fixes * a few clean ups
1 parent fe5b6e0 commit f0f702f

File tree

2 files changed

+32
-38
lines changed

2 files changed

+32
-38
lines changed

library/src/main/java/org/xmtp/android/library/ApiClient.kt

+31-10
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,8 @@
11
package org.xmtp.android.library
22

3-
import io.grpc.InsecureChannelCredentials
43
import io.grpc.ManagedChannel
4+
import io.grpc.ManagedChannelBuilder
55
import io.grpc.Metadata
6-
import io.grpc.TlsChannelCredentials
7-
import io.grpc.okhttp.OkHttpChannelBuilder
86
import kotlinx.coroutines.flow.Flow
97
import org.xmtp.android.library.messages.Pagination
108
import org.xmtp.android.library.messages.Topic
@@ -87,16 +85,39 @@ data class GRPCApiClient(
8785
): SubscribeRequest = SubscribeRequest.newBuilder().addAllContentTopics(topics).build()
8886
}
8987

88+
private val retryPolicy = mapOf(
89+
"methodConfig" to listOf(
90+
mapOf(
91+
"retryPolicy" to mapOf(
92+
"maxAttempts" to 4.0,
93+
"initialBackoff" to "0.5s",
94+
"maxBackoff" to "30s",
95+
"backoffMultiplier" to 2.0,
96+
"retryableStatusCodes" to listOf(
97+
"UNAVAILABLE",
98+
"CANCELLED",
99+
)
100+
)
101+
)
102+
)
103+
)
104+
90105
private val channel: ManagedChannel =
91-
OkHttpChannelBuilder.forAddress(
106+
ManagedChannelBuilder.forAddress(
92107
environment.getValue(),
93-
if (environment == XMTPEnvironment.LOCAL) 5556 else 443,
94-
if (secure) {
95-
TlsChannelCredentials.create()
108+
if (environment == XMTPEnvironment.LOCAL) 5556 else 443
109+
).apply {
110+
keepAliveTime(30L, TimeUnit.SECONDS)
111+
keepAliveTimeout(20L, TimeUnit.SECONDS)
112+
keepAliveWithoutCalls(true)
113+
if (environment != XMTPEnvironment.LOCAL) {
114+
useTransportSecurity()
96115
} else {
97-
InsecureChannelCredentials.create()
98-
},
99-
).build()
116+
usePlaintext()
117+
}
118+
defaultServiceConfig(retryPolicy)
119+
enableRetry()
120+
}.build()
100121

101122
private val client: MessageApiGrpcKt.MessageApiCoroutineStub =
102123
MessageApiGrpcKt.MessageApiCoroutineStub(channel)

library/src/main/java/org/xmtp/android/library/Client.kt

+1-28
Original file line numberDiff line numberDiff line change
@@ -183,34 +183,7 @@ class Client() {
183183
options: ClientOptions? = null,
184184
account: SigningKey? = null,
185185
): Client {
186-
val address = bundle.identityKey.publicKey.recoverWalletSignerPublicKey().walletAddress
187-
val clientOptions = options ?: ClientOptions()
188-
val apiClient =
189-
GRPCApiClient(
190-
environment = clientOptions.api.env,
191-
secure = clientOptions.api.isSecure,
192-
)
193-
val (v3Client, dbPath) = if (isAlphaMlsEnabled(options)) {
194-
runBlocking {
195-
ffiXmtpClient(
196-
options,
197-
account,
198-
options?.appContext,
199-
bundle,
200-
LegacyIdentitySource.STATIC,
201-
address
202-
)
203-
}
204-
} else Pair(null, " ")
205-
206-
return Client(
207-
address = address,
208-
privateKeyBundleV1 = bundle,
209-
apiClient = apiClient,
210-
libXMTPClient = v3Client,
211-
dbPath = dbPath,
212-
installationId = v3Client?.installationId()?.toHex() ?: ""
213-
)
186+
return buildFromV1Bundle(bundle, options, account)
214187
}
215188

216189
fun create(

0 commit comments

Comments
 (0)