From 934088ca4af2c3af9d42028702fd9bd59ad93b79 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Nils=20O=2E=20Sel=C3=A5sdal?= Date: Tue, 15 Oct 2013 22:31:57 +0200 Subject: [PATCH] Remove ticket_cost member as it is no longer needed --- include/ucore/rate_limit.h | 13 ++++++++++--- src/rate_limit.c | 20 +++++++++----------- 2 files changed, 19 insertions(+), 14 deletions(-) diff --git a/include/ucore/rate_limit.h b/include/ucore/rate_limit.h index 6e9ff42..0437674 100644 --- a/include/ucore/rate_limit.h +++ b/include/ucore/rate_limit.h @@ -54,15 +54,22 @@ struct RateLimit { //internal fields: - //how much each ticket is worth. - long ticket_cost; - //money we have to "buy" tickets long funds; //time of the previos period long last_ts; }; +/** Static initializer for a struct RateLimit + * + * @param tickets number of tickets (bucket depth) + * @param period per this period + */ +#define UC_RATELIMIT_INITIALIZER(tickets, period)\ +{ tickets, period, tickets * period, 0} + + + /** * Initialize a struct RateLimit, which can hand out @tickets per @period * the perioid must be in the same units as the current_ts diff --git a/src/rate_limit.c b/src/rate_limit.c index 9bb51d0..5805967 100644 --- a/src/rate_limit.c +++ b/src/rate_limit.c @@ -5,21 +5,19 @@ void uc_ratelimit_init(struct RateLimit *r, long tickets, long period, long curr { r->tickets = tickets; - r->ticket_cost = period; - r->period = period; - r->funds = r->ticket_cost * tickets; + r->funds = r->period * tickets; r->last_ts = current_ts; assert(tickets > 0); assert(period > 0); - assert(r->ticket_cost > 0); + assert(r->period > 0); assert(r->funds > 0); } void uc_ratelimit_reset(struct RateLimit *r, long current_ts) { - r->funds = r->ticket_cost * r->tickets; + r->funds = r->period * r->tickets; if (current_ts) { r->last_ts = current_ts; @@ -46,20 +44,20 @@ int uc_ratelimit_allow(struct RateLimit *r, long current_ts) //the last time. r->funds += diff_period * r->tickets; - //the below is the real algorithm, with ticket_cost = + //the below is the real algorithm, with period = //tickets * period. //we have optimized this to the expression used above //r->funds += diff_period * - // ((r->tickets * r->ticket_cost) / r->period); + // ((r->tickets * r->period) / r->period); //throttle handing out tickets - if (r->funds > r->ticket_cost * r->tickets) { - r->funds = r->ticket_cost * r->tickets; + if (r->funds > r->period * r->tickets) { + r->funds = r->period * r->tickets; } //If we have enough to buy atleast one ticket, we can allow - if (r->funds >= r->ticket_cost) { - r->funds -= r->ticket_cost; + if (r->funds >= r->period) { + r->funds -= r->period; allowed = 1; } else { //not enough to buy a ticket