AutorÃa | Ultima modificación | Ver Log |
// Copyright 2019 Google//// Licensed under the Apache License, Version 2.0 (the "License");// you may not use this file except in compliance with the License.// You may obtain a copy of the License at//// http://www.apache.org/licenses/LICENSE-2.0//// Unless required by applicable law or agreed to in writing, software// distributed under the License is distributed on an "AS IS" BASIS,// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.// See the License for the specific language governing permissions and// limitations under the License.#import "Crashlytics/Shared/FIRCLSNetworking/FIRCLSFABNetworkClient.h"#import "Crashlytics/Shared/FIRCLSNetworking/FIRCLSNetworkResponseHandler.h"static const float FIRCLSNetworkMinimumRetryJitter = 0.90f;static const float FIRCLSNetworkMaximumRetryJitter = 1.10f;const NSUInteger FIRCLSNetworkMaximumRetryCount = 10;@interface FIRCLSFABNetworkClient () <NSURLSessionDelegate, NSURLSessionTaskDelegate>@property(nonatomic, strong, readonly) NSURLSession *session;@end@implementation FIRCLSFABNetworkClient- (instancetype)init {return [self initWithQueue:nil];}- (instancetype)initWithQueue:(nullable NSOperationQueue *)operationQueue {NSURLSessionConfiguration *config = [NSURLSessionConfiguration defaultSessionConfiguration];return [self initWithSessionConfiguration:config queue:operationQueue];}- (instancetype)initWithSessionConfiguration:(NSURLSessionConfiguration *)configqueue:(nullable NSOperationQueue *)operationQueue {self = [super init];if (!self) {return nil;}_session = [NSURLSession sessionWithConfiguration:configdelegate:selfdelegateQueue:operationQueue];if (!_session) {return nil;}return self;}- (void)dealloc {[_session finishTasksAndInvalidate];}#pragma mark - Delay Handling- (double)randomDoubleWithMin:(double)min max:(double)max {return min + ((max - min) * drand48());}- (double)generateRandomJitter {return [self randomDoubleWithMin:FIRCLSNetworkMinimumRetryJittermax:FIRCLSNetworkMaximumRetryJitter];}- (NSTimeInterval)computeDelayForResponse:(NSURLResponse *)responsewithRetryCount:(NSUInteger)count {NSTimeInterval initialValue = [FIRCLSNetworkResponseHandler retryValueForResponse:response];// make sure count is > 0count = MAX(count, 1);// make sure initialValue is >2 for exponential backoff to work reasonably with low count numbersinitialValue = MAX(initialValue, 2.0);const double jitter = [self generateRandomJitter];return pow(initialValue, count) * jitter; // exponential backoff}- (void)runAfterRetryValueFromResponse:(NSURLResponse *)responseattempts:(NSUInteger)countonQueue:(dispatch_queue_t)queueblock:(void (^)(void))block {const NSTimeInterval delay = [self computeDelayForResponse:response withRetryCount:count];dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (uint64_t)(delay * NSEC_PER_SEC)), queue, block);}- (void)runAfterRetryValueFromResponse:(NSURLResponse *)responseattempts:(NSUInteger)countblock:(void (^)(void))block {dispatch_queue_t queue = dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0);[self runAfterRetryValueFromResponse:response attempts:count onQueue:queue block:block];}#pragma mark - Tasks- (void)startDataTaskWithRequest:(NSURLRequest *)requestretryLimit:(NSUInteger)retryLimittries:(NSUInteger)triescompletionHandler:(FIRCLSNetworkDataTaskCompletionHandlerBlock)completionHandler {NSURLSessionTask *task = [self.sessiondataTaskWithRequest:requestcompletionHandler:^(NSData *data, NSURLResponse *response, NSError *taskError) {[FIRCLSNetworkResponseHandlerhandleCompletedResponse:responseforOriginalRequest:requesterror:taskErrorblock:^(BOOL retry, NSError *error) {if (!retry) {completionHandler(data, response, error);return;}if (tries >= retryLimit) {NSDictionary *userInfo = @{@"retryLimit" : @(retryLimit),NSURLErrorFailingURLStringErrorKey : request.URL};completionHandler(nil, nil,[NSErrorerrorWithDomain:FIRCLSNetworkErrorDomaincode:FIRCLSNetworkErrorMaximumAttemptsReacheduserInfo:userInfo]);return;}[selfrunAfterRetryValueFromResponse:responseattempts:triesblock:^{[selfstartDataTaskWithRequest:requestretryLimit:retryLimittries:(tries +1)completionHandler:completionHandler];}];}];}];[task resume];if (!task) {completionHandler(nil, nil,[NSError errorWithDomain:FIRCLSNetworkErrorDomaincode:FIRCLSNetworkErrorFailedToStartOperationuserInfo:nil]);}}- (void)startDataTaskWithRequest:(NSURLRequest *)requestretryLimit:(NSUInteger)retryLimitcompletionHandler:(FIRCLSNetworkDataTaskCompletionHandlerBlock)completionHandler {[self startDataTaskWithRequest:requestretryLimit:retryLimittries:0completionHandler:completionHandler];}- (void)startDataTaskWithRequest:(NSURLRequest *)requestcompletionHandler:(FIRCLSNetworkDataTaskCompletionHandlerBlock)completionHandler {[self startDataTaskWithRequest:requestretryLimit:FIRCLSNetworkMaximumRetryCountcompletionHandler:completionHandler];}- (void)startDownloadTaskWithRequest:(NSURLRequest *)requestretryLimit:(NSUInteger)retryLimittries:(NSUInteger)triescompletionHandler:(FIRCLSNetworkDownloadTaskCompletionHandlerBlock)completionHandler {NSURLSessionTask *task = [self.sessiondownloadTaskWithRequest:requestcompletionHandler:^(NSURL *location, NSURLResponse *response, NSError *taskError) {[FIRCLSNetworkResponseHandlerhandleCompletedResponse:responseforOriginalRequest:requesterror:taskErrorblock:^(BOOL retry, NSError *error) {if (!retry) {completionHandler(location, response, error);return;}if (tries >= retryLimit) {NSDictionary *userInfo = @{@"retryLimit" : @(retryLimit),NSURLErrorFailingURLStringErrorKey : request.URL};completionHandler(nil, nil,[NSErrorerrorWithDomain:FIRCLSNetworkErrorDomaincode:FIRCLSNetworkErrorMaximumAttemptsReacheduserInfo:userInfo]);return;}[selfrunAfterRetryValueFromResponse:responseattempts:triesblock:^{[selfstartDownloadTaskWithRequest:requestretryLimit:retryLimittries:(tries +1)completionHandler:completionHandler];}];}];}];[task resume];if (!task) {completionHandler(nil, nil,[NSError errorWithDomain:FIRCLSNetworkErrorDomaincode:FIRCLSNetworkErrorFailedToStartOperationuserInfo:nil]);}}- (void)startDownloadTaskWithRequest:(NSURLRequest *)requestretryLimit:(NSUInteger)retryLimitcompletionHandler:(FIRCLSNetworkDownloadTaskCompletionHandlerBlock)completionHandler {[self startDownloadTaskWithRequest:requestretryLimit:retryLimittries:0completionHandler:completionHandler];}- (void)startDownloadTaskWithRequest:(NSURLRequest *)requestcompletionHandler:(FIRCLSNetworkDownloadTaskCompletionHandlerBlock)completionHandler {[self startDownloadTaskWithRequest:requestretryLimit:FIRCLSNetworkMaximumRetryCountcompletionHandler:completionHandler];}- (void)invalidateAndCancel {[self.session invalidateAndCancel];}#pragma mark - NSURLSession Delegate- (void)URLSession:(NSURLSession *)session didBecomeInvalidWithError:(NSError *)error {}@end