FSTExponentialBackoff.mm 3.6 KB

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