Remove ticket_cost member as it is no longer needed

This commit is contained in:
Nils O. Selåsdal
2013-10-15 22:31:57 +02:00
parent 0a36c669c6
commit 934088ca4a
2 changed files with 19 additions and 14 deletions
+10 -3
View File
@@ -54,15 +54,22 @@ struct RateLimit {
//internal fields: //internal fields:
//how much each ticket is worth.
long ticket_cost;
//money we have to "buy" tickets //money we have to "buy" tickets
long funds; long funds;
//time of the previos period //time of the previos period
long last_ts; 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 * Initialize a struct RateLimit, which can hand out @tickets per @period
* the perioid must be in the same units as the current_ts * the perioid must be in the same units as the current_ts
+9 -11
View File
@@ -5,21 +5,19 @@ void uc_ratelimit_init(struct RateLimit *r, long tickets, long period, long curr
{ {
r->tickets = tickets; r->tickets = tickets;
r->ticket_cost = period;
r->period = period; r->period = period;
r->funds = r->ticket_cost * tickets; r->funds = r->period * tickets;
r->last_ts = current_ts; r->last_ts = current_ts;
assert(tickets > 0); assert(tickets > 0);
assert(period > 0); assert(period > 0);
assert(r->ticket_cost > 0); assert(r->period > 0);
assert(r->funds > 0); assert(r->funds > 0);
} }
void uc_ratelimit_reset(struct RateLimit *r, long current_ts) 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) { if (current_ts) {
r->last_ts = current_ts; r->last_ts = current_ts;
@@ -46,20 +44,20 @@ int uc_ratelimit_allow(struct RateLimit *r, long current_ts)
//the last time. //the last time.
r->funds += diff_period * r->tickets; 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. //tickets * period.
//we have optimized this to the expression used above //we have optimized this to the expression used above
//r->funds += diff_period * //r->funds += diff_period *
// ((r->tickets * r->ticket_cost) / r->period); // ((r->tickets * r->period) / r->period);
//throttle handing out tickets //throttle handing out tickets
if (r->funds > r->ticket_cost * r->tickets) { if (r->funds > r->period * r->tickets) {
r->funds = r->ticket_cost * r->tickets; r->funds = r->period * r->tickets;
} }
//If we have enough to buy atleast one ticket, we can allow //If we have enough to buy atleast one ticket, we can allow
if (r->funds >= r->ticket_cost) { if (r->funds >= r->period) {
r->funds -= r->ticket_cost; r->funds -= r->period;
allowed = 1; allowed = 1;
} else { } else {
//not enough to buy a ticket //not enough to buy a ticket