2 * UCW Library -- Rate Limiting based on the Token Bucket Filter
4 * (c) 2009 Martin Mares <mj@ucw.cz>
6 * This software may be freely distributed and used according to the terms
7 * of the GNU Lesser General Public License.
14 tbf_init(struct token_bucket_filter *f)
17 f->burst = MAX(2*f->rate, 1);
23 tbf_limit(struct token_bucket_filter *f, timestamp_t now)
25 timestamp_t delta_t = now - f->last_hit;
28 double b = f->bucket + f->rate * delta_t / 1000;
32 uns dropped = f->drop_count;
41 return -f->drop_count;
49 struct token_bucket_filter t = { .rate = 1, .burst = 2 };
51 for (timestamp_t now = 0; now < 3000; now += 77)
53 int res = tbf_limit(&t, now);
54 msg(L_DEBUG, "t=%u result=%d bucket=%f", (uns) now, res, t.bucket);