nanoMMO 0.97.8
Building an MMO: The Engineering Series
Loading...
Searching...
No Matches
backoff.cc
Go to the documentation of this file.
1#include "bot/backoff.h"
2
3#include <algorithm>
4#include <chrono>
5#include <random>
6
7namespace nanommo {
8
9std::chrono::milliseconds Backoff(int failed, std::mt19937& random) {
10 constexpr std::chrono::milliseconds kFirst{1000};
11 constexpr std::chrono::milliseconds kMost{30000};
12 // Past five doublings it's at its most already, and a shift that far
13 // would overflow.
14 const std::chrono::milliseconds wait =
15 std::min(kFirst * (1 << std::clamp(failed, 0, 5)), kMost);
16 std::uniform_int_distribution<std::chrono::milliseconds::rep> part(
17 wait.count() / 2, wait.count());
18 return std::chrono::milliseconds(part(random));
19}
20
21} // namespace nanommo
Definition chat.cc:9
std::chrono::milliseconds Backoff(int failed, std::mt19937 &random)
Definition backoff.cc:9