Skip to content

Commit

Permalink
ratelimit: Avoid replicating "zero" pipes excessively
Browse files Browse the repository at this point in the history
This patch makes it so any time a pipe's `.counter` value reaches 0, it
will only get broadcasted to neighbouring cluster nodes exactly 3 more
times, after which it will be completely skipped from being included in
the replication packet.  This counter is then reset to 3 as soon as the
pipe's value is incremented locally.
  • Loading branch information
liviuchircu committed Sep 13, 2024
1 parent 99879ab commit 377ba7f
Show file tree
Hide file tree
Showing 2 changed files with 8 additions and 5 deletions.
1 change: 1 addition & 0 deletions modules/ratelimit/ratelimit.h
Original file line number Diff line number Diff line change
Expand Up @@ -95,6 +95,7 @@ typedef struct rl_pipe {
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 */
int repl_zero_cnt; /* only broadcast a zero counter N times */
rl_window_t rwin; /* window of requests */
} rl_pipe_t;

Expand Down
12 changes: 7 additions & 5 deletions modules/ratelimit/ratelimit_helper.c
Original file line number Diff line number Diff line change
Expand Up @@ -486,6 +486,7 @@ int w_rl_check(struct sip_msg *_m, str *name, int *limit, str *algorithm)
}
} else {
pipe->counter++;
pipe->repl_zero_cnt = 3;
}

ret = rl_pipe_check(pipe);
Expand Down Expand Up @@ -1057,11 +1058,12 @@ void rl_timer_repl(utime_t ticks, void *param)
LM_ERR("[BUG] bogus map[%d] state\n", i);
goto next_pipe;
}
if (!RL_USE_BIN(pipe))
goto next_pipe;

/* do not replicate if about to expire */
if (pipe->last_local_used + rl_expire_time < now)
/* ignore cachedb replicated stuff */
if (!RL_USE_BIN(pipe)
/* ... or pipes with value: 0 after 'cnt' broadcasts */
|| (pipe->counter == 0 && pipe->repl_zero_cnt-- <= 0)
/* ... and do not replicate if about to expire */
|| pipe->last_local_used + rl_expire_time < now)
goto next_pipe;

key = iterator_key(&it);
Expand Down

0 comments on commit 377ba7f

Please sign in to comment.