Skip to content

Commit b8745ea

Browse files
Remove Android network retries (#1579)
Network retries are implemented in the application layer Relates-To: OCMAM-396 Signed-off-by: Mykhailo Kuchma <[email protected]>
1 parent e3708f2 commit b8745ea

File tree

3 files changed

+11
-89
lines changed

3 files changed

+11
-89
lines changed

olp-cpp-sdk-core/src/http/android/HttpClient.java

Lines changed: 8 additions & 45 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright (C) 2019 HERE Europe B.V.
2+
* Copyright (C) 2019-2025 HERE Europe B.V.
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -103,8 +103,7 @@ public Request(
103103
byte[] postData,
104104
String proxyServer,
105105
int proxyPort,
106-
int proxyType,
107-
int maxRetries) {
106+
int proxyType) {
108107
this.url = url;
109108
this.verb = verb;
110109
this.requestId = requestId;
@@ -140,7 +139,6 @@ public Request(
140139
this.proxyType = Proxy.Type.HTTP;
141140
break;
142141
}
143-
this.maxRetries = maxRetries;
144142
}
145143

146144
public final String url() {
@@ -191,10 +189,6 @@ public final boolean noProxy() {
191189
return (hasProxy() && this.proxyServer.equals("No")) || this.proxyType == Proxy.Type.DIRECT;
192190
}
193191

194-
public final int maxRetries() {
195-
return this.maxRetries;
196-
}
197-
198192
private final String url;
199193
private final long requestId;
200194
private final int connectionTimeout;
@@ -205,7 +199,6 @@ public final int maxRetries() {
205199
private final String proxyServer;
206200
private final int proxyPort;
207201
private final Proxy.Type proxyType;
208-
private final int maxRetries;
209202
}
210203

211204
/**
@@ -232,7 +225,6 @@ protected Void doInBackground(Request... requests) {
232225
boolean downloadContentSizePresent = false;
233226

234227
try {
235-
int retryCount = 0;
236228
boolean isDone = false;
237229
final URL url = new URL(request.url());
238230

@@ -242,13 +234,6 @@ protected Void doInBackground(Request... requests) {
242234
return null;
243235
}
244236

245-
// TODO: replace with more elegant and controlled approach
246-
if (retryCount > 0) {
247-
Thread.sleep(100);
248-
249-
cleanup(httpConn);
250-
}
251-
252237
URLConnection conn;
253238
if (request.hasProxy()) {
254239
if (request.noProxy()) {
@@ -352,21 +337,8 @@ protected Void doInBackground(Request... requests) {
352337
try {
353338
status = httpConn.getResponseCode();
354339
error = httpConn.getResponseMessage();
355-
356-
if ((status > 0)
357-
&& ((status < HttpURLConnection.HTTP_OK)
358-
|| (status >= HttpURLConnection.HTTP_SERVER_ERROR))) {
359-
if (retryCount++ < request.maxRetries()) {
360-
continue;
361-
}
362-
}
363340
} catch (SocketTimeoutException | UnknownHostException e) {
364-
if (retryCount++ < request.maxRetries()) {
365-
httpClient.resetRequest(request.requestId());
366-
continue;
367-
} else {
368-
throw e;
369-
}
341+
throw e;
370342
}
371343
}
372344

@@ -458,17 +430,12 @@ protected Void doInBackground(Request... requests) {
458430
throw e;
459431
}
460432
} catch (SocketTimeoutException e) {
461-
if (retryCount++ < request.maxRetries()) {
462-
httpClient.resetRequest(request.requestId());
463-
continue;
464-
} else {
465-
throw e;
466-
}
433+
throw e;
467434
}
468435

469436
checkCancelled();
470437

471-
// The request is completed, not cancelled or retried
438+
// The request is completed and not cancelled
472439
// Notifies the native (C++) side that request was completed
473440
isDone = true;
474441
httpClient.completeRequest(request.requestId(), status, uploadedContentSize, downloadContentSize, error, contentType);
@@ -616,8 +583,7 @@ public HttpTask send(
616583
byte[] postData,
617584
String proxyServer,
618585
int proxyPort,
619-
int proxyType,
620-
int maxRetries) {
586+
int proxyType) {
621587
final Request request =
622588
new Request(
623589
url,
@@ -629,8 +595,7 @@ public HttpTask send(
629595
postData,
630596
proxyServer,
631597
proxyPort,
632-
proxyType,
633-
maxRetries);
598+
proxyType);
634599
final HttpTask task = new HttpTask(this);
635600
task.executeOnExecutor(executor, request);
636601
return task;
@@ -647,6 +612,4 @@ private synchronized native void completeRequest(
647612
private synchronized native void dateAndOffsetCallback(long requestId, long date, long offset);
648613
// Callback set date and offset
649614
private synchronized native void headersCallback(long requestId, String[] headers);
650-
// Reset request for retry
651-
private synchronized native void resetRequest(long requestId);
652-
}
615+
}

olp-cpp-sdk-core/src/http/android/NetworkAndroid.cpp

Lines changed: 3 additions & 38 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright (C) 2019-2021 HERE Europe B.V.
2+
* Copyright (C) 2019-2025 HERE Europe B.V.
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -148,22 +148,6 @@ Java_com_here_olp_network_HttpClient_completeRequest(
148148
downloaded_bytes, error, content_type);
149149
}
150150

151-
/*
152-
* Reset request upon retry
153-
*/
154-
extern "C" OLP_SDK_NETWORK_ANDROID_EXPORT void JNICALL
155-
Java_com_here_olp_network_HttpClient_resetRequest(JNIEnv* env, jobject obj,
156-
jlong request_id) {
157-
auto network = olp::http::GetNetworkAndroidNativePtr(env, obj);
158-
if (!network) {
159-
OLP_SDK_LOG_WARNING(
160-
kLogTag,
161-
"ResetRequest failed - network is invalid, request_id=" << request_id);
162-
return;
163-
}
164-
network->ResetRequest(env, request_id);
165-
}
166-
167151
NetworkAndroid::NetworkAndroid(size_t max_requests_count)
168152
: java_self_class_(nullptr),
169153
jni_send_method_(nullptr),
@@ -396,10 +380,7 @@ bool NetworkAndroid::Initialize() {
396380
&Java_com_here_olp_network_HttpClient_dataCallback)},
397381
{"completeRequest", "(JIIILjava/lang/String;Ljava/lang/String;)V",
398382
reinterpret_cast<void*>(
399-
&Java_com_here_olp_network_HttpClient_completeRequest)},
400-
{"resetRequest", "(J)V",
401-
reinterpret_cast<void*>(
402-
&Java_com_here_olp_network_HttpClient_resetRequest)}};
383+
&Java_com_here_olp_network_HttpClient_completeRequest)}};
403384

