Back to home page

EIC code displayed by LXR

 
 

    


File indexing completed on 2026-05-10 08:44:30

0001 //===- llvm/Support/ExponentialBackoff.h ------------------------*- C++ -*-===//
0002 //
0003 // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
0004 // See https://llvm.org/LICENSE.txt for license information.
0005 // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
0006 //
0007 //===----------------------------------------------------------------------===//
0008 //
0009 // This file defines a helper class for implementing exponential backoff.
0010 //
0011 //===----------------------------------------------------------------------===//
0012 #ifndef LLVM_EXPONENTIALBACKOFF_H
0013 #define LLVM_EXPONENTIALBACKOFF_H
0014 
0015 #include "llvm/ADT/STLExtras.h"
0016 #include "llvm/Support/Error.h"
0017 #include <chrono>
0018 #include <random>
0019 
0020 namespace llvm {
0021 
0022 /// A class to help implement exponential backoff.
0023 ///
0024 /// Example usage:
0025 /// \code
0026 ///   ExponentialBackoff Backoff(10s);
0027 ///   do {
0028 ///     if (tryToDoSomething())
0029 ///       return ItWorked;
0030 ///   } while (Backoff.waitForNextAttempt());
0031 ///   return Timeout;
0032 /// \endcode
0033 class ExponentialBackoff {
0034 public:
0035   using duration = std::chrono::steady_clock::duration;
0036   using time_point = std::chrono::steady_clock::time_point;
0037 
0038   /// \param Timeout the maximum wall time this should run for starting when
0039   ///        this object is constructed.
0040   /// \param MinWait the minimum amount of time `waitForNextAttempt` will sleep
0041   ///        for.
0042   /// \param MaxWait the maximum amount of time `waitForNextAttempt` will sleep
0043   ///        for.
0044   ExponentialBackoff(duration Timeout,
0045                      duration MinWait = std::chrono::milliseconds(10),
0046                      duration MaxWait = std::chrono::milliseconds(500))
0047       : MinWait(MinWait), MaxWait(MaxWait),
0048         EndTime(std::chrono::steady_clock::now() + Timeout) {}
0049 
0050   /// Blocks while waiting for the next attempt.
0051   /// \returns true if you should try again, false if the timeout has been
0052   /// reached.
0053   bool waitForNextAttempt();
0054 
0055 private:
0056   duration MinWait;
0057   duration MaxWait;
0058   time_point EndTime;
0059   std::random_device RandDev;
0060   int64_t CurrentMultiplier = 1;
0061 };
0062 
0063 } // end namespace llvm
0064 
0065 #endif // LLVM_EXPONENTIALBACKOFF_H