1 //===- llvm/Support/ExponentialBackoff.h ------------------------*- C++ -*-===//
3 // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4 // See https://llvm.org/LICENSE.txt for license information.
5 // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
7 //===----------------------------------------------------------------------===//
9 #include "llvm/Support/ExponentialBackoff.h"
14 bool ExponentialBackoff::waitForNextAttempt() {
15 auto Now
= std::chrono::steady_clock::now();
19 duration CurMaxWait
= std::min(MinWait
* CurrentMultiplier
, MaxWait
);
20 std::uniform_int_distribution
<uint64_t> Dist(MinWait
.count(),
22 // Use random_device directly instead of a PRNG as uniform_int_distribution
23 // often only takes a few samples anyway.
24 duration WaitDuration
= std::min(duration(Dist(RandDev
)), EndTime
- Now
);
25 if (CurMaxWait
< MaxWait
)
26 CurrentMultiplier
*= 2;
27 std::this_thread::sleep_for(WaitDuration
);