Skip to content

Commit c114980

Browse files
author
Rodrigo Gomez Palacio
committed
Add support for Retry-Limit in HttpClient, returned in HttpResponse
Motivation: retry limit logic that is handled earlier in the code path needs to have the limit passed up to it
1 parent 3bd4762 commit c114980

File tree

2 files changed

+22
-3
lines changed

2 files changed

+22
-3
lines changed

OneSignalSDK/onesignal/core/src/main/java/com/onesignal/core/internal/http/HttpResponse.kt

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,11 @@ class HttpResponse(
2323
* The module handing this should delay any future requests by this time.
2424
*/
2525
val retryAfterSeconds: Int? = null,
26+
/**
27+
* Optional Integer value may be returned from the backend.
28+
* The module handling this should not retry more than this number.
29+
*/
30+
val retryLimit: Int? = null,
2631
) {
2732
/**
2833
* Whether the response is a successful one.

OneSignalSDK/onesignal/core/src/main/java/com/onesignal/core/internal/http/impl/HttpClient.kt

Lines changed: 17 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -188,6 +188,7 @@ internal class HttpClient(
188188
httpResponse = con.responseCode
189189

190190
val retryAfter = retryAfterFromResponse(con)
191+
val retryLimit = retryLimitFromResponse(con)
191192
val newDelayUntil = _time.currentTimeMillis + (retryAfter ?: 0) * 1_000
192193
if (newDelayUntil > delayNewRequestsUntil) delayNewRequestsUntil = newDelayUntil
193194

@@ -204,7 +205,7 @@ internal class HttpClient(
204205
)
205206

206207
// TODO: SHOULD RETURN OK INSTEAD OF NOT_MODIFIED TO MAKE TRANSPARENT?
207-
retVal = HttpResponse(httpResponse, cachedResponse, retryAfterSeconds = retryAfter)
208+
retVal = HttpResponse(httpResponse, cachedResponse, retryAfterSeconds = retryAfter, retryLimit = retryLimit)
208209
}
209210
HttpURLConnection.HTTP_ACCEPTED, HttpURLConnection.HTTP_CREATED, HttpURLConnection.HTTP_OK -> {
210211
val inputStream = con.inputStream
@@ -233,7 +234,7 @@ internal class HttpClient(
233234
}
234235
}
235236

236-
retVal = HttpResponse(httpResponse, json, retryAfterSeconds = retryAfter)
237+
retVal = HttpResponse(httpResponse, json, retryAfterSeconds = retryAfter, retryLimit = retryLimit)
237238
}
238239
else -> {
239240
Logging.debug("HttpClient: Got Response = ${method ?: "GET"} ${con.url} - FAILED STATUS: $httpResponse")
@@ -254,7 +255,7 @@ internal class HttpClient(
254255
Logging.warn("HttpClient: Got Response = $method - STATUS: $httpResponse - No response body!")
255256
}
256257

257-
retVal = HttpResponse(httpResponse, jsonResponse, retryAfterSeconds = retryAfter)
258+
retVal = HttpResponse(httpResponse, jsonResponse, retryAfterSeconds = retryAfter, retryLimit = retryLimit)
258259
}
259260
}
260261
} catch (t: Throwable) {
@@ -294,6 +295,19 @@ internal class HttpClient(
294295
}
295296
}
296297

298+
/**
299+
* Reads the HTTP Retry-Limit from the response.
300+
*/
301+
private fun retryLimitFromResponse(con: HttpURLConnection): Int? {
302+
val retryLimitStr = con.getHeaderField("Retry-Limit")
303+
return if (retryLimitStr != null) {
304+
Logging.debug("HttpClient: Response Retry-After: $retryLimitStr")
305+
retryLimitStr.toIntOrNull()
306+
} else {
307+
null
308+
}
309+
}
310+
297311
private fun logHTTPSent(
298312
method: String?,
299313
url: URL,

0 commit comments

Comments
 (0)