aboutsummaryrefslogtreecommitdiffhomepage
path: root/Firebase/Database/Core/Utilities/FIRRetryHelper.m
blob: 199e17da0fdf98153ff0cf56ecc3f95702ee7339 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
/*
 * Copyright 2017 Google
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

#import "FIRRetryHelper.h"
#import "FUtilities.h"

@interface FIRRetryHelperTask : NSObject

@property (nonatomic, strong) void (^block)();

@end

@implementation FIRRetryHelperTask

- (instancetype) initWithBlock:(void (^)())block {
    self = [super init];
    if (self != nil) {
        self->_block = [block copy];
    }
    return self;
}

- (BOOL) isCanceled {
    return self.block == nil;
}

- (void) cancel {
    self.block = nil;
}

- (void) execute {
    if (self.block) {
        self.block();
    }
}

@end



@interface FIRRetryHelper ()

@property (nonatomic, strong) dispatch_queue_t dispatchQueue;
@property (nonatomic) NSTimeInterval minRetryDelayAfterFailure;
@property (nonatomic) NSTimeInterval maxRetryDelay;
@property (nonatomic) double retryExponent;
@property (nonatomic) double jitterFactor;

@property (nonatomic) BOOL lastWasSuccess;
@property (nonatomic) NSTimeInterval currentRetryDelay;

@property (nonatomic, strong) FIRRetryHelperTask *scheduledRetry;

@end

@implementation FIRRetryHelper

- (instancetype) initWithDispatchQueue:(dispatch_queue_t)dispatchQueue
             minRetryDelayAfterFailure:(NSTimeInterval)minRetryDelayAfterFailure
                         maxRetryDelay:(NSTimeInterval)maxRetryDelay
                         retryExponent:(double)retryExponent
                          jitterFactor:(double)jitterFactor {
    self = [super init];
    if (self != nil) {
        self->_dispatchQueue = dispatchQueue;
        self->_minRetryDelayAfterFailure = minRetryDelayAfterFailure;
        self->_maxRetryDelay = maxRetryDelay;
        self->_retryExponent = retryExponent;
        self->_jitterFactor = jitterFactor;
        self->_lastWasSuccess = YES;
    }
    return self;
}

- (void) retry:(void (^)())block {
    if (self.scheduledRetry != nil) {
        FFLog(@"I-RDB054001", @"Canceling existing retry attempt");
        [self.scheduledRetry cancel];
        self.scheduledRetry = nil;
    }

    NSTimeInterval delay;
    if (self.lastWasSuccess) {
        delay = 0;
    } else {
        if (self.currentRetryDelay == 0) {
            self.currentRetryDelay = self.minRetryDelayAfterFailure;
        } else {
            NSTimeInterval newDelay = (self.currentRetryDelay * self.retryExponent);
            self.currentRetryDelay = MIN(newDelay, self.maxRetryDelay);
        }

        delay = ((1 - self.jitterFactor) * self.currentRetryDelay) +
                (self.jitterFactor * self.currentRetryDelay * [FUtilities randomDouble]);
        FFLog(@"I-RDB054002", @"Scheduling retry in %fs", delay);

    }
    self.lastWasSuccess = NO;
    FIRRetryHelperTask *task = [[FIRRetryHelperTask alloc] initWithBlock:block];
    self.scheduledRetry = task;
    dispatch_time_t popTime = dispatch_time(DISPATCH_TIME_NOW, (long long)(delay * NSEC_PER_SEC));
    dispatch_after(popTime, self.dispatchQueue, ^{
        if (![task isCanceled]) {
            self.scheduledRetry = nil;
            [task execute];
        }
    });
}

- (void) signalSuccess {
    self.lastWasSuccess = YES;
    self.currentRetryDelay = 0;
}

- (void) cancel {
    if (self.scheduledRetry != nil) {
        FFLog(@"I-RDB054003", @"Canceling existing retry attempt");
        [self.scheduledRetry cancel];
        self.scheduledRetry = nil;
    } else {
        FFLog(@"I-RDB054004", @"No existing retry attempt to cancel");
    }
    self.currentRetryDelay = 0;
}

@end