Back off on reconnect rate

This commit is contained in:
Chris Marsh
2017-07-18 11:10:39 -07:00
parent 7082a13d49
commit b947e6afe5
2 changed files with 59 additions and 1 deletions

41
src/backoff.h Normal file
View File

@ -0,0 +1,41 @@
#pragma once
#include <stdint.h>
#include <algorithm>
#include <random>
struct Backoff
{
int64_t minAmount;
int64_t maxAmount;
int64_t current;
int fails;
std::mt19937_64 randGenerator;
std::uniform_real_distribution<> randDistribution;
double rand01() {
return randDistribution(randGenerator);
}
Backoff(int64_t min, int64_t max)
: minAmount(min)
, maxAmount(max)
, current(min)
, fails(0)
{}
void reset()
{
fails = 0;
current = minAmount;
}
int64_t nextDelay()
{
++fails;
int64_t delay = (int64_t)((double)current * 2.0 * rand01());
current = std::min(current + delay, maxAmount);
return current;
}
};