tor  0.4.1.1-alpha-dev
Data Structures | Macros | Typedefs | Functions
ratelim.h File Reference
#include <time.h>

Go to the source code of this file.

Data Structures

struct  ratelim_t
 

Macros

#define RATELIM_INIT(r)   { (r), 0, 0 }
 
#define RATELIM_TOOMANY   (16*1000*1000)
 

Typedefs

typedef struct ratelim_t ratelim_t
 

Functions

char * rate_limit_log (ratelim_t *lim, time_t now)
 

Detailed Description

Summarize similar messages that would otherwise flood the logs.

Definition in file ratelim.h.

Typedef Documentation

◆ ratelim_t

typedef struct ratelim_t ratelim_t

A ratelim_t remembers how often an event is occurring, and how often it's allowed to occur. Typical usage is something like:

 if (possibly_very_frequent_event()) {
   const int INTERVAL = 300;
   static ratelim_t warning_limit = RATELIM_INIT(INTERVAL);
   char *m;
   if ((m = rate_limit_log(&warning_limit, approx_time()))) {
     log_warn(LD_GENERAL, "The event occurred!%s", m);
     tor_free(m);
   }
 }

As a convenience wrapper for logging, you can replace the above with:

if (possibly_very_frequent_event()) {
  static ratelim_t warning_limit = RATELIM_INIT(300);
  log_fn_ratelim(&warning_limit, LOG_WARN, LD_GENERAL,
                 "The event occurred!");
}

Function Documentation

◆ rate_limit_log()

char* rate_limit_log ( ratelim_t lim,
time_t  now 
)

If the rate-limiter lim is ready at now, return a newly allocated string indicating how many messages were suppressed, suitable to append to a log message. Otherwise return NULL.

Definition at line 41 of file ratelim.c.

References rate_limit_is_ready().