Skip to content

Commit

Permalink
feat: implement exponential retry mechanism for handling network errors
Browse files Browse the repository at this point in the history
  • Loading branch information
SKannaniOS committed Jul 30, 2024
1 parent dfa32ee commit b3d7d74
Show file tree
Hide file tree
Showing 4 changed files with 9 additions and 9 deletions.
2 changes: 1 addition & 1 deletion Sources/Classes/Headers/Public/RSExponentialBackOff.h
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ NS_ASSUME_NONNULL_BEGIN
*
* @return Next delay value in seconds
*/
- (NSInteger)nextDelay;
- (int)nextDelay;

/**
* Function will resets the attempts.
Expand Down
5 changes: 3 additions & 2 deletions Sources/Classes/RSCloudModeManager.m
Original file line number Diff line number Diff line change
Expand Up @@ -74,8 +74,9 @@ - (void) startCloudModeProcessor {
[RSMetricsReporter report:SDKMETRICS_CM_ATTEMPT_ABORT forMetricType:COUNT withProperties:@{SDKMETRICS_TYPE: SDKMETRICS_DATA_PLANE_URL_INVALID} andValue:1];
break;
} else if (response.state == NETWORK_ERROR) {
int delay = (int)[self->backOff nextDelay];
[RSLogger logDebug:[[NSString alloc] initWithFormat:@"RSCloudModeManager: CloudModeProcessor: Retrying in: %@", [self delayToString:delay]]];
int delay = [self->backOff nextDelay];
NSString *timeString = [RSUtils delayToString:delay];
[RSLogger logDebug:[[NSString alloc] initWithFormat:@"RSCloudModeManager: CloudModeProcessor: Retrying in: %@", timeString]];
[RSMetricsReporter report:SDKMETRICS_CM_ATTEMPT_RETRY forMetricType:COUNT withProperties:nil andValue:1];
sleep(delay);
} else { // To handle the status code RESOURCE_NOT_FOUND(404) & BAD_REQUEST(400)
Expand Down
8 changes: 4 additions & 4 deletions Sources/Classes/RSExponentialBackOff.m
Original file line number Diff line number Diff line change
Expand Up @@ -34,11 +34,11 @@ - (instancetype)initWithMaximumDelay:(int) seconds {
/**
* Function will calculate the next delay value in seconds
*/
- (NSInteger)nextDelay {
NSInteger delay = (NSInteger)pow(2, _attempt++);
NSInteger jitter = arc4random_uniform((uint32_t)(delay + 1));
- (int)nextDelay {
int delay = pow(2, _attempt++);
int jitter = arc4random_uniform((delay + 1));

NSInteger exponentialDelay = _initialDelay + delay + jitter;
int exponentialDelay = _initialDelay + delay + jitter;
exponentialDelay = MIN(exponentialDelay, _maximumDelay);

if (exponentialDelay >= _maximumDelay) {
Expand Down
3 changes: 1 addition & 2 deletions Sources/Classes/RSUtils.m
Original file line number Diff line number Diff line change
Expand Up @@ -294,8 +294,7 @@ +(NSArray*) extractParamFromURL: (NSURL*) deepLinkURL{
+ (NSString *)delayToString:(int) delay {
int min = delay / 60;
int sec = min > 0 ? (delay - (min * 60)) : delay;
NSString *finalString = min > 0 ? [NSString stringWithFormat:@"%d min, %d sec", min, sec] : [NSString stringWithFormat:@"%d sec", sec];
NSLog(@"SK--->>%@", finalString);
NSString *finalString = min > 0 ? [NSString stringWithFormat:@"%dm, %ds", min, sec] : [NSString stringWithFormat:@"%ds", sec];
return finalString;
}

Expand Down

0 comments on commit b3d7d74

Please sign in to comment.