/usr/include/ola/util/Backoff.h is in libola-dev 0.9.8-1.
This file is owned by root:root, with mode 0o644.
The actual contents of the file can be viewed below.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 | /*
* This library is free software; you can redistribute it and/or
* modify it under the terms of the GNU Lesser General Public
* License as published by the Free Software Foundation; either
* version 2.1 of the License, or (at your option) any later version.
*
* This library is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public
* License along with this library; if not, write to the Free Software
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
*
* Backoff.h
* Copyright (C) 2013 Simon Newton
*/
#ifndef INCLUDE_OLA_UTIL_BACKOFF_H_
#define INCLUDE_OLA_UTIL_BACKOFF_H_
#include <math.h>
#include <ola/Clock.h>
#include <memory>
namespace ola {
/**
* A backoff policy will calculate how long to wait before retrying an event
* given the number of previous failed attempts.
*/
class BackOffPolicy {
public:
BackOffPolicy() {}
virtual ~BackOffPolicy() {}
/**
* @brief Calculate the backoff time
* @param failed_attempts is the number of unsuccessfull connection
* attempts since the last successful connection.
* @return how long to wait before the next attempt
*/
virtual TimeInterval BackOffTime(unsigned int failed_attempts) const = 0;
};
/**
* Constant time back off policy. For a duration of 1s we'd produce.
* 1, 1, 1, 1, 1, ...
*/
class ConstantBackoffPolicy: public BackOffPolicy {
public:
explicit ConstantBackoffPolicy(const TimeInterval &duration)
: m_duration(duration) {
}
TimeInterval BackOffTime(unsigned int) const {
return m_duration;
}
private:
const TimeInterval m_duration;
};
/**
* A backoff policy which is:
* t = failed_attempts * duration
* For a duration of 1 and a max of 5 we'd produce:
* 0, 1, 2, 3, 4, 5, 5, 5, ...
*/
class LinearBackoffPolicy: public BackOffPolicy {
public:
LinearBackoffPolicy(const TimeInterval &duration, const TimeInterval &max)
: m_duration(duration),
m_max(max) {
}
TimeInterval BackOffTime(unsigned int failed_attempts) const {
TimeInterval interval = m_duration * failed_attempts;
if (interval > m_max)
interval = m_max;
return interval;
}
private:
const TimeInterval m_duration;
const TimeInterval m_max;
};
/**
* An exponential backoff policy.
* For an initial value of 1 and a max of 20 we'd produce:
* 0, 1, 2, 4, 8, 16, 20, 20, ...
*/
class ExponentialBackoffPolicy: public BackOffPolicy {
public:
ExponentialBackoffPolicy(const TimeInterval &initial,
const TimeInterval &max)
: m_initial(initial),
m_max(max) {
}
TimeInterval BackOffTime(unsigned int failed_attempts) const {
TimeInterval interval = (
m_initial * static_cast<int>(::pow(2, failed_attempts - 1)));
if (interval > m_max)
interval = m_max;
return interval;
}
private:
const TimeInterval m_initial;
const TimeInterval m_max;
};
// TODO(simon): add an ExponentialJitterBackoffPolicy
// Generates backoff times.
class BackoffGenerator {
public:
explicit BackoffGenerator(const BackOffPolicy *policy)
: m_policy(policy),
m_failures(0) {
}
TimeInterval Next() {
return m_policy->BackOffTime(++m_failures);
}
void Reset() {
m_failures = 0;
}
private:
std::auto_ptr<const BackOffPolicy> m_policy;
unsigned int m_failures;
};
} // namespace ola
#endif // INCLUDE_OLA_UTIL_BACKOFF_H_
|