Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Implement maximum batch request size #874

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion Analytics/Classes/Internal/SEGHTTPClient.h
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ NS_ASSUME_NONNULL_BEGIN
* NOTE: You need to re-dispatch within the completionHandler onto a desired queue to avoid threading issues.
* Completion handlers are called on a dispatch queue internal to SEGHTTPClient.
*/
- (NSURLSessionUploadTask *)upload:(JSON_DICT)batch forWriteKey:(NSString *)writeKey completionHandler:(void (^)(BOOL retry))completionHandler;
- (nullable NSURLSessionUploadTask *)upload:(JSON_DICT)batch forWriteKey:(NSString *)writeKey completionHandler:(void (^)(BOOL retry))completionHandler;

- (NSURLSessionDataTask *)settingsForWriteKey:(NSString *)writeKey completionHandler:(void (^)(BOOL success, JSON_DICT _Nullable settings))completionHandler;

Expand Down
8 changes: 7 additions & 1 deletion Analytics/Classes/Internal/SEGHTTPClient.m
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
#import "NSData+SEGGZIP.h"
#import "SEGAnalyticsUtils.h"

static const NSUInteger kMaxBatchSize = 475000; // 475KB

@implementation SEGHTTPClient

Expand Down Expand Up @@ -66,7 +67,7 @@ - (void)dealloc
}


- (NSURLSessionUploadTask *)upload:(NSDictionary *)batch forWriteKey:(NSString *)writeKey completionHandler:(void (^)(BOOL retry))completionHandler
- (nullable NSURLSessionUploadTask *)upload:(NSDictionary *)batch forWriteKey:(NSString *)writeKey completionHandler:(void (^)(BOOL retry))completionHandler
{
// batch = SEGCoerceDictionary(batch);
NSURLSession *session = [self sessionForWriteKey:writeKey];
Expand All @@ -93,6 +94,11 @@ - (NSURLSessionUploadTask *)upload:(NSDictionary *)batch forWriteKey:(NSString *
completionHandler(NO); // Don't retry this batch.
return nil;
}
if (payload.length >= kMaxBatchSize) {
SEGLog(@"Payload exceeded the limit of %luKB per batch", kMaxBatchSize / 1000);
completionHandler(NO);
return nil;
}
NSData *gzippedPayload = [payload seg_gzippedData];

NSURLSessionUploadTask *task = [session uploadTaskWithRequest:request fromData:gzippedPayload completionHandler:^(NSData *_Nullable data, NSURLResponse *_Nullable response, NSError *_Nullable error) {
Expand Down
22 changes: 17 additions & 5 deletions AnalyticsTests/HTTPClientTest.swift
Original file line number Diff line number Diff line change
Expand Up @@ -127,7 +127,7 @@ class HTTPClientTest: QuickSpec {
done = true
}
expect(done).toEventually(beTrue())
expect(task.state).toEventually(equal(URLSessionTask.State.completed))
expect(task?.state).toEventually(equal(URLSessionTask.State.completed))
}

it("asks to retry for 3xx response") {
Expand All @@ -142,7 +142,7 @@ class HTTPClientTest: QuickSpec {
done = true
}
expect(done).toEventually(beTrue())
expect(task.state).toEventually(equal(URLSessionTask.State.completed))
expect(task?.state).toEventually(equal(URLSessionTask.State.completed))
}

it("does not ask to retry for 4xx response") {
Expand All @@ -157,7 +157,7 @@ class HTTPClientTest: QuickSpec {
done = true
}
expect(done).toEventually(beTrue())
expect(task.state).toEventually(equal(URLSessionTask.State.completed))
expect(task?.state).toEventually(equal(URLSessionTask.State.completed))
}

it("asks to retry for 429 response") {
Expand All @@ -172,7 +172,7 @@ class HTTPClientTest: QuickSpec {
done = true
}
expect(done).toEventually(beTrue())
expect(task.state).toEventually(equal(URLSessionTask.State.completed))
expect(task?.state).toEventually(equal(URLSessionTask.State.completed))
}

it("asks to retry for 5xx response") {
Expand All @@ -187,7 +187,19 @@ class HTTPClientTest: QuickSpec {
done = true
}
expect(done).toEventually(beTrue())
expect(task.state).toEventually(equal(URLSessionTask.State.completed))
expect(task?.state).toEventually(equal(URLSessionTask.State.completed))
}

it("fails when batch size exceeds the max limit size") {
let oversizedBatch: [String: Any] = ["sentAt":"2016-07-19'T'19:25:06Z",
"batch": Array(repeating: ["type":"track", "event":"foo"], count: 16000)]
var done = false
let task = client.upload(oversizedBatch, forWriteKey: "bar") { retry in
expect(retry) == false
done = true
}
expect(done).toEventually(beTrue())
expect(task).toEventually(beNil())
}
}

Expand Down