Open Lighting Architecture  0.9.2
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
Backoff.h
1 /*
2  * This library is free software; you can redistribute it and/or
3  * modify it under the terms of the GNU Lesser General Public
4  * License as published by the Free Software Foundation; either
5  * version 2.1 of the License, or (at your option) any later version.
6  *
7  * This library is distributed in the hope that it will be useful,
8  * but WITHOUT ANY WARRANTY; without even the implied warranty of
9  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
10  * Lesser General Public License for more details.
11  *
12  * You should have received a copy of the GNU Lesser General Public
13  * License along with this library; if not, write to the Free Software
14  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
15  *
16  * Backoff.h
17  * Copyright (C) 2013 Simon Newton
18  */
19 
20 #ifndef INCLUDE_OLA_UTIL_BACKOFF_H_
21 #define INCLUDE_OLA_UTIL_BACKOFF_H_
22 
23 #include <math.h>
24 #include <ola/Clock.h>
25 #include <memory>
26 
27 namespace ola {
28 
34  public:
35  BackOffPolicy() {}
36  virtual ~BackOffPolicy() {}
37 
42  virtual TimeInterval BackOffTime(unsigned int failed_attempts) const = 0;
43 };
44 
45 
51  public:
52  explicit ConstantBackoffPolicy(const TimeInterval &duration)
53  : m_duration(duration) {
54  }
55 
56  TimeInterval BackOffTime(unsigned int) const {
57  return m_duration;
58  }
59 
60  private:
61  const TimeInterval m_duration;
62 };
63 
64 
72  public:
73  LinearBackoffPolicy(const TimeInterval &duration, const TimeInterval &max)
74  : m_duration(duration),
75  m_max(max) {
76  }
77 
78  TimeInterval BackOffTime(unsigned int failed_attempts) const {
79  TimeInterval interval = m_duration * failed_attempts;
80  if (interval > m_max)
81  interval = m_max;
82  return interval;
83  }
84 
85  private:
86  const TimeInterval m_duration;
87  const TimeInterval m_max;
88 };
89 
90 
97  public:
99  const TimeInterval &max)
100  : m_initial(initial),
101  m_max(max) {
102  }
103 
104  TimeInterval BackOffTime(unsigned int failed_attempts) const {
105  TimeInterval interval = (
106  m_initial * static_cast<int>(::pow(2, failed_attempts - 1)));
107  if (interval > m_max)
108  interval = m_max;
109  return interval;
110  }
111 
112  private:
113  const TimeInterval m_initial;
114  const TimeInterval m_max;
115 };
116 
117 
118 // TODO(simon): add an ExponentialJitterBackoffPolicy
119 
120 // Generates backoff times.
122  public:
123  explicit BackoffGenerator(const BackOffPolicy *policy)
124  : m_policy(policy),
125  m_failures(0) {
126  }
127 
128  TimeInterval Next() {
129  return m_policy->BackOffTime(++m_failures);
130  }
131 
132  void Reset() {
133  m_failures = 0;
134  }
135 
136  private:
137  std::auto_ptr<const BackOffPolicy> m_policy;
138  unsigned int m_failures;
139 };
140 } // namespace ola
141 #endif // INCLUDE_OLA_UTIL_BACKOFF_H_