mirror of
https://github.com/zeromq/libzmq.git
synced 2025-07-01 08:23:32 +02:00
Scalability improvements for large amounts of connections
Add signaler_sndbuf_size option to config.hpp which allows the user to increase the size of the send buffer used by the signalling socketpair. Implement random backoff for reconnection attempts using a primitive pseudo-random generation to prevent reconnection storms. Add wait_before_connect option to config.hpp to allow the user to enable random delay even on initial connect. Default is false for low latency. Signed-off-by: Martin Lucina <mato@kotelna.sk>
This commit is contained in:
parent
7a685b0f88
commit
f90c8d957e
@ -39,6 +39,10 @@ namespace zmq
|
|||||||
// using a single system call.
|
// using a single system call.
|
||||||
signal_buffer_size = 8,
|
signal_buffer_size = 8,
|
||||||
|
|
||||||
|
// Socketpair send buffer size used by signaler. The default value of
|
||||||
|
// zero means leave it at the system default.
|
||||||
|
signaler_sndbuf_size = 0,
|
||||||
|
|
||||||
// Determines how often does socket poll for new commands when it
|
// Determines how often does socket poll for new commands when it
|
||||||
// still has unprocessed messages to handle. Thus, if it is set to 100,
|
// still has unprocessed messages to handle. Thus, if it is set to 100,
|
||||||
// socket will process 100 inbound messages before doing the poll.
|
// socket will process 100 inbound messages before doing the poll.
|
||||||
@ -72,6 +76,9 @@ namespace zmq
|
|||||||
// How long to wait (milliseconds) till reattempting to connect.
|
// How long to wait (milliseconds) till reattempting to connect.
|
||||||
reconnect_period = 100,
|
reconnect_period = 100,
|
||||||
|
|
||||||
|
// Should initial connection attempts be delayed?
|
||||||
|
wait_before_connect = false,
|
||||||
|
|
||||||
// Maximal delay to process command in API thread (in CPU ticks).
|
// Maximal delay to process command in API thread (in CPU ticks).
|
||||||
// 3,000,000 ticks equals to 1 - 2 milliseconds on current CPUs.
|
// 3,000,000 ticks equals to 1 - 2 milliseconds on current CPUs.
|
||||||
// Note that delay is only applied when there is continuous stream of
|
// Note that delay is only applied when there is continuous stream of
|
||||||
@ -87,7 +94,7 @@ namespace zmq
|
|||||||
|
|
||||||
// Maximal number of non-accepted connections that can be held by
|
// Maximal number of non-accepted connections that can be held by
|
||||||
// TCP listener object.
|
// TCP listener object.
|
||||||
tcp_connection_backlog = 10,
|
tcp_connection_backlog = 100,
|
||||||
|
|
||||||
// Maximum transport data unit size for PGM (TPDU).
|
// Maximum transport data unit size for PGM (TPDU).
|
||||||
pgm_max_tpdu = 1500
|
pgm_max_tpdu = 1500
|
||||||
|
@ -75,6 +75,15 @@ zmq::signaler_t::signaler_t ()
|
|||||||
w = WSASocket (AF_INET, SOCK_STREAM, 0, NULL, 0, 0);
|
w = WSASocket (AF_INET, SOCK_STREAM, 0, NULL, 0, 0);
|
||||||
wsa_assert (w != INVALID_SOCKET);
|
wsa_assert (w != INVALID_SOCKET);
|
||||||
|
|
||||||
|
// Increase signaler SNDBUF if requested in config.hpp.
|
||||||
|
if (signaler_sndbuf_size) {
|
||||||
|
int sndbuf = signaler_sndbuf_size;
|
||||||
|
socklen_t sndbuf_size = sizeof sndbuf;
|
||||||
|
rc = setsockopt (w, SOL_SOCKET, SO_SNDBUF, (const char *)&sndbuf,
|
||||||
|
sndbuf_size);
|
||||||
|
errno_assert (rc == 0);
|
||||||
|
}
|
||||||
|
|
||||||
// Connect to the remote peer.
|
// Connect to the remote peer.
|
||||||
rc = connect (w, (sockaddr *) &addr, sizeof (addr));
|
rc = connect (w, (sockaddr *) &addr, sizeof (addr));
|
||||||
wsa_assert (rc != SOCKET_ERROR);
|
wsa_assert (rc != SOCKET_ERROR);
|
||||||
@ -170,6 +179,14 @@ zmq::signaler_t::signaler_t ()
|
|||||||
flags = 0;
|
flags = 0;
|
||||||
rc = fcntl (r, F_SETFL, flags | O_NONBLOCK);
|
rc = fcntl (r, F_SETFL, flags | O_NONBLOCK);
|
||||||
errno_assert (rc != -1);
|
errno_assert (rc != -1);
|
||||||
|
|
||||||
|
// Increase signaler SNDBUF if requested in config.hpp.
|
||||||
|
if (signaler_sndbuf_size) {
|
||||||
|
int sndbuf = signaler_sndbuf_size;
|
||||||
|
socklen_t sndbuf_size = sizeof sndbuf;
|
||||||
|
rc = setsockopt (w, SOL_SOCKET, SO_SNDBUF, &sndbuf, sndbuf_size);
|
||||||
|
errno_assert (rc == 0);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
zmq::signaler_t::~signaler_t ()
|
zmq::signaler_t::~signaler_t ()
|
||||||
@ -248,6 +265,14 @@ zmq::signaler_t::signaler_t ()
|
|||||||
errno_assert (rc == 0);
|
errno_assert (rc == 0);
|
||||||
w = sv [0];
|
w = sv [0];
|
||||||
r = sv [1];
|
r = sv [1];
|
||||||
|
|
||||||
|
// Increase signaler SNDBUF if requested in config.hpp.
|
||||||
|
if (signaler_sndbuf_size) {
|
||||||
|
int sndbuf = signaler_sndbuf_size;
|
||||||
|
socklen_t sndbuf_size = sizeof sndbuf;
|
||||||
|
rc = setsockopt (w, SOL_SOCKET, SO_SNDBUF, &sndbuf, sndbuf_size);
|
||||||
|
errno_assert (rc == 0);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
zmq::signaler_t::~signaler_t ()
|
zmq::signaler_t::~signaler_t ()
|
||||||
|
@ -19,6 +19,13 @@
|
|||||||
|
|
||||||
#include <new>
|
#include <new>
|
||||||
|
|
||||||
|
#if defined ZMQ_HAVE_WINDOWS
|
||||||
|
#include "windows.hpp"
|
||||||
|
#else
|
||||||
|
#include <sys/types.h>
|
||||||
|
#include <unistd.h>
|
||||||
|
#endif
|
||||||
|
|
||||||
#include "zmq_connecter.hpp"
|
#include "zmq_connecter.hpp"
|
||||||
#include "zmq_engine.hpp"
|
#include "zmq_engine.hpp"
|
||||||
#include "zmq_init.hpp"
|
#include "zmq_init.hpp"
|
||||||
@ -31,7 +38,7 @@ zmq::zmq_connecter_t::zmq_connecter_t (class io_thread_t *io_thread_,
|
|||||||
own_t (io_thread_),
|
own_t (io_thread_),
|
||||||
io_object_t (io_thread_),
|
io_object_t (io_thread_),
|
||||||
handle_valid (false),
|
handle_valid (false),
|
||||||
wait (false),
|
wait (wait_before_connect),
|
||||||
session (session_),
|
session (session_),
|
||||||
options (options_)
|
options (options_)
|
||||||
{
|
{
|
||||||
@ -47,10 +54,20 @@ zmq::zmq_connecter_t::~zmq_connecter_t ()
|
|||||||
rm_fd (handle);
|
rm_fd (handle);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
int zmq::zmq_connecter_t::get_reconnect_period ()
|
||||||
|
{
|
||||||
|
#if defined ZMQ_HAVE_WINDOWS
|
||||||
|
return (reconnect_period + (((int)GetCurrentProcessId () * 13)
|
||||||
|
% reconnect_period));
|
||||||
|
#else
|
||||||
|
return (reconnect_period + (((int)getpid () * 13) % reconnect_period));
|
||||||
|
#endif
|
||||||
|
}
|
||||||
|
|
||||||
void zmq::zmq_connecter_t::process_plug ()
|
void zmq::zmq_connecter_t::process_plug ()
|
||||||
{
|
{
|
||||||
if (wait)
|
if (wait)
|
||||||
add_timer (reconnect_period, reconnect_timer_id);
|
add_timer (get_reconnect_period (), reconnect_timer_id);
|
||||||
else
|
else
|
||||||
start_connecting ();
|
start_connecting ();
|
||||||
}
|
}
|
||||||
@ -73,7 +90,7 @@ void zmq::zmq_connecter_t::out_event ()
|
|||||||
if (fd == retired_fd) {
|
if (fd == retired_fd) {
|
||||||
tcp_connecter.close ();
|
tcp_connecter.close ();
|
||||||
wait = true;
|
wait = true;
|
||||||
add_timer (reconnect_period, reconnect_timer_id);
|
add_timer (get_reconnect_period (), reconnect_timer_id);
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -122,5 +139,5 @@ void zmq::zmq_connecter_t::start_connecting ()
|
|||||||
|
|
||||||
// Handle any other error condition by eventual reconnect.
|
// Handle any other error condition by eventual reconnect.
|
||||||
wait = true;
|
wait = true;
|
||||||
add_timer (reconnect_period, reconnect_timer_id);
|
add_timer (get_reconnect_period (), reconnect_timer_id);
|
||||||
}
|
}
|
||||||
|
@ -56,6 +56,9 @@ namespace zmq
|
|||||||
// Internal function to start the actual connection establishment.
|
// Internal function to start the actual connection establishment.
|
||||||
void start_connecting ();
|
void start_connecting ();
|
||||||
|
|
||||||
|
// Internal function to return the reconnect backoff delay.
|
||||||
|
int get_reconnect_period ();
|
||||||
|
|
||||||
// Actual connecting socket.
|
// Actual connecting socket.
|
||||||
tcp_connecter_t tcp_connecter;
|
tcp_connecter_t tcp_connecter;
|
||||||
|
|
||||||
|
Loading…
x
Reference in New Issue
Block a user