30 lines · cpp
1//===- llvm/Support/ExponentialBackoff.h ------------------------*- C++ -*-===//2//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-exception6//7//===----------------------------------------------------------------------===//8 9#include "llvm/Support/ExponentialBackoff.h"10#include <thread>11 12using namespace llvm;13 14bool ExponentialBackoff::waitForNextAttempt() {15 auto Now = std::chrono::steady_clock::now();16 if (Now >= EndTime)17 return false;18 19 duration CurMaxWait = std::min(MinWait * CurrentMultiplier, MaxWait);20 std::uniform_int_distribution<uint64_t> Dist(MinWait.count(),21 CurMaxWait.count());22 // Use random_device directly instead of a PRNG as uniform_int_distribution23 // 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);28 return true;29}30