Skip to content

Commit

Permalink
[ratelimit] Fix accuracy on the ratelimit timer
Browse files Browse the repository at this point in the history
The computation perform by the ratelimit module is very time sensitive - any deviation of the interval between the runs of the timer handler may lead to false positive detection of CPS thresholds.
So, let's move the timer handler from regular timer (1s sensitivity) to utimer (100ms sensitivity) -> 10 times more accurate.
Still, the number of triggerings is the same, only the precision is 10 times better.
Credits go to @ovidiusas
Closes #2797
  • Loading branch information
bogdan-iancu committed Jul 4, 2023
1 parent 88a0622 commit 0378869
Show file tree
Hide file tree
Showing 3 changed files with 4 additions and 4 deletions.
4 changes: 2 additions & 2 deletions modules/ratelimit/ratelimit.c
Original file line number Diff line number Diff line change
Expand Up @@ -418,8 +418,8 @@ static int mod_init(void)
}

/* register timer to reset counters */
if (register_timer("rl-timer", rl_timer, NULL,
rl_timer_interval, TIMER_FLAG_DELAY_ON_DELAY) < 0 ) {
if (register_utimer("rl-timer", rl_timer, NULL,
rl_timer_interval*1000*1000U, TIMER_FLAG_DELAY_ON_DELAY) < 0 ) {
LM_ERR("could not register timer function\n");
return -1;
}
Expand Down
2 changes: 1 addition & 1 deletion modules/ratelimit/ratelimit.h
Original file line number Diff line number Diff line change
Expand Up @@ -148,7 +148,7 @@ void do_update_load(void);
void pid_setpoint_limit(int);

/* timer */
void rl_timer(unsigned int, void *);
void rl_timer(utime_t, void *);
void rl_timer_repl(utime_t, void *);

/* cachedb functions */
Expand Down
2 changes: 1 addition & 1 deletion modules/ratelimit/ratelimit_helper.c
Original file line number Diff line number Diff line change
Expand Up @@ -507,7 +507,7 @@ int w_rl_check(struct sip_msg *_m, str *name, int *limit, str *algorithm)
}

/* timer housekeeping, invoked each timer interval to reset counters */
void rl_timer(unsigned int ticks, void *param)
void rl_timer(utime_t uticks, void *param)
{
unsigned int i = 0;
map_iterator_t it, del;
Expand Down

0 comments on commit 0378869

Please sign in to comment.