404385
env->RegisterNatives(java_self_class_, methods,
405386
sizeof(methods) / sizeof(methods[0]));
@@ -715,21 +696,6 @@ void NetworkAndroid::CompleteRequest(JNIEnv* env, RequestId request_id,
715696
run_thread_ready_cv_.notify_all();
716697
}
717698

718-
void NetworkAndroid::ResetRequest(JNIEnv* env, RequestId request_id) {
719-
std::lock_guard<std::mutex> lock(requests_mutex_);
720-
if (!started_) {
721-
return;
722-
}
723-
724-
auto req = requests_.find(request_id);
725-
if (req == requests_.end()) {
726-
OLP_SDK_LOG_WARNING(kLogTag,
727-
"ResetRequest of unknown request_id=" << request_id);
728-
return;
729-
}
730-
req->second->Reinitialize();
731-
}
732-
733699
jobjectArray NetworkAndroid::CreateExtraHeaders(
734700
JNIEnv* env,
735701
const std::vector<std::pair<std::string, std::string> >& extra_headers) {
@@ -970,7 +936,6 @@ SendOutcome NetworkAndroid::Send(NetworkRequest request,
970936
.count());
971937
const jint jproxy_port = static_cast<jint>(proxy_settings.GetPort());
972938
const jint jproxy_type = static_cast<jint>(proxy_settings.GetType());
973-
const jint jmax_retries = 1;
974939
// Do sending
975940
{
976941
std::lock_guard<std::mutex> lock(requests_mutex_);
@@ -989,7 +954,7 @@ SendOutcome NetworkAndroid::Send(NetworkRequest request,
989954
auto task_obj = env.GetEnv()->CallObjectMethod(
990955
obj_, jni_send_method_, jurl, jhttp_verb, jrequest_id,
991956
jconnection_timeout, jtransfer_timeout, jheaders, jbody, jproxy,
992-
jproxy_port, jproxy_type, jmax_retries);
957+
jproxy_port, jproxy_type);
993958
if (env.GetEnv()->ExceptionOccurred() || !task_obj) {
994959
OLP_SDK_LOG_ERROR(
995960
kLogTag, "Send failed - HttpClient error, url=" << request.GetUrl());

olp-cpp-sdk-core/src/http/android/NetworkAndroid.h

Lines changed: 0 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -104,12 +104,6 @@ class NetworkAndroid : public Network {
104104
int uploaded_bytes, int downloaded_bytes, jstring error,
105105
jstring content_type);
106106

107-
/**
108-
* @brief Reset the given message if retry attempt invoked
109-
* @param id - Unique Id of the message
110-
*/
111-
void ResetRequest(JNIEnv* env, RequestId request_id);
112-
113107
/**
114108
* @brief Setup JavaVM pointer
115109
* This must be done before calling initialize

0 commit comments

Comments
 (0)