Browse Source

add back fallback for retrying when connect peers is 0 and idealpeercount > 0

cl-refactor
subtly 10 years ago
parent
commit
49d21e9ada
  1. 10
      libp2p/Host.cpp
  2. 26
      libp2p/Peer.cpp
  3. 10
      libp2p/Peer.h

10
libp2p/Host.cpp

@ -528,6 +528,16 @@ void Host::run(boost::system::error_code const&)
keepAlivePeers();
disconnectLatePeers();
if (m_idealPeerCount && !peerCount())
for (auto p: m_peers)
if (p.second->shouldReconnect())
{
// TODO p2p: fixme
p.second->m_lastAttempted = std::chrono::system_clock::now();
connect(p.second);
break;
}
auto runcb = [this](boost::system::error_code const& error) { run(error); };
m_timer->expires_from_now(boost::posix_time::milliseconds(c_timerInterval));

26
libp2p/Peer.cpp

@ -31,6 +31,32 @@ namespace dev
namespace p2p
{
bool Peer::shouldReconnect() const
{
return chrono::system_clock::now() > m_lastAttempted + chrono::seconds(fallbackSeconds());
}
unsigned Peer::fallbackSeconds() const
{
switch (m_lastDisconnect)
{
case BadProtocol:
return 30 * (m_failedAttempts + 1);
case UselessPeer:
case TooManyPeers:
case ClientQuit:
return 15 * (m_failedAttempts + 1);
case NoDisconnect:
default:
if (m_failedAttempts < 5)
return m_failedAttempts ? m_failedAttempts * 5 : 5;
else if (m_failedAttempts < 15)
return 25 + (m_failedAttempts - 5) * 10;
else
return 25 + 100 + (m_failedAttempts - 15) * 20;
}
}
bool Peer::operator<(Peer const& _p) const
{
if (isOffline() != _p.isOffline())

10
libp2p/Peer.h

@ -62,16 +62,22 @@ public:
virtual bool operator<(Peer const& _p) const;
/// This peers rating.
/// WIP: Returns current peer rating.
int rating() const { return m_rating; }
/// Return true if connection attempt should be made to this peer or false if
bool shouldReconnect() const;
/// Number of times connection has been attempted to peer.
int failedAttempts() const { return m_failedAttempts; }
/// Reason peer was previously disconnected.
DisconnectReason lastDisconnect() const { return m_lastDisconnect; }
protected:
/// Returns number of seconds to wait until attempting connection, based on attempted connection history.
unsigned fallbackSeconds() const;
int m_score = 0; ///< All time cumulative.
int m_rating = 0; ///< Trending.

Loading…
Cancel
Save