tor  0.4.1.0-alpha-dev
ratelim.c
Go to the documentation of this file.
1 /* Copyright (c) 2003-2004, Roger Dingledine
2  * Copyright (c) 2004-2006, Roger Dingledine, Nick Mathewson.
3  * Copyright (c) 2007-2019, The Tor Project, Inc. */
4 /* See LICENSE for licensing information */
5 
11 #include "lib/log/ratelim.h"
12 #include "lib/malloc/malloc.h"
13 #include "lib/string/printf.h"
14 
20 static int
21 rate_limit_is_ready(ratelim_t *lim, time_t now)
22 {
23  if (lim->rate + lim->last_allowed <= now) {
24  int res = lim->n_calls_since_last_time + 1;
25  lim->last_allowed = now;
26  lim->n_calls_since_last_time = 0;
27  return res;
28  } else {
29  if (lim->n_calls_since_last_time <= RATELIM_TOOMANY) {
30  ++lim->n_calls_since_last_time;
31  }
32 
33  return 0;
34  }
35 }
36 
40 char *
41 rate_limit_log(ratelim_t *lim, time_t now)
42 {
43  int n;
44  if ((n = rate_limit_is_ready(lim, now))) {
45  if (n == 1) {
46  return tor_strdup("");
47  } else {
48  char *cp=NULL;
49  const char *opt_over = (n >= RATELIM_TOOMANY) ? "over " : "";
50  /* XXXX this is not exactly correct: the messages could have occurred
51  * any time between the old value of lim->allowed and now. */
52  tor_asprintf(&cp,
53  " [%s%d similar message(s) suppressed in last %d seconds]",
54  opt_over, n-1, lim->rate);
55  return cp;
56  }
57  } else {
58  return NULL;
59  }
60 }
Header for printf.c.
Summarize similar messages that would otherwise flood the logs.
char * rate_limit_log(ratelim_t *lim, time_t now)
Definition: ratelim.c:41
Headers for util_malloc.c.
int tor_asprintf(char **strp, const char *fmt,...)
Definition: printf.c:75
static int rate_limit_is_ready(ratelim_t *lim, time_t now)
Definition: ratelim.c:21