Rate-limit counter should increase once per minute.

This commit is contained in:
George Kadianakis 2021-03-10 14:37:36 +02:00
parent e688580277
commit 7740a8b5d4
2 changed files with 54 additions and 4 deletions

View File

@ -280,18 +280,29 @@ STMT_END
void
rep_hist_note_overload(overload_type_t overload)
{
static time_t last_read_counted = 0;
static time_t last_write_counted = 0;
switch (overload) {
case OVERLOAD_GENERAL:
SET_TO_START_OF_HOUR(overload_stats.overload_general_time);
break;
case OVERLOAD_READ:
case OVERLOAD_READ: {
SET_TO_START_OF_HOUR(overload_stats.overload_ratelimits_time);
overload_stats.overload_read_count++;
if (approx_time() >= last_read_counted + 60) { /* Count once a minute */
overload_stats.overload_read_count++;
last_read_counted = approx_time();
}
break;
case OVERLOAD_WRITE:
}
case OVERLOAD_WRITE: {
SET_TO_START_OF_HOUR(overload_stats.overload_ratelimits_time);
overload_stats.overload_write_count++;
if (approx_time() >= last_write_counted + 60) { /* Count once a minute */
overload_stats.overload_write_count++;
last_write_counted = approx_time();
}
break;
}
case OVERLOAD_FD_EXHAUSTED:
SET_TO_START_OF_HOUR(overload_stats.overload_fd_exhausted_time);
overload_stats.overload_fd_exhausted++;

View File

@ -807,6 +807,45 @@ test_overload_stats(void *arg)
stats_str = rep_hist_get_overload_stats_lines();
tt_assert(!stats_str);
/* Now test the rate-limit rate-limiter ;) */
for (int i = 0; i < 10; i++) {
rep_hist_note_overload(OVERLOAD_READ);
}
/* We already have an event registered from the previous tests. We just
* registered ten more overload events, but only one should have been counted
* because of the rate limiter */
stats_str = rep_hist_get_overload_stats_lines();
tt_str_op("overload-ratelimits 1 2002-01-10 02:00:00 1000 2000 2 0",
OP_EQ, stats_str);
tor_free(stats_str);
/* Increment time by 59 secs and try again. No additional events should
register */
current_time += 59;
update_approx_time(current_time);
for (int i = 0; i < 10; i++) {
rep_hist_note_overload(OVERLOAD_READ);
}
stats_str = rep_hist_get_overload_stats_lines();
tt_str_op("overload-ratelimits 1 2002-01-10 02:00:00 1000 2000 2 0",
OP_EQ, stats_str);
tor_free(stats_str);
/* Now increment time by 2 secs -- taking it after the minute rate limiting
and see that events will register again */
current_time += 2;
update_approx_time(current_time);
for (int i = 0; i < 10; i++) {
rep_hist_note_overload(OVERLOAD_READ);
rep_hist_note_overload(OVERLOAD_WRITE);
}
stats_str = rep_hist_get_overload_stats_lines();
tt_str_op("overload-ratelimits 1 2002-01-10 02:00:00 1000 2000 3 1",
OP_EQ, stats_str);
tor_free(stats_str);
done:
tor_free(stats_str);
}