Skip to content

Commit

Permalink
Single threaded rate limiter variant
Browse files Browse the repository at this point in the history
  • Loading branch information
pwojcikdev committed Mar 4, 2024
1 parent ab42634 commit fc59674
Show file tree
Hide file tree
Showing 4 changed files with 43 additions and 12 deletions.
4 changes: 0 additions & 4 deletions nano/lib/rate_limiting.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,6 @@ nano::rate::token_bucket::token_bucket (std::size_t max_token_count_a, std::size
bool nano::rate::token_bucket::try_consume (unsigned tokens_required_a)
{
debug_assert (tokens_required_a <= 1e9);
nano::lock_guard<nano::mutex> guard{ mutex };
refill ();
bool possible = current_size >= tokens_required_a;
if (possible)
Expand Down Expand Up @@ -44,14 +43,11 @@ void nano::rate::token_bucket::refill ()

std::size_t nano::rate::token_bucket::largest_burst () const
{
nano::lock_guard<nano::mutex> guard{ mutex };
return max_token_count - smallest_size;
}

void nano::rate::token_bucket::reset (std::size_t max_token_count_a, std::size_t refill_rate_a)
{
nano::lock_guard<nano::mutex> guard{ mutex };

// A token count of 0 indicates unlimited capacity. We use 1e9 as
// a sentinel, allowing largest burst to still be computed.
if (max_token_count_a == 0 || refill_rate_a == 0)
Expand Down
2 changes: 0 additions & 2 deletions nano/lib/rate_limiting.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -56,8 +56,6 @@ namespace rate
std::size_t smallest_size{ 0 };
std::chrono::steady_clock::time_point last_refill;

mutable nano::mutex mutex;

static std::size_t constexpr unlimited_rate_sentinel{ static_cast<std::size_t> (1e9) };
};
}
Expand Down
29 changes: 25 additions & 4 deletions nano/node/bandwidth_limiter.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2,24 +2,45 @@
#include <nano/node/bandwidth_limiter.hpp>

/*
* bandwidth_limiter
* bandwidth_limiter_st
*/

nano::bandwidth_limiter::bandwidth_limiter (std::size_t limit_a, double burst_ratio_a) :
nano::bandwidth_limiter_st::bandwidth_limiter_st (std::size_t limit_a, double burst_ratio_a) :
bucket (static_cast<std::size_t> (limit_a * burst_ratio_a), limit_a)
{
}

bool nano::bandwidth_limiter::should_pass (std::size_t message_size_a)
bool nano::bandwidth_limiter_st::should_pass (std::size_t message_size_a)
{
return bucket.try_consume (nano::narrow_cast<unsigned int> (message_size_a));
}

void nano::bandwidth_limiter::reset (std::size_t limit_a, double burst_ratio_a)
void nano::bandwidth_limiter_st::reset (std::size_t limit_a, double burst_ratio_a)
{
bucket.reset (static_cast<std::size_t> (limit_a * burst_ratio_a), limit_a);
}

/*
* bandwidth_limiter_mt
*/

nano::bandwidth_limiter_mt::bandwidth_limiter_mt (std::size_t limit_a, double burst_ratio_a) :
bucket (static_cast<std::size_t> (limit_a * burst_ratio_a), limit_a)
{
}

bool nano::bandwidth_limiter_mt::should_pass (std::size_t message_size_a)
{
nano::lock_guard<nano::mutex> guard{ mutex };
return bucket.try_consume (nano::narrow_cast<unsigned int> (message_size_a));
}

void nano::bandwidth_limiter_mt::reset (std::size_t limit_a, double burst_ratio_a)
{
nano::lock_guard<nano::mutex> guard{ mutex };
bucket.reset (static_cast<std::size_t> (limit_a * burst_ratio_a), limit_a);
}

/*
* outbound_bandwidth_limiter
*/
Expand Down
20 changes: 18 additions & 2 deletions nano/node/bandwidth_limiter.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -21,11 +21,11 @@ nano::bandwidth_limit_type to_bandwidth_limit_type (nano::transport::traffic_typ
/**
* Class that tracks and manages bandwidth limits for IO operations
*/
class bandwidth_limiter final
class bandwidth_limiter_st final // Single-threaded
{
public:
// initialize with limit 0 = unbounded
bandwidth_limiter (std::size_t limit, double burst_ratio);
bandwidth_limiter_st (std::size_t limit, double burst_ratio);

bool should_pass (std::size_t buffer_size);
void reset (std::size_t limit, double burst_ratio);
Expand All @@ -34,6 +34,22 @@ class bandwidth_limiter final
nano::rate::token_bucket bucket;
};

class bandwidth_limiter_mt final // Multi-threaded
{
public:
// initialize with limit 0 = unbounded
bandwidth_limiter_mt (std::size_t limit, double burst_ratio);

bool should_pass (std::size_t buffer_size);
void reset (std::size_t limit, double burst_ratio);

private:
nano::rate::token_bucket bucket;
nano::mutex mutex;
};

using bandwidth_limiter = bandwidth_limiter_mt;

class outbound_bandwidth_limiter final
{
public: // Config
Expand Down

0 comments on commit fc59674

Please sign in to comment.