Skip to content

Commit

Permalink
fixup! Update IAM manager & backend service with retry logic, optiona…
Browse files Browse the repository at this point in the history
…l headers
  • Loading branch information
Rodrigo Gomez Palacio committed Sep 27, 2024
1 parent 2083bd0 commit 7707564
Showing 1 changed file with 9 additions and 10 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -204,11 +204,11 @@ internal class InAppBackendService(
rywToken: String?,
sessionDurationProvider: () -> Long,
): List<InAppMessage>? {
var attempts = 1
var retryLimit: Int? = null // Retry limit will be determined dynamically
var attempts = 0
var retryLimit: Int? = null // retry limit is remote defined & set dynamically below

while (retryLimit == null || attempts <= retryLimit + 1) {
val retryCount = if (attempts > 1) attempts - 1 else null
do {
val retryCount = if (attempts > 0) attempts else null
val values =
OptionalHeaders(
rywToken = rywToken,
Expand All @@ -221,13 +221,12 @@ internal class InAppBackendService(
val jsonResponse = response.payload?.let { JSONObject(it) }
return jsonResponse?.let { hydrateInAppMessages(it) }
} else if (response.statusCode == 425 || response.statusCode == 429) {
// Dynamically update the retry limit from response
// update the retry limit from response
retryLimit = response.retryLimit ?: retryLimit

// Apply the Retry-After delay if present, otherwise proceed without delay
val retryAfter = response.retryAfterSeconds
if (retryAfter != null) {
delay(retryAfter * 1_000L)
// apply the Retry-After delay if present
response.retryAfterSeconds?.let {
delay(it * 1_000L)
}
} else if (response.statusCode in 500..599) {
return null
Expand All @@ -236,7 +235,7 @@ internal class InAppBackendService(
}

attempts++
}
} while (attempts <= (retryLimit ?: 0))

// Final attempt without the RYW token if retries fail
return fetchInAppMessagesWithoutRywToken(baseUrl, sessionDurationProvider)
Expand Down

0 comments on commit 7707564

Please sign in to comment.