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;
46 struct token_bucket_filter t = { .rate = 1, .burst = 2 };
48 for (timestamp_t now = 0; now < 3000; now += 77)
50 int res = tbf_limit(&t, now);
51 msg(L_DEBUG, "t=%u result=%d bucket=%f", (uns) now, res, t.bucket);