From bd57e6a8fa516332b6eb75814fce1b23f26ed15c Mon Sep 17 00:00:00 2001 From: Nicola Corti Date: Tue, 17 Feb 2026 10:33:09 -0800 Subject: [PATCH] Add shared DevSupportHttpClient singleton (#55575) Summary: Pull Request resolved: https://github.com/facebook/react-native/pull/55575 The devsupport package creates multiple OkHttpClient instances across different files, each with its own connection pool and thread pool. This is wasteful and introduces bugs (thread-safety race in InspectorNetworkHelper, per-tap allocation in RedBoxContentView). This diff introduces DevSupportHttpClient, an internal object singleton with two shared clients derived from the same connection pool: - httpClient: with connect=5s, write=disabled, read=disabled (for HTTP requests) - websocketClient: derived via newBuilder() with connect=10s, write=10s, read=disabled (for WebSocket/inspector use) Subsequent diffs in this stack migrate each consumer to use these shared clients. Changelog: [Internal] Reviewed By: cipolleschi Differential Revision: D93480249 fbshipit-source-id: b489f5946aa0c0a5318f0fb600dec31f373b8c09 --- .../inspector/DevSupportHttpClient.kt | 35 +++++++++++++++++++ 1 file changed, 35 insertions(+) create mode 100644 packages/react-native/ReactAndroid/src/main/java/com/facebook/react/devsupport/inspector/DevSupportHttpClient.kt diff --git a/packages/react-native/ReactAndroid/src/main/java/com/facebook/react/devsupport/inspector/DevSupportHttpClient.kt b/packages/react-native/ReactAndroid/src/main/java/com/facebook/react/devsupport/inspector/DevSupportHttpClient.kt new file mode 100644 index 00000000000..997c3c971a5 --- /dev/null +++ b/packages/react-native/ReactAndroid/src/main/java/com/facebook/react/devsupport/inspector/DevSupportHttpClient.kt @@ -0,0 +1,35 @@ +/* + * Copyright (c) Meta Platforms, Inc. and affiliates. + * + * This source code is licensed under the MIT license found in the + * LICENSE file in the root directory of this source tree. + */ + +@file:Suppress("DEPRECATION_ERROR") // Conflicting okhttp versions + +package com.facebook.react.devsupport.inspector + +import java.util.concurrent.TimeUnit +import okhttp3.OkHttpClient + +/** + * Shared [OkHttpClient] instances for devsupport networking. Uses a single connection pool and + * dispatcher across all dev support HTTP and WebSocket usage. + */ +internal object DevSupportHttpClient { + /** Client for HTTP requests: connect=5s, write=disabled, read=disabled. */ + val httpClient: OkHttpClient = + OkHttpClient.Builder() + .connectTimeout(5, TimeUnit.SECONDS) + .writeTimeout(0, TimeUnit.MILLISECONDS) + .readTimeout(0, TimeUnit.MINUTES) + .build() + + /** Client for WebSocket connections: connect=10s, write=10s, read=disabled. */ + val websocketClient: OkHttpClient = + httpClient + .newBuilder() + .connectTimeout(10, TimeUnit.SECONDS) + .writeTimeout(10, TimeUnit.SECONDS) + .build() +}