Skip to content

Commit 88b3e5d

Browse files
author
Alexis Oyama
committed
perf(request): Send another request if the last request had maximum events per api call
1 parent 23e6608 commit 88b3e5d

File tree

3 files changed

+23
-14
lines changed

3 files changed

+23
-14
lines changed

Leanplum-SDK/Classes/Constants.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -55,7 +55,7 @@
5555
#define HEARTBEAT_INTERVAL 15 * 60 // 15 minutes
5656

5757
#define MAX_STORED_OCCURRENCES_PER_MESSAGE 100
58-
#define MAX_ACTIONS_PER_API_CALL 10000
58+
#define MAX_EVENTS_PER_API_CALL 10000
5959

6060
#define DEFAULT_PRIORITY 1000
6161

Leanplum-SDK/Classes/LPRequestStorage.m

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -97,8 +97,8 @@ - (void)migrateRequests
9797
*/
9898
- (void)saveRequests:(NSMutableArray *)requests
9999
{
100-
if (requests.count > MAX_ACTIONS_PER_API_CALL) {
101-
NSRange range = NSMakeRange(0, requests.count - MAX_ACTIONS_PER_API_CALL);
100+
if (requests.count > MAX_EVENTS_PER_API_CALL) {
101+
NSRange range = NSMakeRange(0, requests.count - MAX_EVENTS_PER_API_CALL);
102102
[requests removeObjectsInRange:range];
103103
}
104104

Leanplum-SDK/Classes/LeanplumRequest.m

Lines changed: 20 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -301,18 +301,22 @@ - (void)sendNow:(BOOL)async
301301
}
302302

303303
[self sendEventually];
304+
[self sendRequests:async];
305+
}
304306

307+
- (void)sendRequests:(BOOL)async
308+
{
305309
void (^operationBlock)() = ^void() {
306310
[LeanplumRequest generateUUID];
307311

308312
// Simulate pop all requests.
309-
NSArray *requestsToSend = [LPEventDataManager eventsWithLimit:MAX_ACTIONS_PER_API_CALL];
313+
NSArray *requestsToSend = [LPEventDataManager eventsWithLimit:MAX_EVENTS_PER_API_CALL];
310314
lastSentTime = [NSDate timeIntervalSinceReferenceDate];
311315

312316
if (requestsToSend.count == 0) {
313317
return;
314318
}
315-
319+
316320
NSString *requestData = [LPJSON stringFromJSON:@{LP_PARAM_DATA:requestsToSend}];
317321
LPConstantsState *constants = [LPConstantsState sharedState];
318322
NSMutableDictionary *multiRequestArgs = [NSMutableDictionary dictionaryWithObjectsAndKeys:
@@ -324,12 +328,12 @@ - (void)sendNow:(BOOL)async
324328
[self attachApiKeys:multiRequestArgs];
325329
int timeout = async ? constants.networkTimeoutSeconds : constants.syncNetworkTimeoutSeconds;
326330
id<LPNetworkOperationProtocol> op = [engine operationWithPath:constants.apiServlet
327-
params:multiRequestArgs
328-
httpMethod:_httpMethod
329-
ssl:constants.apiSSL
330-
timeoutSeconds:timeout];
331+
params:multiRequestArgs
332+
httpMethod:_httpMethod
333+
ssl:constants.apiSSL
334+
timeoutSeconds:timeout];
331335
__block BOOL finished = NO;
332-
336+
333337
// Schedule timeout.
334338
[LPTimerBlocks scheduledTimerWithTimeInterval:timeout block:^() {
335339
if (finished) {
@@ -345,14 +349,14 @@ - (void)sendNow:(BOOL)async
345349
}
346350
LP_END_TRY
347351
} repeats:NO];
348-
352+
349353
[op addCompletionHandler:^(id<LPNetworkOperationProtocol> operation, id json) {
350354
if (finished) {
351355
return;
352356
}
353357
finished = YES;
354358
LP_TRY
355-
359+
356360
// Handle errors that don't return an HTTP error code.
357361
NSUInteger numResponses = [LPResponse numResponsesInDictionary:json];
358362
for (NSUInteger i = 0; i < numResponses; i++) {
@@ -377,6 +381,11 @@ - (void)sendNow:(BOOL)async
377381

378382
// Delete events on success.
379383
[LPEventDataManager deleteEventsWithLimit:requestsToSend.count];
384+
385+
// Send another request if the last request had maximum events per api call.
386+
if (requestsToSend.count == MAX_EVENTS_PER_API_CALL) {
387+
[self sendRequests:async];
388+
}
380389
LP_END_TRY
381390
if (_response != nil) {
382391
_response(operation, json);
@@ -423,7 +432,7 @@ - (void)sendNow:(BOOL)async
423432
}
424433
LP_END_TRY
425434
}];
426-
435+
427436
// Execute synchronously. Don't block for more than 'timeout' seconds.
428437
dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{
429438
[engine runSynchronously:op];
@@ -459,7 +468,7 @@ - (void)sendEventually
459468
NSUserDefaults *userDefaults = [NSUserDefaults standardUserDefaults];
460469
NSString *uuid = [userDefaults objectForKey:LEANPLUM_DEFAULTS_UUID_KEY];
461470
NSInteger count = [LPEventDataManager count];
462-
if (!uuid || count % MAX_ACTIONS_PER_API_CALL == 0) {
471+
if (!uuid || count % MAX_EVENTS_PER_API_CALL == 0) {
463472
uuid = [LeanplumRequest generateUUID];
464473
}
465474

0 commit comments

Comments
 (0)