FSTOnlineStateTracker.mm 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149
  1. /*
  2. * Copyright 2018 Google
  3. *
  4. * Licensed under the Apache License, Version 2.0 (the "License");
  5. * you may not use this file except in compliance with the License.
  6. * You may obtain a copy of the License at
  7. *
  8. * http://www.apache.org/licenses/LICENSE-2.0
  9. *
  10. * Unless required by applicable law or agreed to in writing, software
  11. * distributed under the License is distributed on an "AS IS" BASIS,
  12. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  13. * See the License for the specific language governing permissions and
  14. * limitations under the License.
  15. */
  16. #import "Firestore/Source/Remote/FSTOnlineStateTracker.h"
  17. #import "Firestore/Source/Remote/FSTRemoteStore.h"
  18. #import "Firestore/Source/Util/FSTAssert.h"
  19. #import "Firestore/Source/Util/FSTDispatchQueue.h"
  20. #import "Firestore/Source/Util/FSTLogger.h"
  21. NS_ASSUME_NONNULL_BEGIN
  22. // To deal with transient failures, we allow multiple stream attempts before giving up and
  23. // transitioning from FSTOnlineState Unknown to Offline.
  24. static const int kMaxWatchStreamFailures = 2;
  25. // To deal with stream attempts that don't succeed or fail in a timely manner, we have a
  26. // timeout for FSTOnlineState to reach Online or Offline. If the timeout is reached, we transition
  27. // to Offline rather than waiting indefinitely.
  28. static const NSTimeInterval kOnlineStateTimeout = 10;
  29. @interface FSTOnlineStateTracker ()
  30. /** The current FSTOnlineState. */
  31. @property(nonatomic, assign) FSTOnlineState state;
  32. /**
  33. * A count of consecutive failures to open the stream. If it reaches the maximum defined by
  34. * kMaxWatchStreamFailures, we'll revert to FSTOnlineStateOffline.
  35. */
  36. @property(nonatomic, assign) int watchStreamFailures;
  37. /**
  38. * A timer that elapses after kOnlineStateTimeout, at which point we transition from FSTOnlineState
  39. * Unknown to Offline without waiting for the stream to actually fail (kMaxWatchStreamFailures
  40. * times).
  41. */
  42. @property(nonatomic, strong, nullable) FSTDelayedCallback *watchStreamTimer;
  43. /**
  44. * Whether the client should log a warning message if it fails to connect to the backend
  45. * (initially YES, cleared after a successful stream, or if we've logged the message already).
  46. */
  47. @property(nonatomic, assign) BOOL shouldWarnClientIsOffline;
  48. /** The FSTDispatchQueue to use for running timers (and to call onlineStateDelegate). */
  49. @property(nonatomic, strong, readonly) FSTDispatchQueue *queue;
  50. @end
  51. @implementation FSTOnlineStateTracker
  52. - (instancetype)initWithWorkerDispatchQueue:(FSTDispatchQueue *)queue {
  53. if (self = [super init]) {
  54. _queue = queue;
  55. _state = FSTOnlineStateUnknown;
  56. _shouldWarnClientIsOffline = YES;
  57. }
  58. return self;
  59. }
  60. - (void)handleWatchStreamStart {
  61. [self setAndBroadcastState:FSTOnlineStateUnknown];
  62. if (self.watchStreamTimer == nil) {
  63. self.watchStreamTimer = [self.queue
  64. dispatchAfterDelay:kOnlineStateTimeout
  65. timerID:FSTTimerIDOnlineStateTimeout
  66. block:^{
  67. self.watchStreamTimer = nil;
  68. FSTAssert(
  69. self.state == FSTOnlineStateUnknown,
  70. @"Timer should be canceled if we transitioned to a different state.");
  71. FSTLog(
  72. @"Watch stream didn't reach Online or Offline within %f seconds. "
  73. @"Considering "
  74. "client offline.",
  75. kOnlineStateTimeout);
  76. [self logClientOfflineWarningIfNecessary];
  77. [self setAndBroadcastState:FSTOnlineStateOffline];
  78. // NOTE: handleWatchStreamFailure will continue to increment
  79. // watchStreamFailures even though we are already marked Offline but this is
  80. // non-harmful.
  81. }];
  82. }
  83. }
  84. - (void)handleWatchStreamFailure {
  85. if (self.state == FSTOnlineStateOnline) {
  86. [self setAndBroadcastState:FSTOnlineStateUnknown];
  87. } else {
  88. self.watchStreamFailures++;
  89. if (self.watchStreamFailures >= kMaxWatchStreamFailures) {
  90. [self clearOnlineStateTimer];
  91. [self logClientOfflineWarningIfNecessary];
  92. [self setAndBroadcastState:FSTOnlineStateOffline];
  93. }
  94. }
  95. }
  96. - (void)updateState:(FSTOnlineState)newState {
  97. [self clearOnlineStateTimer];
  98. self.watchStreamFailures = 0;
  99. if (newState == FSTOnlineStateOnline) {
  100. // We've connected to watch at least once. Don't warn the developer about being offline going
  101. // forward.
  102. self.shouldWarnClientIsOffline = NO;
  103. }
  104. [self setAndBroadcastState:newState];
  105. }
  106. - (void)setAndBroadcastState:(FSTOnlineState)newState {
  107. if (newState != self.state) {
  108. self.state = newState;
  109. [self.onlineStateDelegate applyChangedOnlineState:newState];
  110. }
  111. }
  112. - (void)logClientOfflineWarningIfNecessary {
  113. if (self.shouldWarnClientIsOffline) {
  114. FSTWarn(@"Could not reach Firestore backend.");
  115. self.shouldWarnClientIsOffline = NO;
  116. }
  117. }
  118. - (void)clearOnlineStateTimer {
  119. if (self.watchStreamTimer) {
  120. [self.watchStreamTimer cancel];
  121. self.watchStreamTimer = nil;
  122. }
  123. }
  124. @end
  125. NS_ASSUME_NONNULL_END