26 #ifndef _AMPS_RECONNECTDELAYSTRATEGYIMPL_HPP_ 27 #define _AMPS_RECONNECTDELAYSTRATEGYIMPL_HPP_ 28 #include <amps/util.hpp> 64 const std::string& uri_) = 0;
69 virtual void reset(
void) = 0;
111 unsigned int initialDelay_ = 200,
112 unsigned int maximumDelay_ = 20 * 1000,
113 double backoffExponent_ = 2.0,
114 unsigned int maximumRetryTime_ = 0,
115 double jitter_ = 1.0)
116 : _initialDelay(initialDelay_),
117 _maximumDelay(maximumDelay_),
118 _backoffExponent(backoffExponent_),
120 _maximumRetryTime(maximumRetryTime_),
121 _timer(maximumRetryTime_)
125 ::srand((
unsigned int)amps_now());
131 _throwIfMaximumExceeded();
132 URIDelayMapIterator currentDelay = _currentDelays.find(uri_);
133 if (currentDelay == _currentDelays.end())
136 if (_maximumRetryTime != 0 && _currentDelays.empty())
141 _currentDelays[uri_] = 0;
145 return _currentDurationAndIncrease(&(currentDelay->second));
149 _currentDelays.clear();
155 void _throwError(
void)
157 throw ReconnectMaximumExceededException(
158 "The maximum time to attempt " 159 "connection to a server has been exceeded.");
162 void _throwIfMaximumExceeded(
void)
170 unsigned int _currentDurationAndIncrease(
unsigned int* pCurrentDelay_)
173 unsigned long long newDelay = (*pCurrentDelay_ == 0) ?
174 (
unsigned long long)_initialDelay :
175 (
unsigned long long)(*pCurrentDelay_ * _backoffExponent);
176 if (newDelay > _maximumDelay)
178 newDelay = _maximumDelay;
181 *pCurrentDelay_ = (
unsigned int)newDelay;
183 unsigned int delay = (
unsigned int)newDelay;
184 unsigned int maxJitter = (
unsigned int)(_initialDelay * _jitter);
187 if (delay > _maximumDelay - maxJitter)
188 delay = (_maximumDelay - maxJitter > _initialDelay) ?
189 _maximumDelay - maxJitter : _initialDelay;
191 (
unsigned int)(_initialDelay * _jitter * (::rand() * 1.0 / RAND_MAX));
192 if (delay > _maximumDelay)
194 delay = _maximumDelay;
198 if (_maximumRetryTime)
200 double remaining = 0.0;
201 if (_timer.checkAndGetRemaining(&remaining))
205 unsigned int remainingMillis = (
unsigned int)remaining + 1U;
206 if (remainingMillis < delay)
208 delay = remainingMillis;
214 unsigned int _initialDelay;
215 unsigned int _maximumDelay;
216 double _backoffExponent;
218 unsigned int _maximumRetryTime;
219 typedef std::map<std::string, unsigned int> URIDelayMap;
220 typedef std::map<std::string, unsigned int>::iterator URIDelayMapIterator;
221 URIDelayMap _currentDelays;
245 : _duration(duration_),
264 double remaining = 0.0;
268 if (_triedURIs.empty())
273 else if (_timer.checkAndGetRemaining(&remaining))
275 throw ReconnectMaximumExceededException(
276 "The maximum time to attempt " 277 "connection to a server has been exceeded.");
281 if (_triedURIs.count(uri_) == 0)
283 _triedURIs.insert(uri_);
287 if (_maximum > 0 && remaining <= _duration)
289 throw ReconnectMaximumExceededException(
290 "The maximum time to attempt connection to a server " 291 "would be exceeded by another delay.");
310 unsigned int _duration;
311 unsigned int _maximum;
312 std::set<std::string> _triedURIs;
316 #endif // _AMPS_RECONNECTDELAYSTRATEGYIMPL_HPP_ FixedDelayStrategy(unsigned int duration_=200, unsigned maximum_=0)
Construct a FixedDelayStrategy with a given duration.
Definition: ReconnectDelayStrategyImpl.hpp:244
ExponentialDelayStrategy is an implementation that exponentially "backs off" when reconnecting to the...
Definition: ReconnectDelayStrategyImpl.hpp:77
virtual unsigned int getConnectWaitDuration(const std::string &uri_)=0
Returns the time that the client should delay before connecting to the given server URI...
Base class for ReconnectDelayStrategy implementations.
Definition: ReconnectDelayStrategyImpl.hpp:46
void reset(void)
Reset the state of this reconnect delay.
Definition: ReconnectDelayStrategyImpl.hpp:301
unsigned int getConnectWaitDuration(const std::string &uri_)
Returns the time that the client should delay before connecting to the given server URI...
Definition: ReconnectDelayStrategyImpl.hpp:129
ExponentialDelayStrategy(unsigned int initialDelay_=200, unsigned int maximumDelay_=20 *1000, double backoffExponent_=2.0, unsigned int maximumRetryTime_=0, double jitter_=1.0)
Constructs an exponential delay strategy, the default strategy for HAClient.
Definition: ReconnectDelayStrategyImpl.hpp:110
FixedDelayStrategy is an implementation that delays for a fixed time period, as specified in the cons...
Definition: ReconnectDelayStrategyImpl.hpp:231
unsigned int getConnectWaitDuration(const std::string &uri_)
Returns the time that the client should delay before connecting to the given server URI...
Definition: ReconnectDelayStrategyImpl.hpp:262
virtual void reset(void)=0
Reset the state of this reconnect delay.
void reset(void)
Reset the state of this reconnect delay.
Definition: ReconnectDelayStrategyImpl.hpp:147
Definition: ampsplusplus.hpp:102