FIRRetryHelper.m 3.9 KB

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