Problem: relicensing forced to remove support for ZMQ_RECONNECT_IVL_MAX option

Solution: reimplement support for ZMQ_RECONNECT_IVL_MAX without knowledge of the previous code.
This commit is contained in:
Stéphane Valès 2023-06-06 11:12:03 +02:00
parent 1a921db728
commit de1bd5c8e7
3 changed files with 41 additions and 22 deletions

View File

@ -186,6 +186,7 @@ zmq::options_t::options_t () :
tcp_maxrt (0),
reconnect_stop (0),
reconnect_ivl (100),
reconnect_ivl_max (0),
backlog (100),
maxmsgsize (-1),
rcvtimeo (-1),
@ -400,11 +401,11 @@ int zmq::options_t::setsockopt (int option_,
break;
case ZMQ_RECONNECT_IVL_MAX:
#ifdef ZMQ_HAVE_WINDOWS
return WSAEOPNOTSUPP;
#else
return -EOPNOTSUPP;
#endif
if (is_int && value >= 0) {
reconnect_ivl_max = value;
return 0;
}
break;
case ZMQ_BACKLOG:
if (is_int && value >= 0) {
@ -1048,11 +1049,11 @@ int zmq::options_t::getsockopt (int option_,
break;
case ZMQ_RECONNECT_IVL_MAX:
#ifdef ZMQ_HAVE_WINDOWS
return WSAEOPNOTSUPP;
#else
return -EOPNOTSUPP;
#endif
if (is_int) {
*value = reconnect_ivl_max;
return 0;
}
break;
case ZMQ_BACKLOG:
if (is_int) {

View File

@ -100,6 +100,10 @@ struct options_t
// Default 100ms
int reconnect_ivl;
// Maximum interval between attempts to reconnect, in milliseconds.
// Default 0ms (meaning maximum interval is disabled)
int reconnect_ivl_max;
// Maximum backlog for pending connections.
int backlog;

View File

@ -30,7 +30,7 @@ zmq::stream_connecter_base_t::stream_connecter_base_t (
_socket (session_->get_socket ()),
_delayed_start (delayed_start_),
_reconnect_timer_started (false),
_current_reconnect_ivl (options.reconnect_ivl),
_current_reconnect_ivl (-1),
_session (session_)
{
zmq_assert (_addr);
@ -85,19 +85,33 @@ void zmq::stream_connecter_base_t::add_reconnect_timer ()
int zmq::stream_connecter_base_t::get_new_reconnect_ivl ()
{
// TODO should the random jitter be really based on the configured initial
// reconnect interval options.reconnect_ivl, or better on the
// _current_reconnect_ivl?
if (options.reconnect_ivl_max > 0) {
int candidate_interval = 0;
if (_current_reconnect_ivl == -1)
candidate_interval = options.reconnect_ivl;
else if (_current_reconnect_ivl > std::numeric_limits<int>::max () / 2)
candidate_interval = std::numeric_limits<int>::max ();
else
candidate_interval = _current_reconnect_ivl * 2;
// The new interval is the current interval + random value.
if (candidate_interval > options.reconnect_ivl_max)
_current_reconnect_ivl = options.reconnect_ivl_max;
else
_current_reconnect_ivl = candidate_interval;
return _current_reconnect_ivl;
} else {
if (_current_reconnect_ivl == -1)
_current_reconnect_ivl = options.reconnect_ivl;
// The new interval is the base interval + random value.
const int random_jitter = generate_random () % options.reconnect_ivl;
const int interval =
_current_reconnect_ivl < std::numeric_limits<int>::max () - random_jitter
_current_reconnect_ivl
< std::numeric_limits<int>::max () - random_jitter
? _current_reconnect_ivl + random_jitter
: std::numeric_limits<int>::max ();
return interval;
}
}
void zmq::stream_connecter_base_t::rm_handle ()
{