Skip to content

Commit

Permalink
fixup! OSMessagingController Retry Logic
Browse files Browse the repository at this point in the history
  • Loading branch information
Rodrigo Gomez Palacio committed Oct 10, 2024
1 parent eb06720 commit d546445
Showing 1 changed file with 26 additions and 22 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -247,34 +247,37 @@ - (void)updateInAppMessagesFromCache {
}

- (void)getInAppMessagesFromServer:(NSString *)subscriptionId {
[OneSignalLog onesignalLog:ONE_S_LL_VERBOSE message:@"getInAppMessagesFromServer"];
dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{
[OneSignalLog onesignalLog:ONE_S_LL_VERBOSE message:@"getInAppMessagesFromServer"];

if (!subscriptionId) {
[self updateInAppMessagesFromCache];
return;
}
if (!subscriptionId) {
[self updateInAppMessagesFromCache];
return;
}

OSConsistencyManager *consistencyManager = [OSConsistencyManager shared];
NSString *onesignalId = OneSignalUserManagerImpl.sharedInstance.onesignalId;
OSConsistencyManager *consistencyManager = [OSConsistencyManager shared];
NSString *onesignalId = OneSignalUserManagerImpl.sharedInstance.onesignalId;

if (!onesignalId) {
[OneSignalLog onesignalLog:ONE_S_LL_VERBOSE message:@"Failed to get in app messages due to no OneSignal ID"];
return;
}
if (!onesignalId) {
[OneSignalLog onesignalLog:ONE_S_LL_VERBOSE message:@"Failed to get in app messages due to no OneSignal ID"];
return;
}

OSIamFetchReadyCondition *condition = [OSIamFetchReadyCondition sharedInstanceWithId:onesignalId];
NSString *rywToken = [consistencyManager registerCondition:condition forId:onesignalId];
OSIamFetchReadyCondition *condition = [OSIamFetchReadyCondition sharedInstanceWithId:onesignalId];
NSString *rywToken = [consistencyManager registerCondition:condition forId:onesignalId];

NSNumber *sessionDuration = @([OSSessionManager getTimeFocusedElapsed]);
NSNumber *sessionDuration = @([OSSessionManager getTimeFocusedElapsed]);

// Initial request
[self attemptFetchWithRetries:subscriptionId
rywToken:rywToken
sessionDuration:sessionDuration
attempts:@0 // Starting with 0 attempts
retryLimit:nil]; // Retry limit to be set dynamically on first failure
// Initial request
[self attemptFetchWithRetries:subscriptionId
rywToken:rywToken
sessionDuration:sessionDuration
attempts:@0 // Starting with 0 attempts
retryLimit:nil]; // Retry limit to be set dynamically on first failure
});
}


- (void)attemptFetchWithRetries:(NSString *)subscriptionId
rywToken:(NSString *)rywToken
sessionDuration:(NSNumber *)sessionDuration
Expand Down Expand Up @@ -315,6 +318,7 @@ - (void)attemptFetchWithRetries:(NSString *)subscriptionId
onFailure:^(NSError *error) {
NSDictionary *errorInfo = error.userInfo[@"returned"];
NSNumber *statusCode = errorInfo[@"httpStatusCode"];
NSDictionary* responseHeaders = errorInfo[@"headers"];

if (!statusCode) {
[self updateInAppMessagesFromCache];
Expand All @@ -325,11 +329,11 @@ - (void)attemptFetchWithRetries:(NSString *)subscriptionId

NSInteger code = [statusCode integerValue];
if (code == 425 || code == 429) { // 425 Too Early or 429 Too Many Requests
NSInteger retryAfter = [errorInfo[@"Retry-After"] integerValue] ?: DEFAULT_RETRY_AFTER_SECONDS;
NSInteger retryAfter = [responseHeaders[@"Retry-After"] integerValue] ?: DEFAULT_RETRY_AFTER_SECONDS;

// Dynamically set the retry limit from the header, if not already set
if (!blockRetryLimit) {
blockRetryLimit = @([errorInfo[@"OneSignal-Retry-Limit"] integerValue] ?: DEFAULT_RETRY_LIMIT);
blockRetryLimit = @([responseHeaders[@"OneSignal-Retry-Limit"] integerValue] ?: DEFAULT_RETRY_LIMIT);
}

if ([attempts integerValue] < [blockRetryLimit integerValue]) {
Expand Down

0 comments on commit d546445

Please sign in to comment.