26 #ifndef _AMPS_RECONNECTDELAYSTRATEGYIMPL_HPP_ 27 #define _AMPS_RECONNECTDELAYSTRATEGYIMPL_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) newDelay = _maximumDelay;
178 *pCurrentDelay_ = (
unsigned int)newDelay;
180 unsigned int delay = (
unsigned int)newDelay;
181 unsigned int maxJitter = (
unsigned int)(_initialDelay * _jitter);
184 if (delay > _maximumDelay - maxJitter)
185 delay = (_maximumDelay - maxJitter > _initialDelay) ?
186 _maximumDelay - maxJitter : _initialDelay;
188 (
unsigned int)(_initialDelay * _jitter * (::rand()*1.0/RAND_MAX));
189 if (delay > _maximumDelay) delay = _maximumDelay;
192 if(_maximumRetryTime)
194 double remaining = 0.0;
195 if (_timer.checkAndGetRemaining(&remaining)) _throwError();
196 unsigned int remainingMillis = (
unsigned int)remaining+1U;
197 if (remainingMillis < delay)
198 delay = remainingMillis;
203 unsigned int _initialDelay;
204 unsigned int _maximumDelay;
205 double _backoffExponent;
207 unsigned int _maximumRetryTime;
208 typedef std::map<std::string, unsigned int> URIDelayMap;
209 typedef std::map<std::string, unsigned int>::iterator URIDelayMapIterator;
210 URIDelayMap _currentDelays;
234 : _duration(duration_),
253 double remaining = 0.0;
257 if(_triedURIs.empty())
262 else if (_timer.checkAndGetRemaining(&remaining))
264 throw ReconnectMaximumExceededException(
265 "The maximum time to attempt " 266 "connection to a server has been exceeded.");
270 if (_triedURIs.count(uri_) == 0)
272 _triedURIs.insert(uri_);
276 if (_maximum > 0 && remaining <= _duration)
278 throw ReconnectMaximumExceededException(
279 "The maximum time to attempt connection to a server " 280 "would be exceeded by another delay.");
299 unsigned int _duration;
300 unsigned int _maximum;
301 std::set<std::string> _triedURIs;
305 #endif // _AMPS_RECONNECTDELAYSTRATEGYIMPL_HPP_ FixedDelayStrategy(unsigned int duration_=200, unsigned maximum_=0)
Construct a FixedDelayStrategy with a given duration.
Definition: ReconnectDelayStrategyImpl.hpp:233
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:290
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:220
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:251
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:103