improved control over the rate of connection attempts

This commit is contained in:
Arvid Norberg
2007-05-25 21:00:35 +00:00
parent bb71832f5f
commit c10e74f4fc
4 changed files with 42 additions and 12 deletions

View File

@@ -99,6 +99,7 @@ namespace libtorrent
, min_reconnect_time(60) , min_reconnect_time(60)
, peer_connect_timeout(10) , peer_connect_timeout(10)
, ignore_limits_on_local_network(true) , ignore_limits_on_local_network(true)
, connection_speed(20)
#ifndef TORRENT_DISABLE_DHT #ifndef TORRENT_DISABLE_DHT
, use_dht_as_fallback(true) , use_dht_as_fallback(true)
#endif #endif
@@ -204,9 +205,13 @@ namespace libtorrent
int peer_connect_timeout; int peer_connect_timeout;
// if set to true, upload, download and unchoke limits // 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; bool ignore_limits_on_local_network;
// the number of connection attempts that
// are made per second.
int connection_speed;
#ifndef TORRENT_DISABLE_DHT #ifndef TORRENT_DISABLE_DHT
// while this is true, the dht will note be used unless the // while this is true, the dht will note be used unless the
// tracker is online // tracker is online

View File

@@ -250,7 +250,7 @@ namespace libtorrent
void remove_peer(peer_connection* p); void remove_peer(peer_connection* p);
bool want_more_peers() const; bool want_more_peers() const;
void try_connect_peer(); bool try_connect_peer();
peer_connection* connection_for(tcp::endpoint const& a) peer_connection* connection_for(tcp::endpoint const& a)
{ {

View File

@@ -622,6 +622,8 @@ namespace libtorrent { namespace detail
void session_impl::set_settings(session_settings const& s) void session_impl::set_settings(session_settings const& s)
{ {
mutex_t::scoped_lock l(m_mutex); mutex_t::scoped_lock l(m_mutex);
assert(s.connection_speed > 0);
assert(s.file_pool_size > 0);
m_settings = s; m_settings = s;
m_files.resize(m_settings.file_pool_size); m_files.resize(m_settings.file_pool_size);
// replace all occurances of '\n' with ' '. // replace all occurances of '\n' with ' '.
@@ -891,29 +893,52 @@ namespace libtorrent { namespace detail
// let torrents connect to peers if they want to // let torrents connect to peers if they want to
// if there are any torrents and any free slots // 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()) 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())) 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 else
m_next_connect_torrent = 0; m_next_connect_torrent = 0;
torrent_map::iterator i = next_connect_torrent; int steps_since_last_connect = 0;
do int num_torrents = int(m_torrents.size());
for (;;)
{ {
torrent& t = *i->second; torrent& t = *i->second;
if (t.want_more_peers()) 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; ++m_next_connect_torrent;
if (!m_half_open.free_slots()) break; ++steps_since_last_connect;
++i; ++i;
if (i == m_torrents.end()) 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(); i = m_torrents.begin();
m_next_connect_torrent = 0; 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 // do the second_tick() on each connection

View File

@@ -2484,10 +2484,10 @@ namespace libtorrent
m_stat.second_tick(tick_interval); m_stat.second_tick(tick_interval);
} }
void torrent::try_connect_peer() bool torrent::try_connect_peer()
{ {
assert(want_more_peers()); assert(want_more_peers());
m_policy->connect_one_peer(); return m_policy->connect_one_peer();
} }
void torrent::distribute_resources(float tick_interval) void torrent::distribute_resources(float tick_interval)