FIRRetryHelper.m 4.0 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 "FIRRetryHelper.h"
  17. #import "FUtilities.h"
  18. #import <FirebaseCore/FIRLogger.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 =
  83. (self.currentRetryDelay * self.retryExponent);
  84. self.currentRetryDelay = MIN(newDelay, self.maxRetryDelay);
  85. }
  86. delay = ((1 - self.jitterFactor) * self.currentRetryDelay) +
  87. (self.jitterFactor * self.currentRetryDelay *
  88. [FUtilities randomDouble]);
  89. FFLog(@"I-RDB054002", @"Scheduling retry in %fs", delay);
  90. }
  91. self.lastWasSuccess = NO;
  92. FIRRetryHelperTask *task = [[FIRRetryHelperTask alloc] initWithBlock:block];
  93. self.scheduledRetry = task;
  94. dispatch_time_t popTime =
  95. dispatch_time(DISPATCH_TIME_NOW, (long long)(delay * NSEC_PER_SEC));
  96. dispatch_after(popTime, self.dispatchQueue, ^{
  97. if (![task isCanceled]) {
  98. self.scheduledRetry = nil;
  99. [task execute];
  100. }
  101. });
  102. }
  103. - (void)signalSuccess {
  104. self.lastWasSuccess = YES;
  105. self.currentRetryDelay = 0;
  106. }
  107. - (void)cancel {
  108. if (self.scheduledRetry != nil) {
  109. FFLog(@"I-RDB054003", @"Canceling existing retry attempt");
  110. [self.scheduledRetry cancel];
  111. self.scheduledRetry = nil;
  112. } else {
  113. FFLog(@"I-RDB054004", @"No existing retry attempt to cancel");
  114. }
  115. self.currentRetryDelay = 0;
  116. }
  117. @end