Commit 96e7ba69 authored by Juliusz Chroboczek's avatar Juliusz Chroboczek

Rewrite token bucket.

parent 2171df59
......@@ -329,17 +329,18 @@ handle_request(struct neighbour *neigh, const unsigned char *prefix,
static int
check_bucket(struct network *net)
{
if(net->bucket > 0 && now.tv_sec > net->bucket_time) {
net->bucket =
MIN(MAX((int)net->bucket - 40 * (now.tv_sec - net->bucket_time),
0),
400);
if(net->bucket <= 0) {
int seconds = now.tv_sec - net->bucket_time;
if(seconds > 0) {
net->bucket = MIN(BUCKET_TOKENS_MAX,
seconds * BUCKET_TOKENS_PER_SEC);
}
/* Reset bucket time unconditionally, in case clock is stepped. */
net->bucket_time = now.tv_sec;
}
if(net->bucket < 400) {
net->bucket++;
if(net->bucket > 0) {
net->bucket--;
return 1;
} else {
return 0;
......
......@@ -22,6 +22,9 @@ THE SOFTWARE.
#define MAX_BUFFERED_UPDATES 200
#define BUCKET_TOKENS_MAX 200
#define BUCKET_TOKENS_PER_SEC 40
extern unsigned short myseqno;
extern struct timeval seqno_time;
extern int seqno_interval;
......
......@@ -59,7 +59,7 @@ add_network(char *ifname)
nets[numnets].sendbuf = NULL;
nets[numnets].buffered = 0;
nets[numnets].bucket_time = now.tv_sec;
nets[numnets].bucket = 0;
nets[numnets].bucket = BUCKET_TOKENS_MAX;
nets[numnets].hello_seqno = (random() & 0xFFFF);
numnets++;
return &nets[numnets - 1];
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment