[tor-commits] [tor/master] Rate-limit counter should increase once per minute.

asn at torproject.org asn at torproject.org
Wed Mar 17 16:24:24 UTC 2021


commit 7740a8b5d4de649e3ba2a0578f789140725974b6
Author: George Kadianakis <desnacked at riseup.net>
Date:   Wed Mar 10 14:37:36 2021 +0200

    Rate-limit counter should increase once per minute.
---
 src/feature/stats/rephist.c | 19 +++++++++++++++----
 src/test/test_stats.c       | 39 +++++++++++++++++++++++++++++++++++++++
 2 files changed, 54 insertions(+), 4 deletions(-)

diff --git a/src/feature/stats/rephist.c b/src/feature/stats/rephist.c
index 64bec000df..2c3f362e45 100644
--- a/src/feature/stats/rephist.c
+++ b/src/feature/stats/rephist.c
@@ -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++;
diff --git a/src/test/test_stats.c b/src/test/test_stats.c
index 1f14255e31..a22a42f723 100644
--- a/src/test/test_stats.c
+++ b/src/test/test_stats.c
@@ -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);
 }





More information about the tor-commits mailing list