connection rate improvement when using a half-open connections limit

This commit is contained in:
Arvid Norberg 2008-03-16 10:49:47 +00:00
parent 622f81aa18
commit 537f21d1b9
5 changed files with 46 additions and 14 deletions

View File

@ -40,6 +40,10 @@ POSSIBILITY OF SUCH DAMAGE.
#include "libtorrent/socket.hpp"
#include "libtorrent/time.hpp"
#ifdef TORRENT_CONNECTION_LOGGING
#include <fstream>
#endif
namespace libtorrent
{
@ -48,7 +52,9 @@ class connection_queue : public boost::noncopyable
public:
connection_queue(io_service& ios);
bool free_slots() const;
// if there are no free slots, returns the negative
// number of queued up connections
int free_slots() const;
void enqueue(boost::function<void(int)> const& on_connect
, boost::function<void()> const& on_timeout
@ -59,9 +65,7 @@ public:
void close();
#ifndef NDEBUG
void check_invariant() const;
#endif
private:
@ -98,6 +102,9 @@ private:
#ifndef NDEBUG
bool m_in_timeout_function;
#endif
#ifdef TORRENT_CONNECTION_LOGGING
std::ofstream m_log;
#endif
};
}

View File

@ -45,10 +45,18 @@ namespace libtorrent
#ifndef NDEBUG
, m_in_timeout_function(false)
#endif
{}
{
#ifdef TORRENT_CONNECTION_LOGGING
m_log.open("connection_queue.log");
#endif
}
bool connection_queue::free_slots() const
{ return m_num_connecting < m_half_open_limit || m_half_open_limit <= 0; }
int connection_queue::free_slots() const
{
mutex_t::scoped_lock l(m_mutex);
return m_half_open_limit == 0 ? std::numeric_limits<int>::max()
: m_half_open_limit - m_queue.size();
}
void connection_queue::enqueue(boost::function<void(int)> const& on_connect
, boost::function<void()> const& on_timeout
@ -109,7 +117,10 @@ namespace libtorrent
}
void connection_queue::limit(int limit)
{ m_half_open_limit = limit; }
{
TORRENT_ASSERT(limit >= 0);
m_half_open_limit = limit;
}
int connection_queue::limit() const
{ return m_half_open_limit; }
@ -133,8 +144,11 @@ namespace libtorrent
{
INVARIANT_CHECK;
if (!free_slots())
return;
#ifdef TORRENT_CONNECTION_LOGGING
m_log << log_time() << " " << free_slots() << std::endl;
#endif
if (m_num_connecting >= m_half_open_limit) return;
if (m_queue.empty())
{
@ -171,7 +185,11 @@ namespace libtorrent
} catch (std::exception&) {}
#endif
if (!free_slots()) break;
#ifdef TORRENT_CONNECTION_LOGGING
m_log << log_time() << " " << free_slots() << std::endl;
#endif
if (m_num_connecting >= m_half_open_limit) break;
i = std::find_if(i, m_queue.end(), boost::bind(&entry::connecting, _1) == false);
}
}

View File

@ -2943,10 +2943,16 @@ namespace libtorrent
m_connection_ticket = ticket;
boost::shared_ptr<torrent> t = m_torrent.lock();
TORRENT_ASSERT(t);
m_queued = false;
TORRENT_ASSERT(m_connecting);
if (!t)
{
disconnect("torrent aborted");
return;
}
m_socket->open(t->get_interface().protocol(), ec);
if (ec)
{

View File

@ -844,8 +844,9 @@ namespace aux {
// round robin fashion, so that every torrent is
// equallt likely to connect to a peer
int free_slots = m_half_open.free_slots();
if (!m_torrents.empty()
&& m_half_open.free_slots()
&& free_slots > -m_half_open.limit()
&& num_connections() < m_max_connections)
{
// this is the maximum number of connections we will
@ -869,6 +870,7 @@ namespace aux {
if (t.try_connect_peer())
{
--max_connections;
--free_slots;
steps_since_last_connect = 0;
}
}
@ -894,7 +896,7 @@ namespace aux {
// 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 (free_slots <= -m_half_open.limit()) break;
// if we should not make any more connections
// attempts this tick, abort
if (max_connections == 0) break;

View File

@ -2469,7 +2469,6 @@ namespace libtorrent
bool torrent::want_more_peers() const
{
return int(m_connections.size()) < m_max_connections
&& m_ses.m_half_open.free_slots()
&& !m_paused
&& m_state != torrent_status::checking_files
&& m_state != torrent_status::queued_for_checking;