FIRRetryHelper.m 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139
  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 "FIRRetryHelper.h"
  17. #import "FUtilities.h"
  18. @interface FIRRetryHelperTask : NSObject
  19. @property (nonatomic, strong) void (^block)();
  20. @end
  21. @implementation FIRRetryHelperTask
  22. - (instancetype) initWithBlock:(void (^)())block {
  23. self = [super init];
  24. if (self != nil) {
  25. self->_block = [block copy];
  26. }
  27. return self;
  28. }
  29. - (BOOL) isCanceled {
  30. return self.block == nil;
  31. }
  32. - (void) cancel {
  33. self.block = nil;
  34. }
  35. - (void) execute {
  36. if (self.block) {
  37. self.block();
  38. }
  39. }
  40. @end
  41. @interface FIRRetryHelper ()
  42. @property (nonatomic, strong) dispatch_queue_t dispatchQueue;
  43. @property (nonatomic) NSTimeInterval minRetryDelayAfterFailure;
  44. @property (nonatomic) NSTimeInterval maxRetryDelay;
  45. @property (nonatomic) double retryExponent;
  46. @property (nonatomic) double jitterFactor;
  47. @property (nonatomic) BOOL lastWasSuccess;
  48. @property (nonatomic) NSTimeInterval currentRetryDelay;
  49. @property (nonatomic, strong) FIRRetryHelperTask *scheduledRetry;
  50. @end
  51. @implementation FIRRetryHelper
  52. - (instancetype) initWithDispatchQueue:(dispatch_queue_t)dispatchQueue
  53. minRetryDelayAfterFailure:(NSTimeInterval)minRetryDelayAfterFailure
  54. maxRetryDelay:(NSTimeInterval)maxRetryDelay
  55. retryExponent:(double)retryExponent
  56. jitterFactor:(double)jitterFactor {
  57. self = [super init];
  58. if (self != nil) {
  59. self->_dispatchQueue = dispatchQueue;
  60. self->_minRetryDelayAfterFailure = minRetryDelayAfterFailure;
  61. self->_maxRetryDelay = maxRetryDelay;
  62. self->_retryExponent = retryExponent;
  63. self->_jitterFactor = jitterFactor;
  64. self->_lastWasSuccess = YES;
  65. }
  66. return self;
  67. }
  68. - (void) retry:(void (^)())block {
  69. if (self.scheduledRetry != nil) {
  70. FFLog(@"I-RDB054001", @"Canceling existing retry attempt");
  71. [self.scheduledRetry cancel];
  72. self.scheduledRetry = nil;
  73. }
  74. NSTimeInterval delay;
  75. if (self.lastWasSuccess) {
  76. delay = 0;
  77. } else {
  78. if (self.currentRetryDelay == 0) {
  79. self.currentRetryDelay = self.minRetryDelayAfterFailure;
  80. } else {
  81. NSTimeInterval newDelay = (self.currentRetryDelay * self.retryExponent);
  82. self.currentRetryDelay = MIN(newDelay, self.maxRetryDelay);
  83. }
  84. delay = ((1 - self.jitterFactor) * self.currentRetryDelay) +
  85. (self.jitterFactor * self.currentRetryDelay * [FUtilities randomDouble]);
  86. FFLog(@"I-RDB054002", @"Scheduling retry in %fs", delay);
  87. }
  88. self.lastWasSuccess = NO;
  89. FIRRetryHelperTask *task = [[FIRRetryHelperTask alloc] initWithBlock:block];
  90. self.scheduledRetry = task;
  91. dispatch_time_t popTime = dispatch_time(DISPATCH_TIME_NOW, (long long)(delay * NSEC_PER_SEC));
  92. dispatch_after(popTime, self.dispatchQueue, ^{
  93. if (![task isCanceled]) {
  94. self.scheduledRetry = nil;
  95. [task execute];
  96. }
  97. });
  98. }
  99. - (void) signalSuccess {
  100. self.lastWasSuccess = YES;
  101. self.currentRetryDelay = 0;
  102. }
  103. - (void) cancel {
  104. if (self.scheduledRetry != nil) {
  105. FFLog(@"I-RDB054003", @"Canceling existing retry attempt");
  106. [self.scheduledRetry cancel];
  107. self.scheduledRetry = nil;
  108. } else {
  109. FFLog(@"I-RDB054004", @"No existing retry attempt to cancel");
  110. }
  111. self.currentRetryDelay = 0;
  112. }
  113. @end