feat(android): Create default OkHttpClient only when needed (#4411)

This commit is contained in:
Jaakko Malkki
2026-07-21 18:45:06 +03:00
committed by GitHub
parent d866b289cc
commit 63293b3b2e
2 changed files with 33 additions and 10 deletions

View File

@@ -1,5 +1,7 @@
package org.maplibre.android.module.http;
import static org.maplibre.android.module.http.HttpRequestUtil.toHumanReadableAscii;
import android.os.Build;
import android.text.TextUtils;
import android.util.Log;
@@ -22,7 +24,6 @@ import java.net.NoRouteToHostException;
import java.net.ProtocolException;
import java.net.SocketException;
import java.net.UnknownHostException;
import java.util.Objects;
import javax.net.ssl.SSLException;
@@ -35,8 +36,6 @@ import okhttp3.Request;
import okhttp3.Response;
import okhttp3.ResponseBody;
import static org.maplibre.android.module.http.HttpRequestUtil.toHumanReadableAscii;
public class HttpRequestImpl implements HttpRequest {
private static final String userAgentString = toHumanReadableAscii(
@@ -49,10 +48,10 @@ public class HttpRequestImpl implements HttpRequest {
);
@VisibleForTesting
static final OkHttpClient DEFAULT_CLIENT = new OkHttpClient.Builder().dispatcher(getDispatcher()).build();
static volatile OkHttpClient defaultClient = null;
@VisibleForTesting
static Call.Factory client = DEFAULT_CLIENT;
static volatile Call.Factory client = null;
private Call call;
@@ -87,7 +86,7 @@ public class HttpRequestImpl implements HttpRequest {
}
final Request request = builder.build();
call = client.newCall(request);
call = getHttpClient().newCall(request);
call.enqueue(callback);
} catch (Exception exception) {
callback.handleFailure(call, exception);
@@ -113,7 +112,7 @@ public class HttpRequestImpl implements HttpRequest {
}
public static void setOkHttpClient(@Nullable Call.Factory client) {
HttpRequestImpl.client = Objects.requireNonNullElse(client, DEFAULT_CLIENT);
HttpRequestImpl.client = client != null ? client : getOrCreateDefaultClient();
}
private static class OkHttpCallback implements Callback {
@@ -188,6 +187,27 @@ public class HttpRequestImpl implements HttpRequest {
}
}
private static Call.Factory getHttpClient() {
if (client == null) {
synchronized (HttpRequestImpl.class) {
if (client == null) {
client = getOrCreateDefaultClient();
}
}
}
return client;
}
@NonNull
private static synchronized OkHttpClient getOrCreateDefaultClient() {
if (defaultClient == null) {
defaultClient = new OkHttpClient.Builder().dispatcher(getDispatcher()).build();
}
return defaultClient;
}
@NonNull
private static Dispatcher getDispatcher() {
Dispatcher dispatcher = new Dispatcher();

View File

@@ -5,6 +5,8 @@ import org.maplibre.android.utils.ConfigUtils
import io.mockk.mockk
import okhttp3.Call
import org.junit.Assert.assertEquals
import org.junit.Assert.assertNotNull
import org.junit.Assert.assertNull
import org.junit.Test
import org.junit.runner.RunWith
import org.maplibre.android.BaseTest
@@ -17,10 +19,10 @@ class HttpRequestUtilTest : BaseTest() {
fun replaceHttpClient() {
MapLibreInjector.inject(mockk(relaxed = true), "", ConfigUtils.getMockedOptions())
assertEquals(HttpRequestImpl.DEFAULT_CLIENT, HttpRequestImpl.client)
val httpMock = mockk<Call.Factory>()
HttpRequestUtil.setOkHttpClient(httpMock)
assertNull("Default HTTP client should not be initialized", HttpRequestImpl.defaultClient)
assertEquals(
"Http client should have set to the mocked client",
httpMock,
@@ -28,9 +30,10 @@ class HttpRequestUtilTest : BaseTest() {
)
HttpRequestUtil.setOkHttpClient(null)
assertNotNull("Default HTTP client should be initialized", HttpRequestImpl.defaultClient)
assertEquals(
"Http client should have been reset to the default client",
HttpRequestImpl.DEFAULT_CLIENT,
HttpRequestImpl.defaultClient,
HttpRequestImpl.client
)