Skip to content

Commit a106beb

Browse files
committed
Retry requests with a backoff strategy & keep max retries
* Keep max retries for now so we don't spam the server with requests when it is in trouble to avoid retrying forever. * Let a request retry 4 times (so total 5 times) with backoff strategy calculated with multiples of 3. * Remove some no longer used tag delay variables from common defines
1 parent 5a331d3 commit a106beb

File tree

2 files changed

+13
-13
lines changed

2 files changed

+13
-13
lines changed

iOS_SDK/OneSignalSDK/OneSignalCore/Source/API/OneSignalClient.m

Lines changed: 11 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -59,8 +59,8 @@ -(instancetype)init {
5959

6060
- (NSURLSessionConfiguration *)configurationWithCachingPolicy:(NSURLRequestCachePolicy)policy {
6161
NSURLSessionConfiguration *configuration = [NSURLSessionConfiguration defaultSessionConfiguration];
62-
configuration.timeoutIntervalForRequest = REQUEST_TIMEOUT_REQUEST;
63-
configuration.timeoutIntervalForResource = REQUEST_TIMEOUT_RESOURCE;
62+
configuration.timeoutIntervalForRequest = REQUEST_TIMEOUT_REQUEST; // TODO: Are these anything?
63+
configuration.timeoutIntervalForResource = REQUEST_TIMEOUT_RESOURCE; // TODO: Are these anything?
6464

6565
//prevent caching of requests, this mainly impacts OSRequestGetIosParams,
6666
//since the OSRequestGetTags endpoint has a caching header policy
@@ -333,10 +333,11 @@ - (BOOL)willReattemptRequest:(int)statusCode withRequest:(OneSignalRequest *)req
333333
OSReattemptRequest *reattempt = [OSReattemptRequest withRequest:request successBlock:successBlock failureBlock:failureBlock];
334334

335335
if (async) {
336-
//retry again in 15 seconds
337-
[OneSignalLog onesignalLog:ONE_S_LL_DEBUG message:[NSString stringWithFormat:@"Re-scheduling request (%@) to be re-attempted in %.3f seconds due to failed HTTP request with status code %i", NSStringFromClass([request class]), REATTEMPT_DELAY, (int)statusCode]];
336+
//retry again in an increasing interval calculated with reattemptDelay
337+
double reattemptDelay = [self calculateReattemptDelay:request.reattemptCount];
338+
[OneSignalLog onesignalLog:ONE_S_LL_DEBUG message:[NSString stringWithFormat:@"Re-scheduling request (%@) to be re-attempted in %.3f seconds due to failed HTTP request with status code %i", NSStringFromClass([request class]), reattemptDelay, (int)statusCode]];
338339
[OneSignalCoreHelper dispatch_async_on_main_queue:^{
339-
[self performSelector:@selector(reattemptRequest:) withObject:reattempt afterDelay:REATTEMPT_DELAY];
340+
[self performSelector:@selector(reattemptRequest:) withObject:reattempt afterDelay:reattemptDelay];
340341
}];
341342
} else {
342343
//retry again immediately
@@ -349,6 +350,11 @@ - (BOOL)willReattemptRequest:(int)statusCode withRequest:(OneSignalRequest *)req
349350
return false;
350351
}
351352

353+
// A request will retry with intervals of 5, 15 , 45, 135 seconds...
354+
- (double)calculateReattemptDelay:(int)reattemptCount {
355+
return REATTEMPT_DELAY * pow(3, reattemptCount);
356+
}
357+
352358
- (void)prettyPrintDebugStatementWithRequest:(OneSignalRequest *)request {
353359
if (![NSJSONSerialization isValidJSONObject:request.parameters])
354360
return;

iOS_SDK/OneSignalSDK/OneSignalCore/Source/OneSignalCommonDefines.h

Lines changed: 2 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -231,13 +231,10 @@ typedef enum {GET, POST, HEAD, PUT, DELETE, OPTIONS, CONNECT, TRACE, PATCH} HTTP
231231

232232
#ifndef OS_TEST
233233
// OneSignal API Client Defines
234-
#define REATTEMPT_DELAY 30.0
234+
#define REATTEMPT_DELAY 5.0
235235
#define REQUEST_TIMEOUT_REQUEST 120.0 //for most HTTP requests
236236
#define REQUEST_TIMEOUT_RESOURCE 120.0 //for loading a resource like an image
237-
#define MAX_ATTEMPT_COUNT 3
238-
239-
// Send tags batch delay
240-
#define SEND_TAGS_DELAY 5.0
237+
#define MAX_ATTEMPT_COUNT 5
241238

242239
// the max number of UNNotificationCategory ID's the SDK will register
243240
#define MAX_CATEGORIES_SIZE 128
@@ -254,9 +251,6 @@ typedef enum {GET, POST, HEAD, PUT, DELETE, OPTIONS, CONNECT, TRACE, PATCH} HTTP
254251
#define REQUEST_TIMEOUT_RESOURCE 0.02 //for loading a resource like an image
255252
#define MAX_ATTEMPT_COUNT 3
256253

257-
// Send tags batch delay
258-
#define SEND_TAGS_DELAY 0.005
259-
260254
// the max number of UNNotificationCategory ID's the SDK will register
261255
#define MAX_CATEGORIES_SIZE 5
262256

0 commit comments

Comments
 (0)