From 3e5542cd957902df53b095b148e9cc0617115c6f Mon Sep 17 00:00:00 2001 From: subtly Date: Sun, 19 Jul 2015 05:55:43 -0400 Subject: [PATCH] Host: Fix guard. Fix acceptor loop when connection count exceeded. Acceptor loop exit on shutdown. --- libp2p/Host.cpp | 49 +++++++++++++++++++++++------------------------ libp2p/Host.h | 2 +- test/TestHelper.h | 2 +- 3 files changed, 26 insertions(+), 27 deletions(-) diff --git a/libp2p/Host.cpp b/libp2p/Host.cpp index d57741e61..46e0efe95 100644 --- a/libp2p/Host.cpp +++ b/libp2p/Host.cpp @@ -399,43 +399,42 @@ void Host::runAcceptor() auto socket = make_shared(new bi::tcp::socket(m_ioService)); m_tcp4Acceptor.async_accept(socket->ref(), [=](boost::system::error_code ec) { - if (peerCount() > 9 * m_idealPeerCount) + m_accepting = false; + if (ec || !m_run) + { + socket->close(); + return; + } + if (peerCount() > Ingress * m_idealPeerCount) { - clog(NetConnect) << "Dropping incoming connect due to maximum peer count (9 * ideal peer count): " << socket->remoteEndpoint(); + clog(NetConnect) << "Dropping incoming connect due to maximum peer count (" << Ingress << " * ideal peer count): " << socket->remoteEndpoint(); socket->close(); if (ec.value() < 1) runAcceptor(); return; } - // if no error code bool success = false; - if (!ec) + try { - try - { - // incoming connection; we don't yet know nodeid - auto handshake = make_shared(this, socket); - m_connecting.push_back(handshake); - handshake->start(); - success = true; - } - catch (Exception const& _e) - { - clog(NetWarn) << "ERROR: " << diagnostic_information(_e); - } - catch (std::exception const& _e) - { - clog(NetWarn) << "ERROR: " << _e.what(); - } + // incoming connection; we don't yet know nodeid + auto handshake = make_shared(this, socket); + m_connecting.push_back(handshake); + handshake->start(); + success = true; + } + catch (Exception const& _e) + { + clog(NetWarn) << "ERROR: " << diagnostic_information(_e); + } + catch (std::exception const& _e) + { + clog(NetWarn) << "ERROR: " << _e.what(); } if (!success) socket->ref().close(); - - m_accepting = false; - if (ec.value() < 1) - runAcceptor(); + runAcceptor(); }); } } @@ -627,7 +626,7 @@ void Host::run(boost::system::error_code const&) m_nodeTable->processEvents(); // cleanup zombies - DEV_GUARDED(x_connecting); + DEV_GUARDED(x_connecting) m_connecting.remove_if([](std::weak_ptr h){ return h.expired(); }); DEV_GUARDED(x_timers) m_timers.remove_if([](std::shared_ptr t) diff --git a/libp2p/Host.h b/libp2p/Host.h index 17e98f8f6..0fe6da5f9 100644 --- a/libp2p/Host.h +++ b/libp2p/Host.h @@ -212,7 +212,7 @@ protected: void restoreNetwork(bytesConstRef _b); private: - enum PeerSlotRatio { Egress = 2, Ingress = 9 }; + enum PeerSlotRatio { Egress = 1, Ingress = 4 }; bool havePeerSession(NodeId const& _id) { return !!peerSession(_id); } diff --git a/test/TestHelper.h b/test/TestHelper.h index 420278838..4ac59e917 100644 --- a/test/TestHelper.h +++ b/test/TestHelper.h @@ -224,7 +224,7 @@ public: bool inputLimits = false; bool bigData = false; bool wallet = false; - bool nonetwork = true; + bool nonetwork = false; bool nodag = true; /// @}