Skip to content

Commit

Permalink
ratelimit: proper expire for replicated pipes
Browse files Browse the repository at this point in the history
Instead of updating the last_used on every received pipe, we shall only
consider the last locally used value when we want to expire. This way,
we only replicate pipes that have been locally used recently, if the
ones that are not, will not be replicated, thus will eventually be
deleted by the backup nodes.

Thanks go to Ken Rice (SIPNav) for reporting this and troubleshooting
the problem.

(cherry picked from commit d430c1a)
  • Loading branch information
razvancrainea committed Aug 17, 2021
1 parent 0370387 commit c266e8c
Show file tree
Hide file tree
Showing 2 changed files with 10 additions and 2 deletions.
3 changes: 2 additions & 1 deletion modules/ratelimit/ratelimit.h
Expand Up @@ -73,7 +73,8 @@ typedef struct rl_pipe {
int last_counter; /* last counter */
int load; /* countes the accesses */
rl_algo_t algo; /* the algorithm used */
unsigned long last_used; /* timestamp when the pipe was last accessed */
time_t last_used; /* timestamp when the pipe was last accessed */
time_t last_local_used; /* timestamp when the pipe was last locally accessed */
rl_repl_counter_t *dsts; /* counters per destination */
rl_window_t rwin; /* window of requests */
} rl_pipe_t;
Expand Down
9 changes: 8 additions & 1 deletion modules/ratelimit/ratelimit_helper.c
Expand Up @@ -389,6 +389,7 @@ int w_rl_check(struct sip_msg *_m, str *name, int *limit, str *algorithm)
name->len, name->s, *pipe);
if ((*pipe)->algo == PIPE_ALGO_NETWORK)
should_update = 1;
(*pipe)->last_local_used = time(0);
} else {
LM_DBG("Pipe %.*s found: %p - last used %lu\n",
name->len, name->s, *pipe, (*pipe)->last_used);
Expand Down Expand Up @@ -476,7 +477,8 @@ void rl_timer(unsigned int ticks, void *param)
goto next_pipe;
}
/* check to see if it is expired */
if ((*pipe)->last_used + rl_expire_time < now) {
if (((*pipe)->last_local_used + rl_expire_time < now) &&
((*pipe)->last_used + rl_expire_time + rl_timer_interval < now)) {
/* this pipe is engaged in a transaction */
del = it;
if (iterator_next(&it) < 0)
Expand Down Expand Up @@ -904,6 +906,7 @@ void rl_timer_repl(utime_t ticks, void *param)
int nr = 0;
int ret = 0;
bin_packet_t packet;
time_t now = time(0);

if (bin_init(&packet, &pipe_repl_cap, RL_PIPE_COUNTER, BIN_VERSION, 0) < 0) {
LM_ERR("cannot initiate bin buffer\n");
Expand All @@ -928,6 +931,10 @@ void rl_timer_repl(utime_t ticks, void *param)
if (RL_USE_CDB(*pipe))
goto next_pipe;

/* do not replicate if about to expire */
if ((*pipe)->last_local_used + rl_expire_time < now)
goto next_pipe;

key = iterator_key(&it);
if (!key) {
LM_ERR("cannot retrieve pipe key\n");
Expand Down

0 comments on commit c266e8c

Please sign in to comment.