From c10e74f4fc9473fdf71030b6d96e5bdcfd384883 Mon Sep 17 00:00:00 2001 From: Arvid Norberg Date: Fri, 25 May 2007 21:00:35 +0000 Subject: [PATCH] improved control over the rate of connection attempts --- include/libtorrent/session_settings.hpp | 7 ++++- include/libtorrent/torrent.hpp | 2 +- src/session_impl.cpp | 41 ++++++++++++++++++++----- src/torrent.cpp | 4 +-- 4 files changed, 42 insertions(+), 12 deletions(-) diff --git a/include/libtorrent/session_settings.hpp b/include/libtorrent/session_settings.hpp index 363384a70..2fcc67a1a 100644 --- a/include/libtorrent/session_settings.hpp +++ b/include/libtorrent/session_settings.hpp @@ -99,6 +99,7 @@ namespace libtorrent , min_reconnect_time(60) , peer_connect_timeout(10) , ignore_limits_on_local_network(true) + , connection_speed(20) #ifndef TORRENT_DISABLE_DHT , use_dht_as_fallback(true) #endif @@ -204,9 +205,13 @@ namespace libtorrent int peer_connect_timeout; // if set to true, upload, download and unchoke limits - // are ignored for peers on the local network + // are ignored for peers on the local network. bool ignore_limits_on_local_network; + // the number of connection attempts that + // are made per second. + int connection_speed; + #ifndef TORRENT_DISABLE_DHT // while this is true, the dht will note be used unless the // tracker is online diff --git a/include/libtorrent/torrent.hpp b/include/libtorrent/torrent.hpp index 7194463e9..e6acbbef6 100755 --- a/include/libtorrent/torrent.hpp +++ b/include/libtorrent/torrent.hpp @@ -250,7 +250,7 @@ namespace libtorrent void remove_peer(peer_connection* p); bool want_more_peers() const; - void try_connect_peer(); + bool try_connect_peer(); peer_connection* connection_for(tcp::endpoint const& a) { diff --git a/src/session_impl.cpp b/src/session_impl.cpp index 2e2238465..007b83ca8 100755 --- a/src/session_impl.cpp +++ b/src/session_impl.cpp @@ -622,6 +622,8 @@ namespace libtorrent { namespace detail void session_impl::set_settings(session_settings const& s) { mutex_t::scoped_lock l(m_mutex); + assert(s.connection_speed > 0); + assert(s.file_pool_size > 0); m_settings = s; m_files.resize(m_settings.file_pool_size); // replace all occurances of '\n' with ' '. @@ -891,29 +893,52 @@ namespace libtorrent { namespace detail // let torrents connect to peers if they want to // if there are any torrents and any free slots + + // this loop will "hand out" max(connection_speed + // , half_open.free_slots()) to the torrents, in a + // round robin fashion, so that every torrent is + // equallt likely to connect to a peer + if (!m_torrents.empty() && m_half_open.free_slots()) { - torrent_map::iterator next_connect_torrent = m_torrents.begin(); + // this is the maximum number of connections we will + // attempt this tick + int max_connections = m_settings.connection_speed; + + torrent_map::iterator i = m_torrents.begin(); if (m_next_connect_torrent < int(m_torrents.size())) - std::advance(next_connect_torrent, m_next_connect_torrent); + std::advance(i, m_next_connect_torrent); else m_next_connect_torrent = 0; - torrent_map::iterator i = next_connect_torrent; - do + int steps_since_last_connect = 0; + int num_torrents = int(m_torrents.size()); + for (;;) { torrent& t = *i->second; if (t.want_more_peers()) - t.try_connect_peer(); + if (t.try_connect_peer()) + { + --max_connections; + steps_since_last_connect = 0; + } ++m_next_connect_torrent; - if (!m_half_open.free_slots()) break; + ++steps_since_last_connect; ++i; if (i == m_torrents.end()) { - assert(m_next_connect_torrent == int(m_torrents.size())); + assert(m_next_connect_torrent == num_torrents); i = m_torrents.begin(); m_next_connect_torrent = 0; } - } while (i != next_connect_torrent); + // if we have gone one whole loop without + // handing out a single connection, break + if (steps_since_last_connect > num_torrents) break; + // if there are no more free connection slots, abort + if (m_half_open.free_slots() == 0) break; + // if we should not make any more connections + // attempts this tick, abort + if (max_connections == 0) break; + } } // do the second_tick() on each connection diff --git a/src/torrent.cpp b/src/torrent.cpp index 408102d2e..e64dba39d 100755 --- a/src/torrent.cpp +++ b/src/torrent.cpp @@ -2484,10 +2484,10 @@ namespace libtorrent m_stat.second_tick(tick_interval); } - void torrent::try_connect_peer() + bool torrent::try_connect_peer() { assert(want_more_peers()); - m_policy->connect_one_peer(); + return m_policy->connect_one_peer(); } void torrent::distribute_resources(float tick_interval)