Files
firebase-ios-sdk/Firestore/Source/Remote/FSTExponentialBackoff.h
T
Michael Lehenbauer 81ee594e32 DispatchQueue delayed callback improvements + testing (#784)
Basically a port of https://github.com/firebase/firebase-js-sdk/commit/a1e346ff93c6cbcc0a1b3b33f0fbc3a7b66e7e12 and https://github.com/firebase/firebase-js-sdk/commit/fce4168309f42aa038125f39818fbf654b65b05f

* Introduces a DelayedCallback helper class in FSTDispatchQueue to encapsulate delayed callback logic.
* Adds cancellation support.
* Updates the idle timer in FSTStream to use new cancellation support.
* Adds a FSTTimerId enum for identifying delayed operations on the queue and uses it to identify our existing backoff and idle timers.
* Added containsDelayedCallback: and runDelayedCallbacksUntil: methods to FSTDispatchQueue which can be used from tests to check for the presence of a callback or to schedule them to run early.
* Removes FSTTestDispatchQueue and changes idle tests to use new test methods.
2018-02-15 16:17:44 -08:00

82 lines
3.0 KiB
Objective-C

/*
* 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 <Foundation/Foundation.h>
#import "Firestore/Source/Util/FSTDispatchQueue.h"
NS_ASSUME_NONNULL_BEGIN
/**
* Helper to implement exponential backoff.
*
* In general, call -reset after each successful round-trip. Call -backoffAndRunBlock before
* retrying after an error. Each backoffAndRunBlock will increase the delay between retries.
*/
@interface FSTExponentialBackoff : NSObject
/**
* Initializes a helper for running delayed tasks following an exponential backoff curve
* between attempts.
*
* Each delay is made up of a "base" delay which follows the exponential backoff curve, and a
* +/- 50% "jitter" that is calculated and added to the base delay. This prevents clients from
* accidentally synchronizing their delays causing spikes of load to the backend.
*
* @param dispatchQueue The dispatch queue to run tasks on.
* @param timerID The ID to use when scheduling backoff operations on the FSTDispatchQueue.
* @param initialDelay The initial delay (used as the base delay on the first retry attempt).
* Note that jitter will still be applied, so the actual delay could be as little as
* 0.5*initialDelay.
* @param backoffFactor The multiplier to use to determine the extended base delay after each
* attempt.
* @param maxDelay The maximum base delay after which no further backoff is performed. Note that
* jitter will still be applied, so the actual delay could be as much as 1.5*maxDelay.
*/
- (instancetype)initWithDispatchQueue:(FSTDispatchQueue *)dispatchQueue
timerID:(FSTTimerID)timerID
initialDelay:(NSTimeInterval)initialDelay
backoffFactor:(double)backoffFactor
maxDelay:(NSTimeInterval)maxDelay NS_DESIGNATED_INITIALIZER;
- (instancetype)init NS_UNAVAILABLE;
/**
* Resets the backoff delay.
*
* The very next backoffAndRunBlock: will have no delay. If it is called again (i.e. due to an
* error), initialDelay (plus jitter) will be used, and subsequent ones will increase according
* to the backoffFactor.
*/
- (void)reset;
/**
* Resets the backoff to the maximum delay (e.g. for use after a RESOURCE_EXHAUSTED error).
*/
- (void)resetToMax;
/**
* Waits for currentDelay seconds, increases the delay and runs the specified block. If there was
* a pending block waiting to be run already, it will be canceled.
*
* @param block The block to run.
*/
- (void)backoffAndRunBlock:(void (^)(void))block;
@end
NS_ASSUME_NONNULL_END