diff options
author | Dimitry Andric <dim@FreeBSD.org> | 2024-07-27 23:34:35 +0000 |
---|---|---|
committer | Dimitry Andric <dim@FreeBSD.org> | 2024-10-23 18:26:01 +0000 |
commit | 0fca6ea1d4eea4c934cfff25ac9ee8ad6fe95583 (patch) | |
tree | 6cf5ab1f05330c6773b1f3f64799d56a9c7a1faa /contrib/llvm-project/llvm/lib/Support/ExponentialBackoff.cpp | |
parent | 6b9f7133aba44189d9625c352bc2c2a59baf18ef (diff) | |
parent | ac9a064cb179f3425b310fa2847f8764ac970a4d (diff) |
Diffstat (limited to 'contrib/llvm-project/llvm/lib/Support/ExponentialBackoff.cpp')
-rw-r--r-- | contrib/llvm-project/llvm/lib/Support/ExponentialBackoff.cpp | 29 |
1 files changed, 29 insertions, 0 deletions
diff --git a/contrib/llvm-project/llvm/lib/Support/ExponentialBackoff.cpp b/contrib/llvm-project/llvm/lib/Support/ExponentialBackoff.cpp new file mode 100644 index 000000000000..7e68cf67ad38 --- /dev/null +++ b/contrib/llvm-project/llvm/lib/Support/ExponentialBackoff.cpp @@ -0,0 +1,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; +} |