]> mj.ucw.cz Git - libucw.git/blob - ucw/tbf.h
Main: Introduce functions for telling whether an object is active
[libucw.git] / ucw / tbf.h
1 /*
2  *      UCW Library -- Rate Limiting based on the Token Bucket Filter
3  *
4  *      (c) 2009 Martin Mares <mj@ucw.cz>
5  *
6  *      This software may be freely distributed and used according to the terms
7  *      of the GNU Lesser General Public License.
8  */
9
10 #ifndef _UCW_TBF_H_
11 #define _UCW_TBF_H_
12
13 /** A data structure describing a single TBF. **/
14 struct token_bucket_filter {
15   double rate;                          // Number of tokens received per second
16   uns burst;                            // Capacity of the bucket
17   timestamp_t last_hit;                 // Internal state...
18   double bucket;
19   uns drop_count;
20 };
21
22 /** Initialize the bucket. **/
23 void tbf_init(struct token_bucket_filter *f);
24
25 /**
26  * Ask the filter to process a single event. Returns a negative number
27  * if the event exceeds the rate (and should be dropped) and a non-negative
28  * number if the event passes the filter.
29  * The absolute value of the result is the number of dropped events
30  * since the last passed event.
31  **/
32 int tbf_limit(struct token_bucket_filter *f, timestamp_t now);
33
34 #endif