Skip to content

Commit

Permalink
rate_limiter use int64_t instead of long
Browse files Browse the repository at this point in the history
  • Loading branch information
StephenRi committed Nov 1, 2023
1 parent f1f9eaf commit ff5d5f5
Show file tree
Hide file tree
Showing 2 changed files with 5 additions and 7 deletions.
4 changes: 1 addition & 3 deletions include/ylt/coro_io/rate_limiter.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -50,8 +50,6 @@ class rate_limiter {
int permits, std::chrono::steady_clock::time_point now_micros) = 0;
std::chrono::steady_clock::time_point current_time_mills() {
return std::chrono::steady_clock::now();
// return std::chrono::duration_cast<std::chrono::milliseconds>(
// now.time_since_epoch());
}

private:
Expand Down Expand Up @@ -114,7 +112,7 @@ class abstract_smooth_rate_limiter : public rate_limiter {
stored_permits_to_wait_time(this->stored_permits_,
stored_permits_to_spend) +
std::chrono::milliseconds(
(long)(fresh_permits * this->stable_internal_micros_));
(int64_t)(fresh_permits * this->stable_internal_micros_));
this->next_free_ticket_micros_ += wait_micros;
this->stored_permits_ -= stored_permits_to_spend;
return return_value;
Expand Down
8 changes: 4 additions & 4 deletions src/coro_io/tests/test_rate_limiter.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
#include <ylt/coro_io/io_context_pool.hpp>
#include <ylt/coro_io/rate_limiter.hpp>

long current_time_mills() {
int64_t current_time_mills() {
std::chrono::system_clock::time_point now = std::chrono::system_clock::now();
std::chrono::milliseconds ms =
std::chrono::duration_cast<std::chrono::milliseconds>(
Expand All @@ -30,7 +30,7 @@ TEST_CASE("test smooth_bursty_rate_limiter acquire multi permits") {
// acquire much enough, next acquire will wait
async_simple::coro::syncAwait(rate_limiter.acquire(permits_to_acquire));

long start_mills = current_time_mills();
int64_t start_mills = current_time_mills();
async_simple::coro::syncAwait(rate_limiter.acquire(1));
double cost = (current_time_mills() - start_mills) / 1000.0;
CHECK(cost > expected_cost - cost_diff);
Expand All @@ -43,7 +43,7 @@ TEST_CASE("test smooth_bursty_rate_limiter single thread") {
double expected_cost = (permits_to_acquire - 1) * (1 / permits_per_second);
double cost_diff = 0.1;

long start_mills = current_time_mills();
int64_t start_mills = current_time_mills();
coro_io::smooth_bursty_rate_limiter rate_limiter(permits_per_second);
for (int i = 0; i < permits_to_acquire; i++) {
std::chrono::milliseconds wait_mills =
Expand Down Expand Up @@ -81,7 +81,7 @@ TEST_CASE("test smooth_bursty_rate_limiter multi coroutine") {
co_await collectAllPara(std::move(lazy_list));
};

long start_mills = current_time_mills();
int64_t start_mills = current_time_mills();
syncAwait(consumer_list_lazy().via(
coro_io::g_block_io_context_pool<coro_io::multithread_context_pool>(1)
.get_executor()));
Expand Down

0 comments on commit ff5d5f5

Please sign in to comment.