From de9aeeae40a1a44c4788273910ce70ed3123ef42 Mon Sep 17 00:00:00 2001 From: Naomi Plasterer Date: Thu, 19 Dec 2024 09:11:54 -0800 Subject: [PATCH] remove the apiClient from the functions --- .../org/xmtp/android/library/ClientTest.kt | 14 +++--- .../java/org/xmtp/android/library/Client.kt | 44 +++++++++---------- 2 files changed, 25 insertions(+), 33 deletions(-) diff --git a/library/src/androidTest/java/org/xmtp/android/library/ClientTest.kt b/library/src/androidTest/java/org/xmtp/android/library/ClientTest.kt index c7da0007..9a5a3fd7 100644 --- a/library/src/androidTest/java/org/xmtp/android/library/ClientTest.kt +++ b/library/src/androidTest/java/org/xmtp/android/library/ClientTest.kt @@ -557,31 +557,27 @@ class ClientTest { val time3 = end3.time - start3.time Log.d("PERF", "Built a client with inboxId in ${time3 / 1000.0}s") + runBlocking { Client.connectToApiBackend(ClientOptions.Api(XMTPEnvironment.DEV, true)) } val start4 = Date() - val buildClient3 = runBlocking { - Client().build( - fakeWallet.address, + runBlocking { + Client().create( + PrivateKeyBuilder(), options = ClientOptions( ClientOptions.Api(XMTPEnvironment.DEV, true), appContext = context, dbEncryptionKey = key ), - inboxId = client.inboxId, - apiClient = client.apiClient ) } val end4 = Date() val time4 = end4.time - start4.time - Log.d("PERF", "Built a client with inboxId and apiClient in ${time4 / 1000.0}s") + Log.d("PERF", "Create a client after prebuilding apiClient in ${time4 / 1000.0}s") assert(time2 < time1) assert(time3 < time1) assert(time3 < time2) assert(time4 < time1) - assert(time4 < time2) - assert(time4 < time3) assertEquals(client.inboxId, buildClient1.inboxId) assertEquals(client.inboxId, buildClient2.inboxId) - assertEquals(client.inboxId, buildClient3.inboxId) } } 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 fd7393d5..74f338c0 100644 --- a/library/src/main/java/org/xmtp/android/library/Client.kt +++ b/library/src/main/java/org/xmtp/android/library/Client.kt @@ -1,7 +1,6 @@ package org.xmtp.android.library import android.content.Context -import com.google.protobuf.api import kotlinx.coroutines.runBlocking import org.xmtp.android.library.codecs.ContentCodec import org.xmtp.android.library.codecs.TextCodec @@ -49,7 +48,6 @@ class Client() { lateinit var conversations: Conversations lateinit var environment: XMTPEnvironment lateinit var dbPath: String - lateinit var apiClient: XmtpApiClient val libXMTPVersion: String = getVersionInfo() private lateinit var ffiClient: FfiXmtpClient @@ -62,17 +60,26 @@ class Client() { registry } + private val apiClientCache = mutableMapOf() + private val cacheLock = Any() + suspend fun connectToApiBackend(api: ClientOptions.Api): XmtpApiClient { - return connectToBackend(api.env.getUrl(), api.isSecure) + val cacheKey = api.env.getUrl() + return synchronized(cacheLock) { + apiClientCache.getOrPut(cacheKey) { + runBlocking { + connectToBackend(api.env.getUrl(), api.isSecure) + } + } + } } suspend fun getOrCreateInboxId( api: ClientOptions.Api, address: String, - apiClient: XmtpApiClient? = null, ): String { var inboxId = getInboxIdForAddress( - api = apiClient ?: connectToApiBackend(api), + api = connectToApiBackend(api), accountAddress = address.lowercase() ) if (inboxId.isNullOrBlank()) { @@ -89,7 +96,6 @@ class Client() { accountAddresses: List, appContext: Context, api: ClientOptions.Api, - apiClient: XmtpApiClient? = null, ): Map { val accountAddress = "0x0000000000000000000000000000000000000000" val inboxId = getOrCreateInboxId(api, accountAddress) @@ -100,7 +106,7 @@ class Client() { val dbPath = directoryFile.absolutePath + "/$alias.db3" val ffiClient = createClient( - api = apiClient ?: connectToApiBackend(api), + api = connectToApiBackend(api), db = dbPath, encryptionKey = null, accountAddress = accountAddress.lowercase(), @@ -125,7 +131,6 @@ class Client() { installationId: String, inboxId: String, environment: XMTPEnvironment, - apiClient: XmtpApiClient, ) : this() { this.address = address.lowercase() this.preferences = PrivatePreferences(client = this, ffiClient = libXMTPClient) @@ -136,7 +141,6 @@ class Client() { this.installationId = installationId this.inboxId = inboxId this.environment = environment - this.apiClient = apiClient } private suspend fun initializeV3Client( @@ -144,19 +148,17 @@ class Client() { clientOptions: ClientOptions, signingKey: SigningKey? = null, inboxId: String? = null, - apiClient: XmtpApiClient? = null, ): Client { val accountAddress = address.lowercase() val recoveredInboxId = - inboxId ?: getOrCreateInboxId(clientOptions.api, accountAddress, apiClient) + inboxId ?: getOrCreateInboxId(clientOptions.api, accountAddress) - val (ffiClient, dbPath, xmtpApiClient) = createFfiClient( + val (ffiClient, dbPath) = createFfiClient( accountAddress, recoveredInboxId, clientOptions, signingKey, clientOptions.appContext, - apiClient, ) return Client( @@ -166,7 +168,6 @@ class Client() { ffiClient.installationId().toHex(), ffiClient.inboxId(), clientOptions.api.env, - xmtpApiClient ) } @@ -174,10 +175,9 @@ class Client() { suspend fun create( account: SigningKey, options: ClientOptions, - apiClient: XmtpApiClient? = null, ): Client { return try { - initializeV3Client(account.address, options, account, apiClient = apiClient) + initializeV3Client(account.address, options, account) } catch (e: Exception) { throw XMTPException("Error creating V3 client: ${e.message}", e) } @@ -188,10 +188,9 @@ class Client() { address: String, options: ClientOptions, inboxId: String? = null, - apiClient: XmtpApiClient? = null, ): Client { return try { - initializeV3Client(address, options, inboxId = inboxId, apiClient = apiClient) + initializeV3Client(address, options, inboxId = inboxId) } catch (e: Exception) { throw XMTPException("Error creating V3 client: ${e.message}", e) } @@ -203,8 +202,7 @@ class Client() { options: ClientOptions, signingKey: SigningKey?, appContext: Context, - apiClient: XmtpApiClient? = null, - ): Triple { + ): Pair { val alias = "xmtp-${options.api.env}-$inboxId" val mlsDbDirectory = options.dbDirectory @@ -216,10 +214,8 @@ class Client() { directoryFile.mkdir() dbPath = directoryFile.absolutePath + "/$alias.db3" - val xmtpApiClient = - apiClient ?: connectToApiBackend(options.api) val ffiClient = createClient( - api = xmtpApiClient, + api = connectToApiBackend(options.api), db = dbPath, encryptionKey = options.dbEncryptionKey, accountAddress = accountAddress.lowercase(), @@ -239,7 +235,7 @@ class Client() { ?: throw XMTPException("No signer passed but signer was required.") ffiClient.registerIdentity(signatureRequest) } - return Triple(ffiClient, dbPath, xmtpApiClient) + return Pair(ffiClient, dbPath) } suspend fun revokeAllOtherInstallations(signingKey: SigningKey) {