From 49d21e9ada1fa140fb66137c7b3bc2453df7df6c Mon Sep 17 00:00:00 2001 From: subtly Date: Wed, 11 Feb 2015 01:43:36 -0500 Subject: [PATCH] add back fallback for retrying when connect peers is 0 and idealpeercount > 0 --- libp2p/Host.cpp | 10 ++++++++++ libp2p/Peer.cpp | 26 ++++++++++++++++++++++++++ libp2p/Peer.h | 10 ++++++++-- 3 files changed, 44 insertions(+), 2 deletions(-) diff --git a/libp2p/Host.cpp b/libp2p/Host.cpp index 5ffb12c8f..e35f8100f 100644 --- a/libp2p/Host.cpp +++ b/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)); diff --git a/libp2p/Peer.cpp b/libp2p/Peer.cpp index 1811da930..4be0fd799 100644 --- a/libp2p/Peer.cpp +++ b/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()) diff --git a/libp2p/Peer.h b/libp2p/Peer.h index 415573c0c..704e5c2b4 100644 --- a/libp2p/Peer.h +++ b/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.