FSTExponentialBackoff.mm 3.5 KB

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