FSTExponentialBackoff.m 3.4 KB

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