diff --git a/ChangeLog b/ChangeLog index 2cf8a9902..c9ceb8cf6 100644 --- a/ChangeLog +++ b/ChangeLog @@ -1,3 +1,4 @@ + * optimized swarm startup time (shaved off about 1-2 seconds) * support DHT name lookup * optimized memory usage of torrent_info and file_storage, forcing some API changes around file_storage and file_entry diff --git a/docs/manual.rst b/docs/manual.rst index 3dc64d1d4..4b7536b1f 100644 --- a/docs/manual.rst +++ b/docs/manual.rst @@ -4008,6 +4008,8 @@ session_settings int listen_queue_size; bool announce_double_nat; + + int torrent_connect_boost; }; ``version`` is automatically set to the libtorrent version you're using @@ -4810,6 +4812,12 @@ if ``announce_double_nat`` is true, the ``&ip=`` argument in tracker requests (unless otherwise specified) will be set to the intermediate IP address, if the user is double NATed. If ther user is not double NATed, this option has no affect. +``torrent_connect_boost`` is the number of peers to try to connect to immediately +when the first tracker response is received for a torrent. This is a boost to +given to new torrents to accelerate them starting up. The normal connect scheduler +is run once every second, this allows peers to be connected immediately instead +of waiting for the session tick to trigger connections. + pe_settings =========== diff --git a/include/libtorrent/aux_/session_impl.hpp b/include/libtorrent/aux_/session_impl.hpp index 064b5b751..471c6c421 100644 --- a/include/libtorrent/aux_/session_impl.hpp +++ b/include/libtorrent/aux_/session_impl.hpp @@ -743,6 +743,11 @@ namespace libtorrent utp_socket_manager m_utp_socket_manager; + // the number of torrent connection boosts + // connections that have been made this second + // this is deducted from the connect speed + int m_boost_connections; + #ifndef TORRENT_DISABLE_ENCRYPTION pe_settings m_pe_settings; #endif diff --git a/include/libtorrent/session_settings.hpp b/include/libtorrent/session_settings.hpp index 6fae84183..3c6a62266 100644 --- a/include/libtorrent/session_settings.hpp +++ b/include/libtorrent/session_settings.hpp @@ -252,6 +252,7 @@ namespace libtorrent , rate_limit_utp(false) , listen_queue_size(5) , announce_double_nat(false) + , torrent_connect_boost(10) {} // libtorrent version. Used for forward binary compatibility @@ -999,6 +1000,12 @@ namespace libtorrent // IP address if the user is double NATed. If ther user is not // double NATed, this option does not have an affect bool announce_double_nat; + + // the first tracker response after a torrent is started + // will cause this many connections to be made immediately. + // instead of waiting for the connection scheduler which + // triggeres every second + int torrent_connect_boost; }; #ifndef TORRENT_DISABLE_DHT diff --git a/include/libtorrent/torrent.hpp b/include/libtorrent/torrent.hpp index 3a9360d39..674d36eb8 100644 --- a/include/libtorrent/torrent.hpp +++ b/include/libtorrent/torrent.hpp @@ -1243,6 +1243,13 @@ namespace libtorrent // is waiting to finish all current download requests // before actually closing all connections bool m_graceful_pause_mode:1; + + // this is set to true when the torrent starts up + // The first tracker response, when this is true, + // will attempt to connect to a bunch of peers immediately + // and set this to false. We only do this once to get + // the torrent kick-started + bool m_need_connect_boost:1; }; } diff --git a/src/session_impl.cpp b/src/session_impl.cpp index e5eb8fb0a..d1ccc85f6 100644 --- a/src/session_impl.cpp +++ b/src/session_impl.cpp @@ -499,6 +499,7 @@ namespace aux { , m_half_open) , m_utp_socket_manager(m_settings, m_udp_socket , boost::bind(&session_impl::incoming_connection, this, _1)) + , m_boost_connections(0) , m_timer(m_io_service) , m_lsd_announce_timer(m_io_service) , m_host_resolver(m_io_service) @@ -2603,15 +2604,33 @@ namespace aux { // equally likely to connect to a peer int free_slots = m_half_open.free_slots(); + int max_connections = m_settings.connection_speed; + // boost connections are connections made by torrent connection + // boost, which are done immediately on a tracker response. These + // connections needs to be deducted from this second + if (m_boost_connections > 0) + { + if (m_boost_connections > max_connections) + { + m_boost_connections -= max_connections; + max_connections = 0; + } + else + { + max_connections -= m_boost_connections; + m_boost_connections = 0; + } + } + if (!m_torrents.empty() && free_slots > -m_half_open.limit() && num_connections() < m_settings.connections_limit && !m_abort - && m_settings.connection_speed > 0) + && m_settings.connection_speed > 0 + && max_connections > 0) { // this is the maximum number of connections we will // attempt this tick - int max_connections = m_settings.connection_speed; int average_peers = 0; if (num_downloads > 0) average_peers = num_downloads_peers / num_downloads; diff --git a/src/torrent.cpp b/src/torrent.cpp index e555374e3..3d8f77a37 100644 --- a/src/torrent.cpp +++ b/src/torrent.cpp @@ -394,6 +394,7 @@ namespace libtorrent , m_downloaders(0xffffff) , m_interface_index(0) , m_graceful_pause_mode(false) + , m_need_connect_boost(true) { TORRENT_ASSERT(m_ses.is_network_thread()); #if defined TORRENT_VERBOSE_LOGGING || defined TORRENT_LOGGING || defined TORRENT_ERROR_LOGGING @@ -1892,6 +1893,27 @@ namespace libtorrent #endif } } + + if (m_need_connect_boost) + { + m_need_connect_boost = false; + // this is the first tracker response for this torrent + // instead of waiting one second for session_impl::on_tick() + // to be called, connect to a few peers immediately + int conns = (std::min)((std::min)(m_ses.m_settings.torrent_connect_boost + , m_ses.m_settings.connections_limit - m_ses.num_connections()) + , m_ses.m_half_open.free_slots()); + + while (want_more_peers() && conns > 0) + { + if (!m_policy.connect_one_peer(m_ses.session_time())) break; + // increase m_ses.m_boost_connections for each connection + // attempt. This will be deducted from the connect speed + // the next time session_impl::on_tick() is triggered + --conns; + ++m_ses.m_boost_connections; + } + } } ptime torrent::next_announce() const