Skip to content

Add Http status check to Realtime stream #10406

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

Merged
merged 7 commits into from
Nov 8, 2022
Merged
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
42 changes: 38 additions & 4 deletions FirebaseRemoteConfig/Sources/RCNConfigRealtime.m
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,16 @@
// Sends the bundle ID. Refer to b/130301479 for details.
static NSString *const kiOSBundleIdentifierHeaderName =
@"X-Ios-Bundle-Identifier"; ///< HTTP Header Field Name

/// Retryable HTTP status code.
static NSInteger const kRCNFetchResponseHTTPStatusOk = 200;
static NSInteger const kRCNFetchResponseHTTPStatusClientTimeout = 429;
static NSInteger const kRCNFetchResponseHTTPStatusTooManyRequests = 429;
static NSInteger const kRCNFetchResponseHTTPStatusCodeBadGateway = 502;
static NSInteger const kRCNFetchResponseHTTPStatusCodeServiceUnavailable = 503;
static NSInteger const kRCNFetchResponseHTTPStatusCodeGatewayTimeout = 504;

/// Invalidation message field names.
static NSString *const kTemplateVersionNumberKey = @"latestTemplateVersionNumber";
static NSString *const kIsFeatureDisabled = @"featureDisabled";

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

static NSTimeInterval gTimeoutSeconds = 4320;
static NSTimeInterval gTimeoutSeconds = 330;
static NSInteger const gFetchAttempts = 3;

// Retry parameters
Expand Down Expand Up @@ -552,18 +562,42 @@ - (void)URLSession:(NSURLSession *)session
}
}

/// Check if response code is retryable
- (bool)isStatusCodeRetryable:(NSInteger)statusCode {
return statusCode == kRCNFetchResponseHTTPStatusClientTimeout ||
statusCode == kRCNFetchResponseHTTPStatusTooManyRequests ||
statusCode == kRCNFetchResponseHTTPStatusCodeServiceUnavailable ||
statusCode == kRCNFetchResponseHTTPStatusCodeBadGateway ||
statusCode == kRCNFetchResponseHTTPStatusCodeGatewayTimeout;
}

/// Delegate to handle initial reply from the server
- (void)URLSession:(NSURLSession *)session
dataTask:(NSURLSessionDataTask *)dataTask
didReceiveResponse:(NSURLResponse *)response
completionHandler:(void (^)(NSURLSessionResponseDisposition disposition))completionHandler {
_isRequestInProgress = false;
NSHTTPURLResponse *_httpURLResponse = (NSHTTPURLResponse *)response;
if ([_httpURLResponse statusCode] != 200) {
NSInteger statusCode = [_httpURLResponse statusCode];
if (statusCode != kRCNFetchResponseHTTPStatusOk) {
[self pauseRealtimeStream];
[self retryHTTPConnection];
if ([self isStatusCodeRetryable:statusCode]) {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

for cases where we don't retry, should we return an error to the client?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yeah it'd make sense to return a StreamError if we can't retry, updated.

[self retryHTTPConnection];
} else {
NSError *error = [NSError
errorWithDomain:FIRRemoteConfigRealtimeErrorDomain
code:FIRRemoteConfigRealtimeErrorStream
userInfo:@{
NSLocalizedDescriptionKey : [NSString
stringWithFormat:@"StreamError: Received non-retryable status code: %@",
[@(statusCode) stringValue]]
}];
FIRLogError(kFIRLoggerRemoteConfig, @"I-RCN000021", @"Cannot establish connection. Error: %@",
error);
[self propogateErrors:error];
}
} else {
// on success reset retry parameters
/// on success reset retry parameters
_remainingRetryCount = gMaxRetries;
_retrySeconds = arc4random_uniform(5) + 1;

Expand Down