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, ...@@ -329,17 +329,18 @@ handle_request(struct neighbour *neigh, const unsigned char *prefix,
static int static int
check_bucket(struct network *net) check_bucket(struct network *net)
{ {
if(net->bucket > 0 && now.tv_sec > net->bucket_time) { if(net->bucket <= 0) {
net->bucket = int seconds = now.tv_sec - net->bucket_time;
MIN(MAX((int)net->bucket - 40 * (now.tv_sec - net->bucket_time), if(seconds > 0) {
0), net->bucket = MIN(BUCKET_TOKENS_MAX,
400); seconds * BUCKET_TOKENS_PER_SEC);
}
/* Reset bucket time unconditionally, in case clock is stepped. */
net->bucket_time = now.tv_sec;
} }
net->bucket_time = now.tv_sec; if(net->bucket > 0) {
net->bucket--;
if(net->bucket < 400) {
net->bucket++;
return 1; return 1;
} else { } else {
return 0; return 0;
......
...@@ -22,6 +22,9 @@ THE SOFTWARE. ...@@ -22,6 +22,9 @@ THE SOFTWARE.
#define MAX_BUFFERED_UPDATES 200 #define MAX_BUFFERED_UPDATES 200
#define BUCKET_TOKENS_MAX 200
#define BUCKET_TOKENS_PER_SEC 40
extern unsigned short myseqno; extern unsigned short myseqno;
extern struct timeval seqno_time; extern struct timeval seqno_time;
extern int seqno_interval; extern int seqno_interval;
......
...@@ -59,7 +59,7 @@ add_network(char *ifname) ...@@ -59,7 +59,7 @@ add_network(char *ifname)
nets[numnets].sendbuf = NULL; nets[numnets].sendbuf = NULL;
nets[numnets].buffered = 0; nets[numnets].buffered = 0;
nets[numnets].bucket_time = now.tv_sec; nets[numnets].bucket_time = now.tv_sec;
nets[numnets].bucket = 0; nets[numnets].bucket = BUCKET_TOKENS_MAX;
nets[numnets].hello_seqno = (random() & 0xFFFF); nets[numnets].hello_seqno = (random() & 0xFFFF);
numnets++; numnets++;
return &nets[numnets - 1]; 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