From 41766ad77df984842f6414720eab3b881e626b93 Mon Sep 17 00:00:00 2001 From: Tomek Zawadzki Date: Tue, 17 Nov 2026 21:53:31 +0100 Subject: [PATCH] Android: set up TLS once for the OkHttp clients React Native creates OkHttp resolves the platform trust manager and builds an SSLContext every time a client is built without an explicit SSL socket factory, which costs 10-20 ms of CPU on a mid-range phone. React Native builds at least two such clients per process (NetworkingModule and FrescoModule), so OkHttpClientProvider now does that once and derives the clients it creates from that client with newBuilder(), keeping a separate dispatcher, connection pool and cookie jar per client as before. Co-Authored-By: Claude Fable 5.1 --- .../modules/network/OkHttpClientProvider.kt | 16 +++++++++++++++- .../modules/network/OkHttpClientProviderTest.kt | 14 ++++++++++++++ 2 files changed, 29 insertions(+), 1 deletion(-) diff --git a/packages/react-native/ReactAndroid/src/main/java/com/facebook/react/modules/network/OkHttpClientProvider.kt b/packages/react-native/ReactAndroid/src/main/java/com/facebook/react/modules/network/OkHttpClientProvider.kt index b753b07b892e..d3117ccdf178 100644 --- a/packages/react-native/ReactAndroid/src/main/java/com/facebook/react/modules/network/OkHttpClientProvider.kt +++ b/packages/react-native/ReactAndroid/src/main/java/com/facebook/react/modules/network/OkHttpClientProvider.kt @@ -11,6 +11,8 @@ import android.content.Context import java.io.File import java.util.concurrent.TimeUnit import okhttp3.Cache +import okhttp3.ConnectionPool +import okhttp3.Dispatcher import okhttp3.OkHttpClient /** @@ -24,6 +26,15 @@ public object OkHttpClientProvider { // User-provided OkHttpClient factory internal var factory: OkHttpClientFactory? = null + // Building an OkHttpClient without an explicit SSL socket factory makes OkHttp resolve the + // platform trust manager (loading the system trust store) and create an SSLContext, which costs + // 10-20 ms of CPU on a mid-range phone. React Native builds at least two clients per process + // (the networking module and Fresco), so that setup is done once here and the clients created by + // createClientBuilder() derive from it with newBuilder(), which copies the socket factory, trust + // manager and certificate chain cleaner. Everything else (dispatcher, connection pool, cookie + // jar, timeouts) stays per client, exactly as with a fresh Builder. + private val tlsConfiguredClient: OkHttpClient by lazy { OkHttpClient.Builder().build() } + @JvmStatic public fun setOkHttpClientFactory(factory: OkHttpClientFactory?) { this.factory = factory @@ -48,7 +59,10 @@ public object OkHttpClientProvider { public fun createClientBuilder(): OkHttpClient.Builder { // No timeouts by default val client: OkHttpClient.Builder = - OkHttpClient.Builder() + tlsConfiguredClient + .newBuilder() + .dispatcher(Dispatcher()) + .connectionPool(ConnectionPool()) .connectTimeout(0, TimeUnit.MILLISECONDS) .readTimeout(0, TimeUnit.MILLISECONDS) .writeTimeout(0, TimeUnit.MILLISECONDS) diff --git a/packages/react-native/ReactAndroid/src/test/java/com/facebook/react/modules/network/OkHttpClientProviderTest.kt b/packages/react-native/ReactAndroid/src/test/java/com/facebook/react/modules/network/OkHttpClientProviderTest.kt index fad7383b5ceb..c4560ffb733b 100644 --- a/packages/react-native/ReactAndroid/src/test/java/com/facebook/react/modules/network/OkHttpClientProviderTest.kt +++ b/packages/react-native/ReactAndroid/src/test/java/com/facebook/react/modules/network/OkHttpClientProviderTest.kt @@ -88,6 +88,20 @@ class OkHttpClientProviderTest { assertThat(client.cookieJar()).isInstanceOf(ReactCookieJarContainer::class.java) } + @Test + fun testCreatedClientsShareTlsSetupButNotDispatcherOrPool() { + val first = OkHttpClientProvider.createClientBuilder().build() + val second = OkHttpClientProvider.createClientBuilder().build() + + // The platform trust manager / SSL socket factory are resolved once and reused ... + assertThat(second.sslSocketFactory()).isSameAs(first.sslSocketFactory()) + assertThat(second.x509TrustManager).isSameAs(first.x509TrustManager) + // ... while each client keeps its own dispatcher, connection pool and cookie jar. + assertThat(second.dispatcher()).isNotSameAs(first.dispatcher()) + assertThat(second.connectionPool()).isNotSameAs(first.connectionPool()) + assertThat(second.cookieJar()).isNotSameAs(first.cookieJar()) + } + @Test fun testCreateClientBuilderWithContextUsesDefaultCacheSize() { val mockContext = mock()