Skip to content

Commit 958760f

Browse files
authored
Merge pull request #736 from segmentio/revert-2e6c57d4146ae2f25b5b372a430224de10319c54
Revert "Create HTTP requests on a background queue."
2 parents 05d1d69 + 9ad9486 commit 958760f

File tree

4 files changed

+34
-45
lines changed

4 files changed

+34
-45
lines changed

Analytics/Classes/Internal/SEGHTTPClient.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,7 @@ NS_ASSUME_NONNULL_BEGIN
3232
* NOTE: You need to re-dispatch within the completionHandler onto a desired queue to avoid threading issues.
3333
* Completion handlers are called on a dispatch queue internal to SEGHTTPClient.
3434
*/
35-
- (NSURLSessionUploadTask *)upload:(NSArray *)batch forWriteKey:(NSString *)writeKey completionHandler:(void (^)(BOOL retry))completionHandler;
35+
- (NSURLSessionUploadTask *)upload:(JSON_DICT)batch forWriteKey:(NSString *)writeKey completionHandler:(void (^)(BOOL retry))completionHandler;
3636

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

Analytics/Classes/Internal/SEGHTTPClient.m

Lines changed: 4 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -62,7 +62,7 @@ - (void)dealloc
6262
}
6363

6464

65-
- (NSURLSessionUploadTask *)upload:(NSArray *)batch forWriteKey:(NSString *)writeKey completionHandler:(void (^)(BOOL retry))completionHandler
65+
- (NSURLSessionUploadTask *)upload:(NSDictionary *)batch forWriteKey:(NSString *)writeKey completionHandler:(void (^)(BOOL retry))completionHandler
6666
{
6767
// batch = SEGCoerceDictionary(batch);
6868
NSURLSession *session = [self sessionForWriteKey:writeKey];
@@ -75,16 +75,11 @@ - (NSURLSessionUploadTask *)upload:(NSArray *)batch forWriteKey:(NSString *)writ
7575

7676
[request setHTTPMethod:@"POST"];
7777

78-
// In particular, set the sentAt after the requestFactory is invoked so that it can be as up to date as possible.
79-
NSMutableDictionary *payload = [[NSMutableDictionary alloc] init];
80-
[payload setObject:iso8601FormattedString([NSDate date]) forKey:@"sentAt"];
81-
[payload setObject:batch forKey:@"batch"];
82-
8378
NSError *error = nil;
8479
NSException *exception = nil;
85-
NSData *payloadData = nil;
80+
NSData *payload = nil;
8681
@try {
87-
payloadData = [NSJSONSerialization dataWithJSONObject:batch options:0 error:&error];
82+
payload = [NSJSONSerialization dataWithJSONObject:batch options:0 error:&error];
8883
}
8984
@catch (NSException *exc) {
9085
exception = exc;
@@ -94,7 +89,7 @@ - (NSURLSessionUploadTask *)upload:(NSArray *)batch forWriteKey:(NSString *)writ
9489
completionHandler(NO); // Don't retry this batch.
9590
return nil;
9691
}
97-
NSData *gzippedPayload = [payloadData seg_gzippedData];
92+
NSData *gzippedPayload = [payload seg_gzippedData];
9893

9994
NSURLSessionUploadTask *task = [session uploadTaskWithRequest:request fromData:gzippedPayload completionHandler:^(NSData *_Nullable data, NSURLResponse *_Nullable response, NSError *_Nullable error) {
10095
if (error) {

Analytics/Classes/Internal/SEGSegmentIntegration.m

Lines changed: 23 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -55,12 +55,10 @@ @interface SEGSegmentIntegration ()
5555
@property (nonatomic, strong) NSMutableArray *queue;
5656
@property (nonatomic, strong) NSDictionary *cachedStaticContext;
5757
@property (nonatomic, strong) NSURLSessionUploadTask *batchRequest;
58-
@property (nonatomic, assign) BOOL batchRequestInProgress; // Thread confined to serialQueue.
5958
@property (nonatomic, assign) UIBackgroundTaskIdentifier flushTaskID;
6059
@property (nonatomic, strong) SEGReachability *reachability;
6160
@property (nonatomic, strong) NSTimer *flushTimer;
6261
@property (nonatomic, strong) dispatch_queue_t serialQueue;
63-
@property (nonatomic, strong) dispatch_queue_t networkQueue;
6462
@property (nonatomic, strong) dispatch_queue_t backgroundTaskQueue;
6563
@property (nonatomic, strong) NSMutableDictionary *traits;
6664
@property (nonatomic, assign) SEGAnalytics *analytics;
@@ -89,9 +87,8 @@ - (id)initWithAnalytics:(SEGAnalytics *)analytics httpClient:(SEGHTTPClient *)ht
8987
self.reachability = [SEGReachability reachabilityWithHostname:@"google.com"];
9088
[self.reachability startNotifier];
9189
self.cachedStaticContext = [self staticContext];
92-
self.serialQueue = seg_dispatch_queue_create_specific("com.segment.analytics.segment", DISPATCH_QUEUE_SERIAL);
93-
self.networkQueue = seg_dispatch_queue_create_specific("com.segment.analytics.segment.network", DISPATCH_QUEUE_SERIAL);
94-
self.backgroundTaskQueue = seg_dispatch_queue_create_specific("com.segment.analytics.segment.backgroundTask", DISPATCH_QUEUE_SERIAL);
90+
self.serialQueue = seg_dispatch_queue_create_specific("io.segment.analytics.segmentio", DISPATCH_QUEUE_SERIAL);
91+
self.backgroundTaskQueue = seg_dispatch_queue_create_specific("io.segment.analytics.backgroundTask", DISPATCH_QUEUE_SERIAL);
9592
self.flushTaskID = UIBackgroundTaskInvalid;
9693

9794
#if !TARGET_OS_TV
@@ -490,7 +487,7 @@ - (void)flushWithMaxSize:(NSUInteger)maxBatchSize
490487
[self endBackgroundTask];
491488
return;
492489
}
493-
if (self.batchRequestInProgress) {
490+
if (self.batchRequest != nil) {
494491
SEGLog(@"%@ API request already in progress, not flushing again.", self);
495492
return;
496493
}
@@ -511,7 +508,7 @@ - (void)flushQueueByLength
511508
[self dispatchBackground:^{
512509
SEGLog(@"%@ Length is %lu.", self, (unsigned long)self.queue.count);
513510

514-
if (!self.batchRequestInProgress && [self.queue count] >= self.configuration.flushAt) {
511+
if (self.batchRequest == nil && [self.queue count] >= self.configuration.flushAt) {
515512
[self flush];
516513
}
517514
}];
@@ -543,37 +540,31 @@ - (void)notifyForName:(NSString *)name userInfo:(id)userInfo
543540

544541
- (void)sendData:(NSArray *)batch
545542
{
546-
if (self.batchRequestInProgress) {
547-
SEGLog(@"API request already in progress, not flushing again.");
548-
return;
549-
}
550-
551-
self.batchRequestInProgress = true;
543+
NSMutableDictionary *payload = [[NSMutableDictionary alloc] init];
544+
[payload setObject:iso8601FormattedString([NSDate date]) forKey:@"sentAt"];
545+
[payload setObject:batch forKey:@"batch"];
552546

553-
seg_dispatch_specific_async(self.networkQueue, ^{
554-
SEGLog(@"%@ Flushing %lu of %lu queued API calls.", self, (unsigned long)batch.count, (unsigned long)self.queue.count);
547+
SEGLog(@"%@ Flushing %lu of %lu queued API calls.", self, (unsigned long)batch.count, (unsigned long)self.queue.count);
548+
SEGLog(@"Flushing batch %@.", payload);
555549

556-
self.batchRequest = [self.httpClient upload:batch forWriteKey:self.configuration.writeKey completionHandler:^(BOOL retry) {
557-
[self dispatchBackground:^{
558-
self.batchRequestInProgress = false;
559-
560-
if (retry) {
561-
[self notifyForName:SEGSegmentRequestDidFailNotification userInfo:batch];
562-
self.batchRequest = nil;
563-
[self endBackgroundTask];
564-
return;
565-
}
566-
567-
[self.queue removeObjectsInArray:batch];
568-
[self persistQueue];
569-
[self notifyForName:SEGSegmentRequestDidSucceedNotification userInfo:batch];
550+
self.batchRequest = [self.httpClient upload:payload forWriteKey:self.configuration.writeKey completionHandler:^(BOOL retry) {
551+
[self dispatchBackground:^{
552+
if (retry) {
553+
[self notifyForName:SEGSegmentRequestDidFailNotification userInfo:batch];
570554
self.batchRequest = nil;
571555
[self endBackgroundTask];
572-
}];
556+
return;
557+
}
558+
559+
[self.queue removeObjectsInArray:batch];
560+
[self persistQueue];
561+
[self notifyForName:SEGSegmentRequestDidSucceedNotification userInfo:batch];
562+
self.batchRequest = nil;
563+
[self endBackgroundTask];
573564
}];
565+
}];
574566

575-
[self notifyForName:SEGSegmentDidSendRequestNotification userInfo:batch];
576-
});
567+
[self notifyForName:SEGSegmentDidSendRequestNotification userInfo:batch];
577568
}
578569

579570
- (void)applicationDidEnterBackground

AnalyticsTests/HTTPClientTest.swift

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -95,8 +95,11 @@ class HTTPClientTest: QuickSpec {
9595

9696
describe("upload") {
9797
it("does not ask to retry for json error") {
98-
// Dates cannot be serialized as is so the json serialzation will fail.
99-
let batch = [[ "foo" : NSDate() ] ]
98+
let batch: [String: Any] = [
99+
// Dates cannot be serialized as is so the json serialzation will fail.
100+
"sentAt": NSDate(),
101+
"batch": [["type": "track", "event": "foo"]],
102+
]
100103
var done = false
101104
let task = client.upload(batch, forWriteKey: "bar") { retry in
102105
expect(retry) == false
@@ -106,7 +109,7 @@ class HTTPClientTest: QuickSpec {
106109
expect(done).toEventually(beTrue())
107110
}
108111

109-
let batch = [["type":"track", "event":"foo"]]
112+
let batch: [String: Any] = ["sentAt":"2016-07-19'T'19:25:06Z", "batch":[["type":"track", "event":"foo"]]]
110113

111114

112115
it("does not ask to retry for 2xx response") {

0 commit comments

Comments
 (0)