aboutsummaryrefslogtreecommitdiff
path: root/llvm/lib/Support/ExponentialBackoff.cpp
blob: 7e68cf67ad3857e8b3cb5eedd4bb49c3b9949684 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
//===- llvm/Support/ExponentialBackoff.h ------------------------*- C++ -*-===//
//
// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
// See https://llvm.org/LICENSE.txt for license information.
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
//
//===----------------------------------------------------------------------===//

#include "llvm/Support/ExponentialBackoff.h"
#include <thread>

using namespace llvm;

bool ExponentialBackoff::waitForNextAttempt() {
  auto Now = std::chrono::steady_clock::now();
  if (Now >= EndTime)
    return false;

  duration CurMaxWait = std::min(MinWait * CurrentMultiplier, MaxWait);
  std::uniform_int_distribution<uint64_t> Dist(MinWait.count(),
                                               CurMaxWait.count());
  // Use random_device directly instead of a PRNG as uniform_int_distribution
  // often only takes a few samples anyway.
  duration WaitDuration = std::min(duration(Dist(RandDev)), EndTime - Now);
  if (CurMaxWait < MaxWait)
    CurrentMultiplier *= 2;
  std::this_thread::sleep_for(WaitDuration);
  return true;
}