all repos — mgba @ ff8f03ab74c20ce387dbbd20a57c317dfd225ab5

mGBA Game Boy Advance Emulator

src/third-party/discord-rpc/src/backoff.h (view raw)

 1#pragma once
 2
 3#include <algorithm>
 4#include <random>
 5#include <stdint.h>
 6#include <time.h>
 7
 8struct Backoff {
 9    int64_t minAmount;
10    int64_t maxAmount;
11    int64_t current;
12    int fails;
13    std::mt19937_64 randGenerator;
14    std::uniform_real_distribution<> randDistribution;
15
16    double rand01() { return randDistribution(randGenerator); }
17
18    Backoff(int64_t min, int64_t max)
19      : minAmount(min)
20      , maxAmount(max)
21      , current(min)
22      , fails(0)
23      , randGenerator((uint64_t)time(0))
24    {
25    }
26
27    void reset()
28    {
29        fails = 0;
30        current = minAmount;
31    }
32
33    int64_t nextDelay()
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};