Skip to content

Commit

Permalink
Remove largest burst tracking
Browse files Browse the repository at this point in the history
  • Loading branch information
pwojcikdev committed Nov 24, 2024
1 parent fd73c5e commit deab421
Show file tree
Hide file tree
Showing 3 changed files with 2 additions and 27 deletions.
6 changes: 0 additions & 6 deletions nano/core_test/rate_limiting.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,6 @@ TEST (rate, basic)
// Allow time for the bucket to completely refill and do a full burst
std::this_thread::sleep_for (1s);
ASSERT_TRUE (bucket.try_consume (10));
ASSERT_EQ (bucket.largest_burst (), 10);
}

TEST (rate, network)
Expand All @@ -35,9 +34,7 @@ TEST (rate, network)

// Initial burst of 10 mb/s over two calls
ASSERT_TRUE (bucket.try_consume (5));
ASSERT_EQ (bucket.largest_burst (), 5);
ASSERT_TRUE (bucket.try_consume (5));
ASSERT_EQ (bucket.largest_burst (), 10);
ASSERT_FALSE (bucket.try_consume (5));

// After 200 ms, the 5 mb/s fillrate means we have 1 mb available
Expand Down Expand Up @@ -84,13 +81,10 @@ TEST (rate, unlimited)
{
nano::rate::token_bucket bucket (0, 0);
ASSERT_TRUE (bucket.try_consume (5));
ASSERT_EQ (bucket.largest_burst (), 5);
ASSERT_TRUE (bucket.try_consume (static_cast<size_t> (1e9)));
ASSERT_EQ (bucket.largest_burst (), static_cast<size_t> (1e9));

// With unlimited tokens, consuming always succeed
ASSERT_TRUE (bucket.try_consume (static_cast<size_t> (1e9)));
ASSERT_EQ (bucket.largest_burst (), static_cast<size_t> (1e9));
}

TEST (rate, busy_spin)
Expand Down
17 changes: 2 additions & 15 deletions nano/lib/rate_limiting.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -19,18 +19,12 @@ bool nano::rate::token_bucket::try_consume (unsigned tokens_required)

refill ();

// Keep track of largest observed bucket size so burst size can be computed (for tests and stats)
largest_size = std::max (largest_size, current_size);

bool possible = current_size >= tokens_required;
if (possible)
{
current_size -= tokens_required;
}

// Keep track of smallest observed bucket size so burst size can be computed (for tests and stats)
smallest_size = std::min (smallest_size, current_size);

return possible || refill_rate == unlimited_rate_sentinel;
}

Expand All @@ -48,8 +42,7 @@ void nano::rate::token_bucket::refill ()

void nano::rate::token_bucket::reset (std::size_t max_token_count_a, std::size_t refill_rate_a)
{
// A token count of 0 indicates unlimited capacity. We use 1e9 as
// a sentinel, allowing largest burst to still be computed.
// 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)
{
// Unlimited capacity
Expand All @@ -61,18 +54,12 @@ void nano::rate::token_bucket::reset (std::size_t max_token_count_a, std::size_t
refill_rate_a = unlimited_rate_sentinel;
}

max_token_count = smallest_size = max_token_count_a;
max_token_count = max_token_count_a;
refill_rate = refill_rate_a;
current_size = max_token_count < unlimited_rate_sentinel ? max_token_count : 0;
last_refill = std::chrono::steady_clock::now ();
}

std::size_t nano::rate::token_bucket::largest_burst () const
{
debug_assert (largest_size >= smallest_size);
return largest_size - smallest_size;
}

std::size_t nano::rate::token_bucket::size () const
{
return current_size;
Expand Down
6 changes: 0 additions & 6 deletions nano/lib/rate_limiting.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -44,18 +44,12 @@ class token_bucket
/** Returns the current number of tokens in the bucket */
std::size_t size () const;

/** Returns the largest burst observed */
std::size_t largest_burst () const;

void refill ();

private:
std::size_t max_token_count{ 0 };
std::size_t refill_rate{ 0 };

std::size_t current_size{ 0 };
std::size_t smallest_size{ 0 }; // The minimum observed bucket size, from which the largest burst can be derived
std::size_t largest_size{ 0 }; // The largest observed bucket size, from which the largest burst can be derived
std::chrono::steady_clock::time_point last_refill;

static std::size_t constexpr unlimited_rate_sentinel{ static_cast<std::size_t> (1e9) };
Expand Down

0 comments on commit deab421

Please sign in to comment.