xref: /src/contrib/llvm-project/llvm/lib/Support/ExponentialBackoff.cpp (revision 0fca6ea1d4eea4c934cfff25ac9ee8ad6fe95583)
1ac9a064cSDimitry Andric //===- llvm/Support/ExponentialBackoff.h ------------------------*- C++ -*-===//
2ac9a064cSDimitry Andric //
3ac9a064cSDimitry Andric // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4ac9a064cSDimitry Andric // See https://llvm.org/LICENSE.txt for license information.
5ac9a064cSDimitry Andric // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6ac9a064cSDimitry Andric //
7ac9a064cSDimitry Andric //===----------------------------------------------------------------------===//
8ac9a064cSDimitry Andric 
9ac9a064cSDimitry Andric #include "llvm/Support/ExponentialBackoff.h"
10ac9a064cSDimitry Andric #include <thread>
11ac9a064cSDimitry Andric 
12ac9a064cSDimitry Andric using namespace llvm;
13ac9a064cSDimitry Andric 
waitForNextAttempt()14ac9a064cSDimitry Andric bool ExponentialBackoff::waitForNextAttempt() {
15ac9a064cSDimitry Andric   auto Now = std::chrono::steady_clock::now();
16ac9a064cSDimitry Andric   if (Now >= EndTime)
17ac9a064cSDimitry Andric     return false;
18ac9a064cSDimitry Andric 
19ac9a064cSDimitry Andric   duration CurMaxWait = std::min(MinWait * CurrentMultiplier, MaxWait);
20ac9a064cSDimitry Andric   std::uniform_int_distribution<uint64_t> Dist(MinWait.count(),
21ac9a064cSDimitry Andric                                                CurMaxWait.count());
22ac9a064cSDimitry Andric   // Use random_device directly instead of a PRNG as uniform_int_distribution
23ac9a064cSDimitry Andric   // often only takes a few samples anyway.
24ac9a064cSDimitry Andric   duration WaitDuration = std::min(duration(Dist(RandDev)), EndTime - Now);
25ac9a064cSDimitry Andric   if (CurMaxWait < MaxWait)
26ac9a064cSDimitry Andric     CurrentMultiplier *= 2;
27ac9a064cSDimitry Andric   std::this_thread::sleep_for(WaitDuration);
28ac9a064cSDimitry Andric   return true;
29ac9a064cSDimitry Andric }
30