Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Increase network timeouts and retries #226

Merged
merged 2 commits into from
Apr 15, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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)
Copy link
Contributor Author

@nplasterer nplasterer Apr 15, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This was duplicate code inside of buildFromV1Bundle so instead of trying to keep to copies of the code just call into the other one.

val address = v1Bundle.identityKey.publicKey.recoverWalletSignerPublicKey().walletAddress
val newOptions = options ?: ClientOptions()
val apiClient =
GRPCApiClient(
environment = newOptions.api.env,
secure = newOptions.api.isSecure,
)
val (v3Client, dbPath) = if (isAlphaMlsEnabled(options)) {
runBlocking {
ffiXmtpClient(
options,
account,
options?.appContext,
v1Bundle,
LegacyIdentitySource.STATIC,
address
)
}
} else Pair(null, "")
return Client(
address = address,
privateKeyBundleV1 = v1Bundle,
apiClient = apiClient,
libXMTPClient = v3Client,
dbPath = dbPath,
installationId = v3Client?.installationId()?.toHex() ?: ""
)

}

fun create(
Expand Down
Loading