diff --git a/include/ftl/protocol/self.hpp b/include/ftl/protocol/self.hpp index 064a83e068bd14688da3f8ad20a77dd9bd7c0008..51375d121cf964112941b06bf51f52927ec60e61 100644 --- a/include/ftl/protocol/self.hpp +++ b/include/ftl/protocol/self.hpp @@ -114,6 +114,23 @@ class Self { */ size_t numberOfNodes() const; + /** + * @brief Get the maximum allowed number of connections. Any attempt to connect more + * peers will result in them being rejected. + * + * @return size_t + */ + size_t getMaxConnections() const; + + /** + * @brief Set the maximum allowed connections. This should only be changed before + * there are any active connections, resizing with active connections could cause + * errors. The default number is 10. + * + * @param m Number of allowed node connections + */ + void setMaxConnections(size_t m); + /** * @brief Will block until all currently registered connnections have completed. * You should not use this, but rather use onConnect. diff --git a/src/self.cpp b/src/self.cpp index f7a21b8cd1ba541e2b9abc8cbd342f08ebe8b38f..184c784d99240b77cab2625e8968717e3428eaa4 100644 --- a/src/self.cpp +++ b/src/self.cpp @@ -76,6 +76,14 @@ size_t Self::numberOfNodes() const { return universe_->numberOfPeers(); } +size_t Self::getMaxConnections() const { + return universe_->getMaxConnections(); +} + +void Self::setMaxConnections(size_t m) { + universe_->setMaxConnections(m); +} + int Self::waitConnections(int seconds) { return universe_->waitConnections(seconds); } diff --git a/src/universe.cpp b/src/universe.cpp index 26356e78b99f1e1c40d5b9fb1325e86eb4c4b5ca..40ac7ebf31e308504e70a45ae7f4225e06fac990 100644 --- a/src/universe.cpp +++ b/src/universe.cpp @@ -47,6 +47,8 @@ using ftl::net::internal::SocketServer; using ftl::net::internal::Server_TCP; using std::chrono::milliseconds; +constexpr int kDefaultMaxConnections = 10; + namespace ftl { namespace net { @@ -81,30 +83,12 @@ Universe::Universe() : active_(true), this_peer(ftl::protocol::id), impl_(new ftl::net::NetImplDetail()), - peers_(10), + peers_(kDefaultMaxConnections), phase_(0), periodic_time_(1.0), reconnect_attempts_(5), thread_(Universe::__start, this) { _installBindings(); - - // Add an idle timer job to garbage collect peer objects - // Note: Important to be a timer job to ensure no other timer jobs are - // using the object. - // FIXME: Replace use of timer. - /*garbage_timer_ = ftl::timer::add(ftl::timer::kTimerIdle10, [this](int64_t ts) { - if (garbage_.size() > 0) { - UNIQUE_LOCK(net_mutex_,lk); - if (ftl::pool.n_idle() == ftl::pool.size()) { - if (garbage_.size() > 0) LOG(1) << "Garbage collection"; - while (garbage_.size() > 0) { - delete garbage_.front(); - garbage_.pop_front(); - } - } - } - return true; - });*/ } Universe::~Universe() { @@ -112,6 +96,11 @@ Universe::~Universe() { CHECK_EQ(peer_instances_, 0); } +void Universe::setMaxConnections(size_t m) { + UNIQUE_LOCK(net_mutex_, lk); + peers_.resize(m); +} + size_t Universe::getSendBufferSize(ftl::URI::scheme_t s) { // TODO(Nick): Allow these to be configured again. switch (s) { @@ -448,6 +437,19 @@ void Universe::_periodic() { i = reconnects_.erase(i); }*/ } + + // Garbage peers may not be needed any more + if (garbage_.size() > 0) { + UNIQUE_LOCK(net_mutex_, lk); + // Only do garbage if processing is idle. + if (ftl::pool.n_idle() == ftl::pool.size()) { + if (garbage_.size() > 0) DLOG(1) << "Garbage collection"; + while (garbage_.size() > 0) { + garbage_.front().reset(); + garbage_.pop_front(); + } + } + } } void Universe::__start(Universe *u) { diff --git a/src/universe.hpp b/src/universe.hpp index 942557b355345af9c2ba43263be4e253d8e025ba..229dfdd694214a37d1aed31499757138d5cb9160 100644 --- a/src/universe.hpp +++ b/src/universe.hpp @@ -172,6 +172,9 @@ class Universe { static inline std::shared_ptr<Universe> getInstance() { return instance_; } + void setMaxConnections(size_t m); + size_t getMaxConnections() const { return peers_.size(); } + // --- Test support ------------------------------------------------------- PeerPtr injectFakePeer(std::unique_ptr<ftl::net::internal::SocketConnection> s);