FSTExponentialBackoff.h 2.9 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879
  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. @class FSTDispatchQueue;
  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. * Creates and returns 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 initialDelay The initial delay (used as the base delay on the first retry attempt).
  36. * Note that jitter will still be applied, so the actual delay could be as little as
  37. * 0.5*initialDelay.
  38. * @param backoffFactor The multiplier to use to determine the extended base delay after each
  39. * attempt.
  40. * @param maxDelay The maximum base delay after which no further backoff is performed. Note that
  41. * jitter will still be applied, so the actual delay could be as much as 1.5*maxDelay.
  42. */
  43. + (instancetype)exponentialBackoffWithDispatchQueue:(FSTDispatchQueue *)dispatchQueue
  44. initialDelay:(NSTimeInterval)initialDelay
  45. backoffFactor:(double)backoffFactor
  46. maxDelay:(NSTimeInterval)maxDelay;
  47. - (instancetype)init
  48. __attribute__((unavailable("Use exponentialBackoffWithDispatchQueue constructor method.")));
  49. /**
  50. * Resets the backoff delay.
  51. *
  52. * The very next backoffAndRunBlock: will have no delay. If it is called again (i.e. due to an
  53. * error), initialDelay (plus jitter) will be used, and subsequent ones will increase according
  54. * to the backoffFactor.
  55. */
  56. - (void)reset;
  57. /**
  58. * Resets the backoff to the maximum delay (e.g. for use after a RESOURCE_EXHAUSTED error).
  59. */
  60. - (void)resetToMax;
  61. /**
  62. * Waits for currentDelay seconds, increases the delay and runs the specified block.
  63. *
  64. * @param block The block to run.
  65. */
  66. - (void)backoffAndRunBlock:(void (^)(void))block;
  67. @end
  68. NS_ASSUME_NONNULL_END