1 /* policer.c - Leaky bucket
2 * Copyright 2000-2002 srvx Development Team
4 * This program is free software; you can redistribute it and/or modify
5 * it under the terms of the GNU General Public License as published by
6 * the Free Software Foundation; either version 2 of the License, or
7 * (at your option) any later version. Important limitations are
8 * listed in the COPYING file that accompanies this software.
10 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU General Public License for more details.
15 * You should have received a copy of the GNU General Public License
16 * along with this program; if not, email srvx-maintainers@srvx.net.
22 /* This policer uses the "leaky bucket" (GCRA) algorithm. */
24 struct policer_params {
29 struct policer_params *
30 policer_params_new(void)
32 struct policer_params *params = malloc(sizeof(struct policer_params));
33 params->bucket_size = 0.0;
34 params->drain_rate = 0.0;
39 policer_params_set(struct policer_params *params, const char *param, const char *value)
41 if (!irccasecmp(param, "size")) {
42 params->bucket_size = strtod(value, NULL);
43 } else if (!irccasecmp(param, "drain-rate")) {
44 params->drain_rate = strtod(value, NULL);
52 policer_params_delete(struct policer_params *params)
58 policer_conforms(struct policer *pol, time_t reqtime, double weight)
61 pol->level -= pol->params->drain_rate * (reqtime - pol->last_req);
62 if (pol->level < 0.0) pol->level = 0.0;
63 res = pol->level < pol->params->bucket_size;
65 pol->last_req = reqtime;