From b3d7d7421cb9429b45bba6dc529929c4a6e54b42 Mon Sep 17 00:00:00 2001 From: Satheesh Kannan Date: Tue, 30 Jul 2024 13:05:40 +0530 Subject: [PATCH] feat: implement exponential retry mechanism for handling network errors --- Sources/Classes/Headers/Public/RSExponentialBackOff.h | 2 +- Sources/Classes/RSCloudModeManager.m | 5 +++-- Sources/Classes/RSExponentialBackOff.m | 8 ++++---- Sources/Classes/RSUtils.m | 3 +-- 4 files changed, 9 insertions(+), 9 deletions(-) diff --git a/Sources/Classes/Headers/Public/RSExponentialBackOff.h b/Sources/Classes/Headers/Public/RSExponentialBackOff.h index 61062115..f34daf73 100644 --- a/Sources/Classes/Headers/Public/RSExponentialBackOff.h +++ b/Sources/Classes/Headers/Public/RSExponentialBackOff.h @@ -28,7 +28,7 @@ NS_ASSUME_NONNULL_BEGIN * * @return Next delay value in seconds */ -- (NSInteger)nextDelay; +- (int)nextDelay; /** * Function will resets the attempts. diff --git a/Sources/Classes/RSCloudModeManager.m b/Sources/Classes/RSCloudModeManager.m index a9e0471c..5da10e6d 100644 --- a/Sources/Classes/RSCloudModeManager.m +++ b/Sources/Classes/RSCloudModeManager.m @@ -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) diff --git a/Sources/Classes/RSExponentialBackOff.m b/Sources/Classes/RSExponentialBackOff.m index 80bc569e..fae72b3c 100644 --- a/Sources/Classes/RSExponentialBackOff.m +++ b/Sources/Classes/RSExponentialBackOff.m @@ -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) { diff --git a/Sources/Classes/RSUtils.m b/Sources/Classes/RSUtils.m index aec4c4cb..f05ae20b 100644 --- a/Sources/Classes/RSUtils.m +++ b/Sources/Classes/RSUtils.m @@ -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; }