Skip to content

Commit 2a6b3f9

Browse files
authored
Add Http status check to Realtime stream (#10406)
* Check status code before retrying * Change time out to 5 and a half minutes * Throw exception if unable to establish connection due to non-retryable status code * Format file
1 parent ea551f5 commit 2a6b3f9

File tree

1 file changed

+38
-4
lines changed

1 file changed

+38
-4
lines changed

FirebaseRemoteConfig/Sources/RCNConfigRealtime.m

Lines changed: 38 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,16 @@
4444
// Sends the bundle ID. Refer to b/130301479 for details.
4545
static NSString *const kiOSBundleIdentifierHeaderName =
4646
@"X-Ios-Bundle-Identifier"; ///< HTTP Header Field Name
47+
48+
/// Retryable HTTP status code.
49+
static NSInteger const kRCNFetchResponseHTTPStatusOk = 200;
50+
static NSInteger const kRCNFetchResponseHTTPStatusClientTimeout = 429;
51+
static NSInteger const kRCNFetchResponseHTTPStatusTooManyRequests = 429;
52+
static NSInteger const kRCNFetchResponseHTTPStatusCodeBadGateway = 502;
53+
static NSInteger const kRCNFetchResponseHTTPStatusCodeServiceUnavailable = 503;
54+
static NSInteger const kRCNFetchResponseHTTPStatusCodeGatewayTimeout = 504;
55+
56+
/// Invalidation message field names.
4757
static NSString *const kTemplateVersionNumberKey = @"latestTemplateVersionNumber";
4858
static NSString *const kIsFeatureDisabled = @"featureDisabled";
4959

@@ -52,7 +62,7 @@
5262
/// @param error Error message on failure.
5363
typedef void (^RCNConfigUpdateCompletion)(NSError *_Nullable error);
5464

55-
static NSTimeInterval gTimeoutSeconds = 4320;
65+
static NSTimeInterval gTimeoutSeconds = 330;
5666
static NSInteger const gFetchAttempts = 3;
5767

5868
// Retry parameters
@@ -552,18 +562,42 @@ - (void)URLSession:(NSURLSession *)session
552562
}
553563
}
554564

565+
/// Check if response code is retryable
566+
- (bool)isStatusCodeRetryable:(NSInteger)statusCode {
567+
return statusCode == kRCNFetchResponseHTTPStatusClientTimeout ||
568+
statusCode == kRCNFetchResponseHTTPStatusTooManyRequests ||
569+
statusCode == kRCNFetchResponseHTTPStatusCodeServiceUnavailable ||
570+
statusCode == kRCNFetchResponseHTTPStatusCodeBadGateway ||
571+
statusCode == kRCNFetchResponseHTTPStatusCodeGatewayTimeout;
572+
}
573+
555574
/// Delegate to handle initial reply from the server
556575
- (void)URLSession:(NSURLSession *)session
557576
dataTask:(NSURLSessionDataTask *)dataTask
558577
didReceiveResponse:(NSURLResponse *)response
559578
completionHandler:(void (^)(NSURLSessionResponseDisposition disposition))completionHandler {
560579
_isRequestInProgress = false;
561580
NSHTTPURLResponse *_httpURLResponse = (NSHTTPURLResponse *)response;
562-
if ([_httpURLResponse statusCode] != 200) {
581+
NSInteger statusCode = [_httpURLResponse statusCode];
582+
if (statusCode != kRCNFetchResponseHTTPStatusOk) {
563583
[self pauseRealtimeStream];
564-
[self retryHTTPConnection];
584+
if ([self isStatusCodeRetryable:statusCode]) {
585+
[self retryHTTPConnection];
586+
} else {
587+
NSError *error = [NSError
588+
errorWithDomain:FIRRemoteConfigRealtimeErrorDomain
589+
code:FIRRemoteConfigRealtimeErrorStream
590+
userInfo:@{
591+
NSLocalizedDescriptionKey : [NSString
592+
stringWithFormat:@"StreamError: Received non-retryable status code: %@",
593+
[@(statusCode) stringValue]]
594+
}];
595+
FIRLogError(kFIRLoggerRemoteConfig, @"I-RCN000021", @"Cannot establish connection. Error: %@",
596+
error);
597+
[self propogateErrors:error];
598+
}
565599
} else {
566-
// on success reset retry parameters
600+
/// on success reset retry parameters
567601
_remainingRetryCount = gMaxRetries;
568602
_retrySeconds = arc4random_uniform(5) + 1;
569603

0 commit comments

Comments
 (0)