Skip to content

Commit

Permalink
rework ratelimit distributed queries
Browse files Browse the repository at this point in the history
instead of adding -1 to the counter, use the cachedb_sub function exported by
the cachedb module. This prevents ratelimit counters from adding extremely
large values for some backends (ex: memcached).
Thanks go to Brett Nemeroff for reporting and testing.
  • Loading branch information
razvancrainea committed Nov 15, 2013
1 parent cae7f80 commit a8bdfa8
Showing 1 changed file with 17 additions and 3 deletions.
20 changes: 17 additions & 3 deletions modules/ratelimit/ratelimit_helper.c
Expand Up @@ -105,17 +105,31 @@ static inline int rl_set_name(str * name)
static int rl_change_counter(str *name, rl_pipe_t *pipe, int c)
{
int new_counter;
int ret;

if (rl_set_name(name) < 0)
return -1;

if (pipe->my_counter + c <= 0) {
if (pipe->my_counter + c < 0) {
LM_DBG("Counter going negative\n");
return 1;
}

if (cdbf.add(cdbc, &rl_name_buffer, c ? c : -(pipe->my_counter),
rl_expire_time, &new_counter) < 0){
if (c) {
if (c < 0)
ret = cdbf.sub(cdbc, &rl_name_buffer, -c, rl_expire_time, &new_counter);
else
ret = cdbf.add(cdbc, &rl_name_buffer, c, rl_expire_time, &new_counter);
} else {
if (pipe->my_counter) {
ret = cdbf.sub(cdbc, &rl_name_buffer, pipe->my_counter, rl_expire_time,
&new_counter);
} else {
ret = cdbf.get_counter(cdbc, &rl_name_buffer, &new_counter);
}
}

if (ret < 0) {
LM_ERR("cannot change counter for pipe %.*s with %d\n",
name->len, name->s, c);
return -1;
Expand Down

0 comments on commit a8bdfa8

Please sign in to comment.