Skip to content

Make backoff account for already-elapsed time. #1712

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
Aug 18, 2018
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
32 changes: 28 additions & 4 deletions Firestore/Source/Remote/FSTExponentialBackoff.mm
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@

#include <random>

#import "Firestore/Source/Util/FSTClasses.h"
#import "Firestore/Source/Util/FSTDispatchQueue.h"

#include "Firestore/core/src/firebase/firestore/util/log.h"
Expand All @@ -33,6 +34,7 @@ @interface FSTExponentialBackoff ()
@property(nonatomic) NSTimeInterval initialDelay;
@property(nonatomic) NSTimeInterval maxDelay;
@property(nonatomic) NSTimeInterval currentBase;
@property(nonatomic) NSTimeInterval lastAttemptTime;
@property(nonatomic, strong, nullable) FSTDelayedCallback *timerCallback;
@end

Expand All @@ -51,6 +53,7 @@ - (instancetype)initWithDispatchQueue:(FSTDispatchQueue *)dispatchQueue
_initialDelay = initialDelay;
_backoffFactor = backoffFactor;
_maxDelay = maxDelay;
_lastAttemptTime = [[NSDate date] timeIntervalSince1970];

[self reset];
}
Expand All @@ -69,13 +72,34 @@ - (void)backoffAndRunBlock:(void (^)(void))block {
[self cancel];

// First schedule the block using the current base (which may be 0 and should be honored as such).
NSTimeInterval delayWithJitter = _currentBase + [self jitterDelay];
NSTimeInterval desiredDelayWithJitter = _currentBase + [self jitterDelay];

// Guard against lastAttemptTime being in the future due to a clock change.
NSTimeInterval delaySoFar = MAX(0, [[NSDate date] timeIntervalSince1970] - self.lastAttemptTime);

// Guard against the backoff delay already being past.
NSTimeInterval remainingDelay = MAX(0, desiredDelayWithJitter - delaySoFar);

if (_currentBase > 0) {
LOG_DEBUG("Backing off for %s seconds (base delay: %s seconds)", delayWithJitter, _currentBase);
LOG_DEBUG(
"Backing off for %s seconds ("
"base delay: %s seconds, "
"delay with jitter: %s seconds, "
"last attempt: %s seconds ago)",
remainingDelay, _currentBase, desiredDelayWithJitter, delaySoFar);
}

self.timerCallback =
[self.dispatchQueue dispatchAfterDelay:delayWithJitter timerID:self.timerID block:block];
FSTWeakify(self);
self.timerCallback = [self.dispatchQueue
dispatchAfterDelay:remainingDelay
timerID:self.timerID
block:^{
FSTStrongify(self);
if (self) {
self.lastAttemptTime = [[NSDate date] timeIntervalSince1970];
block();
}
}];

// Apply backoff factor to determine next delay and ensure it is within bounds.
_currentBase *= _backoffFactor;
Expand Down