FSTExponentialBackoff.m 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596
  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. [self.dispatchQueue dispatchAfterDelay:delayWithJitter block:block];
  68. // Apply backoff factor to determine next delay and ensure it is within bounds.
  69. _currentBase *= _backoffFactor;
  70. if (_currentBase < _initialDelay) {
  71. _currentBase = _initialDelay;
  72. }
  73. if (_currentBase > _maxDelay) {
  74. _currentBase = _maxDelay;
  75. }
  76. }
  77. /** Returns a random value in the range [-currentBase/2, currentBase/2] */
  78. - (NSTimeInterval)jitterDelay {
  79. return ([FSTUtil randomDouble] - 0.5) * _currentBase;
  80. }
  81. @end