Skip to content

Commit

Permalink
Extract bucket::available concept which is distinct from ::empty when…
Browse files Browse the repository at this point in the history
… rate limiting is happening.
  • Loading branch information
clemahieu committed Sep 12, 2023
1 parent b9fd55b commit c91949d
Show file tree
Hide file tree
Showing 6 changed files with 19 additions and 7 deletions.
2 changes: 1 addition & 1 deletion nano/core_test/scheduler_buckets.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -110,7 +110,7 @@ TEST (buckets, construction)
{
nano::scheduler::buckets buckets;
ASSERT_EQ (0, buckets.size ());
ASSERT_TRUE (buckets.empty ());
ASSERT_TRUE (buckets.empty () && !buckets.available ()); // Initial state
ASSERT_EQ (62, buckets.bucket_count ());
}

Expand Down
5 changes: 5 additions & 0 deletions nano/node/scheduler/bucket.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,11 @@ bool nano::scheduler::bucket::empty () const
return queue.empty ();
}

bool nano::scheduler::bucket::available () const
{
return !queue.empty ();
}

void nano::scheduler::bucket::dump () const
{
for (auto const & item : queue)
Expand Down
1 change: 1 addition & 0 deletions nano/node/scheduler/bucket.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@ class bucket final
void push (uint64_t time, std::shared_ptr<nano::block> block);
size_t size () const;
bool empty () const;
bool available () const;
void dump () const;
};
} // namespace nano::scheduler
15 changes: 10 additions & 5 deletions nano/node/scheduler/buckets.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ void nano::scheduler::buckets::next ()
void nano::scheduler::buckets::seek ()
{
next ();
for (std::size_t i = 0, n = buckets_m.size (); (*current)->empty () && i < n; ++i)
for (std::size_t i = 0, n = buckets_m.size (); !(*current)->available () && i < n; ++i)
{
next ();
}
Expand Down Expand Up @@ -73,10 +73,10 @@ std::size_t nano::scheduler::buckets::index (nano::uint128_t const & balance) co
*/
void nano::scheduler::buckets::push (uint64_t time, std::shared_ptr<nano::block> block, nano::amount const & priority)
{
auto was_empty = empty ();
auto was_available = available ();
auto & bucket = buckets_m[index (priority.number ())];
bucket->push (time, block);
if (was_empty)
if (!was_available)
{
seek ();
}
Expand All @@ -85,15 +85,15 @@ void nano::scheduler::buckets::push (uint64_t time, std::shared_ptr<nano::block>
/** Return the highest priority block of the current bucket */
std::shared_ptr<nano::block> nano::scheduler::buckets::top () const
{
debug_assert (!empty ());
debug_assert (available ());
auto result = (*current)->top ();
return result;
}

/** Pop the current block from the container and seek to the next block, if it exists */
void nano::scheduler::buckets::pop ()
{
debug_assert (!empty ());
debug_assert (available ());
auto & bucket = *current;
bucket->pop ();
seek ();
Expand Down Expand Up @@ -128,6 +128,11 @@ bool nano::scheduler::buckets::empty () const
return std::all_of (buckets_m.begin (), buckets_m.end (), [] (auto const & bucket) { return bucket->empty (); });
}

bool nano::scheduler::buckets::available () const
{
return std::any_of (buckets_m.begin (), buckets_m.end (), [] (auto const & bucket) { return bucket->available (); });
}

/** Print the state of the class in stderr */
void nano::scheduler::buckets::dump () const
{
Expand Down
1 change: 1 addition & 0 deletions nano/node/scheduler/buckets.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,7 @@ class buckets final
std::size_t bucket_count () const;
std::size_t bucket_size (std::size_t index) const;
bool empty () const;
bool available () const;
void dump () const;
std::size_t index (nano::uint128_t const & balance) const;

Expand Down
2 changes: 1 addition & 1 deletion nano/node/scheduler/priority.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -88,7 +88,7 @@ bool nano::scheduler::priority::empty () const

bool nano::scheduler::priority::predicate () const
{
return node.active.vacancy () > 0 && !buckets->empty ();
return node.active.vacancy () > 0 && buckets->available ();
}

void nano::scheduler::priority::run ()
Expand Down

0 comments on commit c91949d

Please sign in to comment.