Skip to content

Commit

Permalink
Rate limit cementing
Browse files Browse the repository at this point in the history
  • Loading branch information
pwojcikdev committed Nov 27, 2024
1 parent 63bafa7 commit dff18bf
Show file tree
Hide file tree
Showing 3 changed files with 26 additions and 2 deletions.
1 change: 1 addition & 0 deletions nano/lib/stats_enums.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -149,6 +149,7 @@ enum class detail
sync,
requeued,
evicted,
rate_limited,

// processing queue
queue,
Expand Down
21 changes: 19 additions & 2 deletions nano/node/confirming_set.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ nano::confirming_set::confirming_set (confirming_set_config const & config_a, na
block_processor{ block_processor_a },
stats{ stats_a },
logger{ logger_a },
limiter{ config.rate_limit, /* unlimited token bucket capacity */ 0 },
workers{ 1, nano::thread_role::name::confirmation_height_notifications }
{
batch_cemented.add ([this] (auto const & cemented) {
Expand Down Expand Up @@ -134,7 +135,9 @@ void nano::confirming_set::run ()
}
else
{
condition.wait (lock, [&] () { return !set.empty () || stopped; });
condition.wait (lock, [&] () {
return !set.empty () || stopped;
});
}
}
}
Expand Down Expand Up @@ -243,11 +246,24 @@ void nano::confirming_set::run_batch (std::unique_lock<std::mutex> & lock)
{
// Confirming this block may implicitly confirm more
stats.add (nano::stat::type::confirming_set, nano::stat::detail::cemented, added.size ());
for (auto & block : added)
for (auto const & block : added)
{
cemented.push_back ({ block, hash, election });
}
cemented_count += added.size ();

// Rate limit cementing
while (!limiter.should_pass (added.size ()))
{
stats.inc (nano::stat::type::confirming_set, nano::stat::detail::rate_limited);
transaction.commit ();
std::this_thread::sleep_for (100ms);
transaction.renew ();
if (stopped)
{
return;
}
}
}
else
{
Expand Down Expand Up @@ -336,6 +352,7 @@ nano::container_info nano::confirming_set::container_info () const
nano::container_info info;
info.put ("set", set);
info.put ("deferred", deferred);
info.put ("limiter", limiter.size ());
info.add ("workers", workers.container_info ());
return info;
}
6 changes: 6 additions & 0 deletions nano/node/confirming_set.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
#include <nano/lib/numbers.hpp>
#include <nano/lib/numbers_templ.hpp>
#include <nano/lib/observer_set.hpp>
#include <nano/lib/rate_limiting.hpp>
#include <nano/lib/thread_pool.hpp>
#include <nano/node/fwd.hpp>
#include <nano/secure/common.hpp>
Expand Down Expand Up @@ -41,6 +42,9 @@ class confirming_set_config final
size_t max_deferred{ 16 * 1024 };
/** Max age of deferred blocks before they are dropped */
std::chrono::seconds deferred_age_cutoff{ 15min };

/** For bounded backlog testing */
size_t rate_limit{ 0 };
};

/**
Expand Down Expand Up @@ -120,6 +124,8 @@ class confirming_set final
// Blocks that are being cemented in the current batch
std::unordered_set<nano::block_hash> current;

nano::rate_limiter limiter;

std::atomic<bool> stopped{ false };
mutable std::mutex mutex;
std::condition_variable condition;
Expand Down

0 comments on commit dff18bf

Please sign in to comment.