RetroArch
backoff.h
Go to the documentation of this file.
1 #pragma once
2 
3 #include <algorithm>
4 #include <random>
5 #include <stdint.h>
6 #include <time.h>
7 
8 struct Backoff {
12  int fails;
13  std::mt19937_64 randGenerator;
14  std::uniform_real_distribution<> randDistribution;
15 
16  double rand01() { return randDistribution(randGenerator); }
17 
19  : minAmount(min)
20  , maxAmount(max)
21  , current(min)
22  , fails(0)
24  {
25  }
26 
27  void reset()
28  {
29  fails = 0;
31  }
32 
34  {
35  ++fails;
36  int64_t delay = (int64_t)((double)current * 2.0 * rand01());
37  current = std::min(current + delay, maxAmount);
38  return current;
39  }
40 };
void reset()
Definition: backoff.h:27
int64_t current
Definition: backoff.h:11
Definition: backoff.h:8
int fails
Definition: backoff.h:12
Backoff(int64_t min, int64_t max)
Definition: backoff.h:18
int64_t maxAmount
Definition: backoff.h:10
time_t time(time_t *timer)
int64_t minAmount
Definition: backoff.h:9
std::mt19937_64 randGenerator
Definition: backoff.h:13
signed __int64 int64_t
Definition: stdint.h:135
double rand01()
Definition: backoff.h:16
int64_t nextDelay()
Definition: backoff.h:33
std::uniform_real_distribution randDistribution
Definition: backoff.h:14
unsigned __int64 uint64_t
Definition: stdint.h:136