From 9aa09d384cd548c44088ea909b763a563394b046 Mon Sep 17 00:00:00 2001 From: Arvid Norberg Date: Wed, 16 Feb 2011 03:11:55 +0000 Subject: [PATCH] added smooth_connect and turned it on by default --- docs/manual.rst | 7 +++++++ include/libtorrent/session_settings.hpp | 5 +++++ src/session_impl.cpp | 8 ++++++++ 3 files changed, 20 insertions(+) diff --git a/docs/manual.rst b/docs/manual.rst index 2885bbf9a..495ac4f51 100644 --- a/docs/manual.rst +++ b/docs/manual.rst @@ -4304,6 +4304,7 @@ session_settings bool no_connect_privileged_ports; int alert_queue_size; int max_metadata_size; + bool smooth_connects; }; ``version`` is automatically set to the libtorrent version you're using @@ -5137,6 +5138,12 @@ defaults to 1000. ``max_metadata_size`` is the maximum allowed size (in bytes) to be received by the metadata extension, i.e. magnet links. It defaults to 1 MiB. +``smooth_connects`` is true by default, which means the number of connection +attempts per second may be limited to below the ``connection_speed``, in case +we're close to bump up against the limit of number of connections. The intention +of this setting is to more evenly distribute our connection attempts over time, +instead of attempting to connectin in batches, and timing them out in batches. + pe_settings =========== diff --git a/include/libtorrent/session_settings.hpp b/include/libtorrent/session_settings.hpp index 91149335d..c422a9dba 100644 --- a/include/libtorrent/session_settings.hpp +++ b/include/libtorrent/session_settings.hpp @@ -263,6 +263,7 @@ namespace libtorrent , no_connect_privileged_ports(true) , alert_queue_size(1000) , max_metadata_size(1024*1024) + , smooth_connects(true) {} // libtorrent version. Used for forward binary compatibility @@ -1046,6 +1047,10 @@ namespace libtorrent // the max allowed size for metadata received by the // ut_metadata extension (i.e. magnet links) int max_metadata_size; + + // attempt to smooth out connects to avoid getting spikes in + // opening connections and timing out connections + bool smooth_connects; }; #ifndef TORRENT_DISABLE_DHT diff --git a/src/session_impl.cpp b/src/session_impl.cpp index dd7eae569..bfa1fe628 100644 --- a/src/session_impl.cpp +++ b/src/session_impl.cpp @@ -358,6 +358,7 @@ namespace aux { TORRENT_SETTING(boolean, no_connect_privileged_ports) TORRENT_SETTING(integer, alert_queue_size) TORRENT_SETTING(integer, max_metadata_size) + TORRENT_SETTING(integer, smooth_connects) }; #undef TORRENT_SETTING @@ -3020,6 +3021,13 @@ namespace aux { } } + // this logic is here to smooth out the number of new connection + // attempts over time, to prevent connecting a large number of + // sockets, wait 10 seconds, and then try again + int limit = (std::min)(m_settings.connections_limit - num_connections(), free_slots); + if (m_settings.smooth_connects && max_connections > (limit+1) / 2) + max_connections = (limit+1) / 2; + if (!m_torrents.empty() && free_slots > -m_half_open.limit() && num_connections() < m_settings.connections_limit