ratelim.c 1.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354
  1. /* Copyright (c) 2003-2004, Roger Dingledine
  2. * Copyright (c) 2004-2006, Roger Dingledine, Nick Mathewson.
  3. * Copyright (c) 2007-2018, The Tor Project, Inc. */
  4. /* See LICENSE for licensing information */
  5. #include "lib/log/ratelim.h"
  6. #include "lib/malloc/util_malloc.h"
  7. /** If the rate-limiter <b>lim</b> is ready at <b>now</b>, return the number
  8. * of calls to rate_limit_is_ready (including this one!) since the last time
  9. * rate_limit_is_ready returned nonzero. Otherwise return 0.
  10. * If the call number hits <b>RATELIM_TOOMANY</b> limit, drop a warning
  11. * about this event and stop counting. */
  12. static int
  13. rate_limit_is_ready(ratelim_t *lim, time_t now)
  14. {
  15. if (lim->rate + lim->last_allowed <= now) {
  16. int res = lim->n_calls_since_last_time + 1;
  17. lim->last_allowed = now;
  18. lim->n_calls_since_last_time = 0;
  19. return res;
  20. } else {
  21. if (lim->n_calls_since_last_time <= RATELIM_TOOMANY) {
  22. ++lim->n_calls_since_last_time;
  23. }
  24. return 0;
  25. }
  26. }
  27. /** If the rate-limiter <b>lim</b> is ready at <b>now</b>, return a newly
  28. * allocated string indicating how many messages were suppressed, suitable to
  29. * append to a log message. Otherwise return NULL. */
  30. char *
  31. rate_limit_log(ratelim_t *lim, time_t now)
  32. {
  33. int n;
  34. if ((n = rate_limit_is_ready(lim, now))) {
  35. if (n == 1) {
  36. return tor_strdup("");
  37. } else {
  38. char *cp=NULL;
  39. const char *opt_over = (n >= RATELIM_TOOMANY) ? "over " : "";
  40. /* XXXX this is not exactly correct: the messages could have occurred
  41. * any time between the old value of lim->allowed and now. */
  42. tor_asprintf(&cp,
  43. " [%s%d similar message(s) suppressed in last %d seconds]",
  44. opt_over, n-1, lim->rate);
  45. return cp;
  46. }
  47. } else {
  48. return NULL;
  49. }
  50. }