Annotate

util/throttle.lua @ 4794:25ce7720555f

mod_posix: Support syslog_facility config option
author Matthew Wild <mwild1@gmail.com>
date Thu, 26 Apr 2012 18:14:42 +0100
parent 4470:0209bd5c4259
child 4952:0e9a5b63206a
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
4362
851885cb332d util.throttle: Generic module by waqas to limit something over some time
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
1
851885cb332d util.throttle: Generic module by waqas to limit something over some time
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
2 local gettime = require "socket".gettime;
4469
500735931677 util.throttle: Import setmetatable
Matthew Wild <mwild1@gmail.com>
parents: 4362
diff changeset
3 local setmetatable = setmetatable;
4362
851885cb332d util.throttle: Generic module by waqas to limit something over some time
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
4
851885cb332d util.throttle: Generic module by waqas to limit something over some time
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
5 module "throttle"
851885cb332d util.throttle: Generic module by waqas to limit something over some time
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
6
851885cb332d util.throttle: Generic module by waqas to limit something over some time
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
7 local throttle = {};
851885cb332d util.throttle: Generic module by waqas to limit something over some time
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
8 local throttle_mt = { __index = throttle };
851885cb332d util.throttle: Generic module by waqas to limit something over some time
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
9
851885cb332d util.throttle: Generic module by waqas to limit something over some time
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
10 function throttle:update()
851885cb332d util.throttle: Generic module by waqas to limit something over some time
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
11 local newt = gettime();
851885cb332d util.throttle: Generic module by waqas to limit something over some time
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
12 local elapsed = newt - self.t;
851885cb332d util.throttle: Generic module by waqas to limit something over some time
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
13 self.t = newt;
851885cb332d util.throttle: Generic module by waqas to limit something over some time
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
14 local balance = self.rate * elapsed + self.balance;
851885cb332d util.throttle: Generic module by waqas to limit something over some time
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
15 if balance > self.max then
851885cb332d util.throttle: Generic module by waqas to limit something over some time
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
16 self.balance = self.max;
851885cb332d util.throttle: Generic module by waqas to limit something over some time
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
17 else
851885cb332d util.throttle: Generic module by waqas to limit something over some time
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
18 self.balance = balance;
851885cb332d util.throttle: Generic module by waqas to limit something over some time
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
19 end
851885cb332d util.throttle: Generic module by waqas to limit something over some time
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
20 return self.balance;
851885cb332d util.throttle: Generic module by waqas to limit something over some time
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
21 end
851885cb332d util.throttle: Generic module by waqas to limit something over some time
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
22
851885cb332d util.throttle: Generic module by waqas to limit something over some time
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
23 function throttle:peek(cost)
851885cb332d util.throttle: Generic module by waqas to limit something over some time
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
24 cost = cost or 1;
851885cb332d util.throttle: Generic module by waqas to limit something over some time
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
25 return self.balance >= cost or self:update() >= cost;
851885cb332d util.throttle: Generic module by waqas to limit something over some time
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
26 end
851885cb332d util.throttle: Generic module by waqas to limit something over some time
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
27
851885cb332d util.throttle: Generic module by waqas to limit something over some time
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
28 function throttle:poll(cost, split)
851885cb332d util.throttle: Generic module by waqas to limit something over some time
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
29 if self:peek(cost) then
851885cb332d util.throttle: Generic module by waqas to limit something over some time
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
30 self.balance = self.balance - cost;
851885cb332d util.throttle: Generic module by waqas to limit something over some time
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
31 return true;
851885cb332d util.throttle: Generic module by waqas to limit something over some time
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
32 else
851885cb332d util.throttle: Generic module by waqas to limit something over some time
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
33 local balance = self.balance;
851885cb332d util.throttle: Generic module by waqas to limit something over some time
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
34 if split then
851885cb332d util.throttle: Generic module by waqas to limit something over some time
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
35 self.balance = 0;
851885cb332d util.throttle: Generic module by waqas to limit something over some time
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
36 end
4470
0209bd5c4259 util.throttle: Fix 'outstanding' return value
Matthew Wild <mwild1@gmail.com>
parents: 4469
diff changeset
37 return false, balance, (cost-balance);
4362
851885cb332d util.throttle: Generic module by waqas to limit something over some time
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
38 end
851885cb332d util.throttle: Generic module by waqas to limit something over some time
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
39 end
851885cb332d util.throttle: Generic module by waqas to limit something over some time
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
40
851885cb332d util.throttle: Generic module by waqas to limit something over some time
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
41 function create(max, period)
851885cb332d util.throttle: Generic module by waqas to limit something over some time
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
42 return setmetatable({ rate = max / period, max = max, t = 0, balance = max }, throttle_mt);
851885cb332d util.throttle: Generic module by waqas to limit something over some time
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
43 end
851885cb332d util.throttle: Generic module by waqas to limit something over some time
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
44
851885cb332d util.throttle: Generic module by waqas to limit something over some time
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
45 return _M;