From 06b609aa1faa860193eb5779bb4703f4ef939e00 Mon Sep 17 00:00:00 2001 From: Arvid Norberg Date: Sat, 2 May 2009 03:15:52 +0000 Subject: [PATCH] fixed unchoke bug. made the automatic unchoke strategy the default --- ChangeLog | 2 ++ include/libtorrent/session_settings.hpp | 2 +- src/peer_connection.cpp | 3 +-- src/session_impl.cpp | 27 +++++++++++++++++++++---- test/test_auto_unchoke.cpp | 2 ++ 5 files changed, 29 insertions(+), 7 deletions(-) diff --git a/ChangeLog b/ChangeLog index b2c2c0dd2..dd09edff7 100644 --- a/ChangeLog +++ b/ChangeLog @@ -1,3 +1,5 @@ + * added support for fully automatic unchoker (no need to specify + number of upload slots). This is on by default * added support for changing socket buffer sizes through session_settings * added support for merkle hash tree torrents (.merkle.torrent) diff --git a/include/libtorrent/session_settings.hpp b/include/libtorrent/session_settings.hpp index 8a4e32405..3fa90cf5f 100644 --- a/include/libtorrent/session_settings.hpp +++ b/include/libtorrent/session_settings.hpp @@ -121,7 +121,7 @@ namespace libtorrent , upnp_ignore_nonrouters(false) , send_buffer_watermark(80 * 1024) , auto_upload_slots(true) - , auto_upload_slots_rate_based(false) + , auto_upload_slots_rate_based(true) , use_parole_mode(true) , cache_size(1024) , cache_expiry(60) diff --git a/src/peer_connection.cpp b/src/peer_connection.cpp index 35c7b4dad..46e0a50c5 100644 --- a/src/peer_connection.cpp +++ b/src/peer_connection.cpp @@ -354,6 +354,7 @@ namespace libtorrent void peer_connection::reset_choke_counters() { m_downloaded_at_last_unchoke = m_statistics.total_payload_download(); + m_uploaded_at_last_unchoke = m_statistics.total_payload_upload(); } void peer_connection::start() @@ -2529,8 +2530,6 @@ namespace libtorrent write_unchoke(); m_choked = false; - m_uploaded_at_last_unchoke = m_statistics.total_payload_upload(); - #ifdef TORRENT_VERBOSE_LOGGING (*m_logger) << time_now_string() << " ==> UNCHOKE\n"; #endif diff --git a/src/session_impl.cpp b/src/session_impl.cpp index ee88c7dee..475e5bccd 100644 --- a/src/session_impl.cpp +++ b/src/session_impl.cpp @@ -1787,6 +1787,7 @@ namespace aux { m_optimistic_unchoke_time_scaler = 0; } t->choke_peer(*p); + continue; } peers.push_back(p.get()); } @@ -1800,6 +1801,21 @@ namespace aux { std::sort(peers.begin(), peers.end() , bind(&peer_connection::upload_rate_compare, _1, _2)); +#ifdef TORRENT_DEBUG + for (std::vector::const_iterator i = peers.begin() + , end(peers.end()), prev(peers.end()); i != end; ++i) + { + if (prev != end) + { + TORRENT_ASSERT((*prev)->uploaded_since_unchoke() * 1000 + / total_milliseconds(unchoke_interval) + >= (*i)->uploaded_since_unchoke() * 1000 + / total_milliseconds(unchoke_interval)); + } + prev = i; + } +#endif + // TODO: make configurable int rate_threshold = 1024; @@ -1809,7 +1825,9 @@ namespace aux { peer_connection const& p = **i; int rate = p.uploaded_since_unchoke() * 1000 / total_milliseconds(unchoke_interval); - if (rate > rate_threshold) ++m_allowed_upload_slots; + + if (rate < rate_threshold) break; + ++m_allowed_upload_slots; // TODO: make configurable rate_threshold += 1024; @@ -1825,9 +1843,6 @@ namespace aux { std::sort(peers.begin(), peers.end() , bind(&peer_connection::unchoke_compare, _1, _2)); - std::for_each(m_connections.begin(), m_connections.end() - , bind(&peer_connection::reset_choke_counters, _1)); - // auto unchoke int upload_limit = m_bandwidth_channel[peer_connection::upload_channel]->throttle(); if (!m_settings.auto_upload_slots_rate_based @@ -1864,6 +1879,10 @@ namespace aux { peer_connection* p = *i; TORRENT_ASSERT(p); TORRENT_ASSERT(!p->ignore_unchoke_slots()); + + // this will update the m_uploaded_at_last_unchoke + p->reset_choke_counters(); + torrent* t = p->associated_torrent().lock().get(); TORRENT_ASSERT(t); if (unchoke_set_size > 0) diff --git a/test/test_auto_unchoke.cpp b/test/test_auto_unchoke.cpp index 68e42a860..778a40afe 100644 --- a/test/test_auto_unchoke.cpp +++ b/test/test_auto_unchoke.cpp @@ -38,6 +38,8 @@ void test_swarm() session_settings settings; settings.allow_multiple_connections_per_ip = true; settings.ignore_limits_on_local_network = false; + settings.auto_upload_slots = true; + settings.auto_upload_slots_rate_based = false; ses1.set_settings(settings); ses2.set_settings(settings); ses3.set_settings(settings);