FSTExponentialBackoff.h 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384
  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 <Foundation/Foundation.h>
  17. #import "Firestore/Source/Util/FSTDispatchQueue.h"
  18. NS_ASSUME_NONNULL_BEGIN
  19. /**
  20. * Helper to implement exponential backoff.
  21. *
  22. * In general, call -reset after each successful round-trip. Call -backoffAndRunBlock before
  23. * retrying after an error. Each backoffAndRunBlock will increase the delay between retries.
  24. */
  25. @interface FSTExponentialBackoff : NSObject
  26. /**
  27. * Initializes a helper for running delayed tasks following an exponential backoff curve
  28. * between attempts.
  29. *
  30. * Each delay is made up of a "base" delay which follows the exponential backoff curve, and a
  31. * +/- 50% "jitter" that is calculated and added to the base delay. This prevents clients from
  32. * accidentally synchronizing their delays causing spikes of load to the backend.
  33. *
  34. * @param dispatchQueue The dispatch queue to run tasks on.
  35. * @param timerID The ID to use when scheduling backoff operations on the FSTDispatchQueue.
  36. * @param initialDelay The initial delay (used as the base delay on the first retry attempt).
  37. * Note that jitter will still be applied, so the actual delay could be as little as
  38. * 0.5*initialDelay.
  39. * @param backoffFactor The multiplier to use to determine the extended base delay after each
  40. * attempt.
  41. * @param maxDelay The maximum base delay after which no further backoff is performed. Note that
  42. * jitter will still be applied, so the actual delay could be as much as 1.5*maxDelay.
  43. */
  44. - (instancetype)initWithDispatchQueue:(FSTDispatchQueue *)dispatchQueue
  45. timerID:(FSTTimerID)timerID
  46. initialDelay:(NSTimeInterval)initialDelay
  47. backoffFactor:(double)backoffFactor
  48. maxDelay:(NSTimeInterval)maxDelay NS_DESIGNATED_INITIALIZER;
  49. - (instancetype)init NS_UNAVAILABLE;
  50. /**
  51. * Resets the backoff delay.
  52. *
  53. * The very next backoffAndRunBlock: will have no delay. If it is called again (i.e. due to an
  54. * error), initialDelay (plus jitter) will be used, and subsequent ones will increase according
  55. * to the backoffFactor.
  56. */
  57. - (void)reset;
  58. /**
  59. * Resets the backoff to the maximum delay (e.g. for use after a RESOURCE_EXHAUSTED error).
  60. */
  61. - (void)resetToMax;
  62. /**
  63. * Waits for currentDelay seconds, increases the delay and runs the specified block. If there was
  64. * a pending block waiting to be run already, it will be canceled.
  65. *
  66. * @param block The block to run.
  67. */
  68. - (void)backoffAndRunBlock:(void (^)(void))block;
  69. /** Cancels any pending backoff block scheduled via backoffAndRunBlock:. */
  70. - (void)cancel;
  71. @end
  72. NS_ASSUME_NONNULL_END