FSTOnlineStateTracker.mm 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155
  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/FSTDispatchQueue.h"
  19. #include "Firestore/core/src/firebase/firestore/util/hard_assert.h"
  20. #include "Firestore/core/src/firebase/firestore/util/log.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 *onlineStateTimer;
  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. if (self.watchStreamFailures == 0) {
  62. [self setAndBroadcastState:FSTOnlineStateUnknown];
  63. HARD_ASSERT(!self.onlineStateTimer, "onlineStateTimer shouldn't be started yet");
  64. self.onlineStateTimer = [self.queue
  65. dispatchAfterDelay:kOnlineStateTimeout
  66. timerID:FSTTimerIDOnlineStateTimeout
  67. block:^{
  68. self.onlineStateTimer = nil;
  69. HARD_ASSERT(
  70. self.state == FSTOnlineStateUnknown,
  71. "Timer should be canceled if we transitioned to a different state.");
  72. LOG_DEBUG(
  73. "Watch stream didn't reach Online or Offline within %s seconds. "
  74. "Considering 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. // To get to FSTOnlineStateOnline, updateState: must have been called which would have reset
  88. // our heuristics.
  89. HARD_ASSERT(self.watchStreamFailures == 0, "watchStreamFailures must be 0");
  90. HARD_ASSERT(!self.onlineStateTimer, "onlineStateTimer must be nil");
  91. } else {
  92. self.watchStreamFailures++;
  93. if (self.watchStreamFailures >= kMaxWatchStreamFailures) {
  94. [self clearOnlineStateTimer];
  95. [self logClientOfflineWarningIfNecessary];
  96. [self setAndBroadcastState:FSTOnlineStateOffline];
  97. }
  98. }
  99. }
  100. - (void)updateState:(FSTOnlineState)newState {
  101. [self clearOnlineStateTimer];
  102. self.watchStreamFailures = 0;
  103. if (newState == FSTOnlineStateOnline) {
  104. // We've connected to watch at least once. Don't warn the developer about being offline going
  105. // forward.
  106. self.shouldWarnClientIsOffline = NO;
  107. }
  108. [self setAndBroadcastState:newState];
  109. }
  110. - (void)setAndBroadcastState:(FSTOnlineState)newState {
  111. if (newState != self.state) {
  112. self.state = newState;
  113. [self.onlineStateDelegate applyChangedOnlineState:newState];
  114. }
  115. }
  116. - (void)logClientOfflineWarningIfNecessary {
  117. if (self.shouldWarnClientIsOffline) {
  118. LOG_WARN("Could not reach Firestore backend.");
  119. self.shouldWarnClientIsOffline = NO;
  120. }
  121. }
  122. - (void)clearOnlineStateTimer {
  123. if (self.onlineStateTimer) {
  124. [self.onlineStateTimer cancel];
  125. self.onlineStateTimer = nil;
  126. }
  127. }
  128. @end
  129. NS_ASSUME_NONNULL_END