FSTExponentialBackoff.mm 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104
  1. /*
  2. * Copyright 2017 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/FSTExponentialBackoff.h"
  17. #include <random>
  18. #import "Firestore/Source/Util/FSTDispatchQueue.h"
  19. #include "Firestore/core/src/firebase/firestore/util/log.h"
  20. #include "Firestore/core/src/firebase/firestore/util/secure_random.h"
  21. using firebase::firestore::util::SecureRandom;
  22. @interface FSTExponentialBackoff ()
  23. @property(nonatomic, strong) FSTDispatchQueue *dispatchQueue;
  24. @property(nonatomic, assign, readonly) FSTTimerID timerID;
  25. @property(nonatomic) double backoffFactor;
  26. @property(nonatomic) NSTimeInterval initialDelay;
  27. @property(nonatomic) NSTimeInterval maxDelay;
  28. @property(nonatomic) NSTimeInterval currentBase;
  29. @property(nonatomic, strong, nullable) FSTDelayedCallback *timerCallback;
  30. @end
  31. @implementation FSTExponentialBackoff {
  32. SecureRandom _secureRandom;
  33. }
  34. - (instancetype)initWithDispatchQueue:(FSTDispatchQueue *)dispatchQueue
  35. timerID:(FSTTimerID)timerID
  36. initialDelay:(NSTimeInterval)initialDelay
  37. backoffFactor:(double)backoffFactor
  38. maxDelay:(NSTimeInterval)maxDelay {
  39. if (self = [super init]) {
  40. _dispatchQueue = dispatchQueue;
  41. _timerID = timerID;
  42. _initialDelay = initialDelay;
  43. _backoffFactor = backoffFactor;
  44. _maxDelay = maxDelay;
  45. [self reset];
  46. }
  47. return self;
  48. }
  49. - (void)reset {
  50. _currentBase = 0;
  51. }
  52. - (void)resetToMax {
  53. _currentBase = _maxDelay;
  54. }
  55. - (void)backoffAndRunBlock:(void (^)(void))block {
  56. [self cancel];
  57. // First schedule the block using the current base (which may be 0 and should be honored as such).
  58. NSTimeInterval delayWithJitter = _currentBase + [self jitterDelay];
  59. if (_currentBase > 0) {
  60. LOG_DEBUG("Backing off for %s seconds (base delay: %s seconds)", delayWithJitter, _currentBase);
  61. }
  62. self.timerCallback =
  63. [self.dispatchQueue dispatchAfterDelay:delayWithJitter timerID:self.timerID block:block];
  64. // Apply backoff factor to determine next delay and ensure it is within bounds.
  65. _currentBase *= _backoffFactor;
  66. if (_currentBase < _initialDelay) {
  67. _currentBase = _initialDelay;
  68. }
  69. if (_currentBase > _maxDelay) {
  70. _currentBase = _maxDelay;
  71. }
  72. }
  73. - (void)cancel {
  74. if (self.timerCallback) {
  75. [self.timerCallback cancel];
  76. self.timerCallback = nil;
  77. }
  78. }
  79. /** Returns a random value in the range [-currentBase/2, currentBase/2] */
  80. - (NSTimeInterval)jitterDelay {
  81. std::uniform_real_distribution<double> dist;
  82. double random_double = dist(_secureRandom);
  83. return (random_double - 0.5) * _currentBase;
  84. }
  85. @end