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 24, 2024
1 parent 80ce0ef commit 504a703
Show file tree
Hide file tree
Showing 3 changed files with 32 additions and 34 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import android.app.AlertDialog
import com.onesignal.common.AndroidUtils
import com.onesignal.common.IDManager
import com.onesignal.common.JSONUtils
import com.onesignal.common.consistency.IamFetchReadyCondition
import com.onesignal.common.consistency.models.IConsistencyManager
import com.onesignal.common.events.EventProducer
import com.onesignal.common.exceptions.BackendException
Expand Down Expand Up @@ -240,7 +241,7 @@ internal class InAppMessagesManager(
}

// called when a new push subscription is added, or the app id is updated, or a new session starts
private suspend fun fetchMessages(rywToken: String) {
private suspend fun fetchMessages(rywToken: String?) {
// We only want to fetch IAMs if we know the app is in the
// foreground, as we don't want to do this for background
// events (such as push received), wasting resources for
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ internal interface IInAppBackendService {
suspend fun listInAppMessages(
appId: String,
subscriptionId: String,
rywToken: String,
rywToken: String?,
sessionDurationProvider: () -> Long,
): List<InAppMessage>?

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ internal class InAppBackendService(
override suspend fun listInAppMessages(
appId: String,
subscriptionId: String,
rywToken: String,
rywToken: String?,
sessionDurationProvider: () -> Long,
): List<InAppMessage>? {
val baseUrl = "apps/$appId/subscriptions/$subscriptionId/iams"
Expand Down Expand Up @@ -202,44 +202,41 @@ internal class InAppBackendService(

private suspend fun attemptFetchWithRetries(
baseUrl: String,
rywToken: String,
rywToken: String?,
sessionDurationProvider: () -> Long,
): List<InAppMessage>? {
var attempts = 1
var retryLimit: Int? = null // Retry limit will be determined dynamically

// We use RYW_TOKEN_NOT_PROVIDED to designate the backend not returning a RYW token
if (rywToken != ConsistencyManager.RYW_TOKEN_NOT_PROVIDED) {
while (retryLimit == null || attempts <= retryLimit + 1) {
val retryCount = if (attempts > 1) attempts - 1 else null
val values =
OptionalHeaders(
rywToken = rywToken,
sessionDuration = sessionDurationProvider(),
retryCount = retryCount,
)
val response = _httpClient.get(baseUrl, values)

if (response.isSuccess) {
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
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)
}
} else if (response.statusCode in 500..599) {
return null
} else {
return null
while (retryLimit == null || attempts <= retryLimit + 1) {
val retryCount = if (attempts > 1) attempts - 1 else null
val values =
OptionalHeaders(
rywToken = rywToken,
sessionDuration = sessionDurationProvider(),
retryCount = retryCount,
)
val response = _httpClient.get(baseUrl, values)

if (response.isSuccess) {
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
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)
}

attempts++
} else if (response.statusCode in 500..599) {
return null
} else {
return null
}

attempts++
}

// Final attempt without the RYW token if retries fail
Expand Down

0 comments on commit 504a703

Please sign in to comment.