Browse Source

Efforts to make reconnect timing strategy more expansive and consistent.

cl-refactor
Gav Wood 11 years ago
parent
commit
2e972be640
  1. 39
      libp2p/Host.cpp
  2. 5
      libp2p/Host.h
  3. 13
      libp2p/Session.cpp
  4. 3
      libp2p/Session.h

39
libp2p/Host.cpp

@ -506,6 +506,7 @@ void Node::connect(Host* _h)
if (ec) if (ec)
{ {
clog(NetConnect) << "Connection refused to node" << id.abridged() << "@" << address << "(" << ec.message() << ")"; clog(NetConnect) << "Connection refused to node" << id.abridged() << "@" << address << "(" << ec.message() << ")";
lastDisconnect = TCPError;
_h->m_ready += index; _h->m_ready += index;
} }
else else
@ -533,26 +534,33 @@ bool Host::havePeer(NodeId _id) const
return !!m_peers.count(_id); return !!m_peers.count(_id);
} }
unsigned cumulativeFallback(unsigned _failed, DisconnectReason _r) unsigned Node::fallbackSeconds() const
{ {
switch (_r) switch (lastDisconnect)
{ {
case BadProtocol: case BadProtocol:
return 30 * (_failed + 1); return 30 * (failedAttempts + 1);
case UselessPeer: case UselessPeer:
case TooManyPeers: case TooManyPeers:
case ClientQuit: case ClientQuit:
return 15 * (_failed + 1); return 15 * (failedAttempts + 1);
case NoDisconnect:
return 0;
default: default:
if (_failed < 5) if (failedAttempts < 5)
return _failed * 5; return failedAttempts * 5;
else if (_failed < 15) else if (failedAttempts < 15)
return 25 + (_failed - 5) * 10; return 25 + (failedAttempts - 5) * 10;
else else
return 25 + 100 + (_failed - 15) * 20; return 25 + 100 + (failedAttempts - 15) * 20;
} }
} }
bool Node::shouldReconnect() const
{
return chrono::system_clock::now() > lastAttempted + chrono::seconds(fallbackSeconds());
}
void Host::growPeers() void Host::growPeers()
{ {
RecursiveGuard l(x_peers); RecursiveGuard l(x_peers);
@ -564,7 +572,7 @@ void Host::growPeers()
toTry -= m_private; toTry -= m_private;
set<Node> ns; set<Node> ns;
for (auto i: toTry) for (auto i: toTry)
if (chrono::system_clock::now() > m_nodes[m_nodesList[i]]->lastAttempted + chrono::seconds(cumulativeFallback(m_nodes[m_nodesList[i]]->failedAttempts, m_nodes[m_nodesList[i]]->lastDisconnect))) if (m_nodes[m_nodesList[i]]->shouldReconnect())
ns.insert(*m_nodes[m_nodesList[i]]); ns.insert(*m_nodes[m_nodesList[i]]);
if (ns.size()) if (ns.size())
@ -577,18 +585,13 @@ void Host::growPeers()
else else
{ {
ensureAccepting(); ensureAccepting();
requestNodes(); for (auto const& i: m_peers)
if (auto p = i.second.lock())
p->ensureNodesRequested();
} }
} }
} }
void Host::requestNodes()
{
for (auto const& i: m_peers)
if (auto p = i.second.lock())
p->ensureNodesRequested();
}
void Host::prunePeers() void Host::prunePeers()
{ {
RecursiveGuard l(x_peers); RecursiveGuard l(x_peers);

5
libp2p/Host.h

@ -75,6 +75,9 @@ struct Node
int secondsSinceLastConnected() const { return lastConnected == std::chrono::system_clock::time_point() ? -1 : (int)std::chrono::duration_cast<std::chrono::seconds>(std::chrono::system_clock::now() - lastConnected).count(); } int secondsSinceLastConnected() const { return lastConnected == std::chrono::system_clock::time_point() ? -1 : (int)std::chrono::duration_cast<std::chrono::seconds>(std::chrono::system_clock::now() - lastConnected).count(); }
int secondsSinceLastAttempted() const { return lastAttempted == std::chrono::system_clock::time_point() ? -1 : (int)std::chrono::duration_cast<std::chrono::seconds>(std::chrono::system_clock::now() - lastAttempted).count(); } int secondsSinceLastAttempted() const { return lastAttempted == std::chrono::system_clock::time_point() ? -1 : (int)std::chrono::duration_cast<std::chrono::seconds>(std::chrono::system_clock::now() - lastAttempted).count(); }
unsigned fallbackSeconds() const;
bool shouldReconnect() const;
bool isOffline() const { return lastDisconnect == -1 || lastAttempted > lastConnected; } bool isOffline() const { return lastDisconnect == -1 || lastAttempted > lastConnected; }
bool operator<(Node const& _n) const bool operator<(Node const& _n) const
{ {
@ -200,8 +203,6 @@ private:
std::shared_ptr<Node> noteNode(NodeId _id, bi::tcp::endpoint const& _a, Origin _o, bool _ready, NodeId _oldId = h256()); std::shared_ptr<Node> noteNode(NodeId _id, bi::tcp::endpoint const& _a, Origin _o, bool _ready, NodeId _oldId = h256());
Nodes potentialPeers(RangeMask<unsigned> const& _known); Nodes potentialPeers(RangeMask<unsigned> const& _known);
void requestNodes();
std::string m_clientVersion; ///< Our version string. std::string m_clientVersion; ///< Our version string.
NetworkPreferences m_netPrefs; ///< Network settings. NetworkPreferences m_netPrefs; ///< Network settings.

13
libp2p/Session.cpp

@ -249,12 +249,15 @@ bool Session::interpret(RLP const& _r)
{ {
string reason = "Unspecified"; string reason = "Unspecified";
auto r = (DisconnectReason)_r[1].toInt<int>(); auto r = (DisconnectReason)_r[1].toInt<int>();
if (_r[1].isInt()) if (!_r[1].isInt())
drop(BadProtocol);
else
{
reason = reasonOf(r); reason = reasonOf(r);
clogS(NetMessageSummary) << "Disconnect (reason: " << reason << ")";
clogS(NetMessageSummary) << "Disconnect (reason: " << reason << ")"; drop(DisconnectRequested);
drop(DisconnectRequested); }
return true; break;
} }
case PingPacket: case PingPacket:
{ {

3
libp2p/Session.h

@ -93,9 +93,6 @@ private:
/// Perform a read on the socket. /// Perform a read on the socket.
void doRead(); void doRead();
/// The
void writeImpl(bytes& _buffer);
/// Perform a single round of the write operation. This could end up calling itself asynchronously. /// Perform a single round of the write operation. This could end up calling itself asynchronously.
void write(); void write();

Loading…
Cancel
Save