Skip to content

Commit

Permalink
ratelimit: keep only local values in pipe->counter for SBT
Browse files Browse the repository at this point in the history
Up until this commit, the SBT algorithm would keep in pipe->counter the
sum between its local value, as well as the values gathered from the
other servers in the cluster. This means that it would replicate the
other's counters as well, leading to a broken behavior.
This fix makes sure that the SBT algorithm only stores the local counter
in the pipe->counter field, therefore it will not replicate other's
counters.

(cherry picked from commit 9621cac)
  • Loading branch information
razvancrainea committed Dec 22, 2020
1 parent d19b20e commit ec0092d
Showing 1 changed file with 11 additions and 12 deletions.
23 changes: 11 additions & 12 deletions modules/ratelimit/ratelimit.c
Expand Up @@ -487,7 +487,7 @@ int hash[100] = {18, 50, 51, 39, 49, 68, 8, 78, 61, 75, 53, 32, 45, 77, 31,
* @param update whether or not to inc call number
* @return number of calls in the window
*/
static inline int hist_check(rl_pipe_t *pipe, int update)
static inline unsigned hist_update(rl_pipe_t *pipe, int update)
{
#define U2MILI(__usec__) (__usec__/1000)
#define S2MILI(__sec__) (__sec__ *1000)
Expand All @@ -498,9 +498,6 @@ static inline int hist_check(rl_pipe_t *pipe, int update)

struct timeval tv;

pipe->counter = 0;
pipe->counter = rl_get_all_counters(pipe);

gettimeofday(&tv, NULL);
now_time = S2MILI(tv.tv_sec) + U2MILI(tv.tv_usec);
now_index = (now_time%rl_win_ms) / rl_slot_period;
Expand Down Expand Up @@ -547,11 +544,12 @@ static inline int hist_check(rl_pipe_t *pipe, int update)
pipe->rwin.window[pipe->rwin.start_index] += update;
}

pipe->counter = 0;
/* count the total number of calls in the window */
for (i=0; i < pipe->rwin.window_size; i++)
pipe->counter += pipe->rwin.window[i];

return pipe->counter > pipe->limit ? -1 : 1;
return rl_get_all_counters(pipe);

#undef U2MILI
#undef S2MILI
Expand All @@ -560,9 +558,7 @@ static inline int hist_check(rl_pipe_t *pipe, int update)
int hist_get_count(rl_pipe_t *pipe)
{
/* do a NOP to validate the interval, then return the unchanged counter */
hist_check(pipe, 0);

return pipe->counter;
return hist_update(pipe, 0);
}

void hist_set_count(rl_pipe_t *pipe, long int value)
Expand All @@ -573,7 +569,7 @@ void hist_set_count(rl_pipe_t *pipe, long int value)
pipe->rwin.window_size * sizeof(long int));
pipe->rwin.start_time.tv_sec = 0; /* force init */
} else
hist_check(pipe, value);
hist_update(pipe, value);
}


Expand All @@ -584,7 +580,12 @@ void hist_set_count(rl_pipe_t *pipe, long int value)
*/
int rl_pipe_check(rl_pipe_t *pipe)
{
unsigned counter = rl_get_all_counters(pipe);
unsigned counter;

if (pipe->algo == PIPE_ALGO_HISTORY)
return (hist_update(pipe, 1) > pipe->limit ? -1 : 1);

counter = rl_get_all_counters(pipe);

switch (pipe->algo) {
case PIPE_ALGO_NOP:
Expand All @@ -601,8 +602,6 @@ int rl_pipe_check(rl_pipe_t *pipe)
return (pipe->load ? pipe->load : 1);
case PIPE_ALGO_FEEDBACK:
return (hash[counter % 100] < *drop_rate) ? -1 : 1;
case PIPE_ALGO_HISTORY:
return hist_check(pipe, 1);
default:
LM_ERR("ratelimit algorithm %d not implemented\n", pipe->algo);
}
Expand Down

0 comments on commit ec0092d

Please sign in to comment.