Annotate

util/throttle.lua @ 4417:33c149394dcb

migrator/jabberd14: Support for reading jabberd14 spool files.
author Waqas Hussain <waqas20@gmail.com>
date Sat, 19 Nov 2011 21:12:23 +0500
parent 4362:851885cb332d
child 4466:28e0bf9cf0f5
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;
851885cb332d util.throttle: Generic module by waqas to limit something over some time
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
3
851885cb332d util.throttle: Generic module by waqas to limit something over some time
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
4 module "throttle"
851885cb332d util.throttle: Generic module by waqas to limit something over some time
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
5
851885cb332d util.throttle: Generic module by waqas to limit something over some time
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
6 local throttle = {};
851885cb332d util.throttle: Generic module by waqas to limit something over some time
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
7 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
8
851885cb332d util.throttle: Generic module by waqas to limit something over some time
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
9 function throttle:update()
851885cb332d util.throttle: Generic module by waqas to limit something over some time
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
10 local newt = gettime();
851885cb332d util.throttle: Generic module by waqas to limit something over some time
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
11 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
12 self.t = newt;
851885cb332d util.throttle: Generic module by waqas to limit something over some time
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
13 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
14 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
15 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
16 else
851885cb332d util.throttle: Generic module by waqas to limit something over some time
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
17 self.balance = balance;
851885cb332d util.throttle: Generic module by waqas to limit something over some time
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
18 end
851885cb332d util.throttle: Generic module by waqas to limit something over some time
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
19 return self.balance;
851885cb332d util.throttle: Generic module by waqas to limit something over some time
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
20 end
851885cb332d util.throttle: Generic module by waqas to limit something over some time
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
21
851885cb332d util.throttle: Generic module by waqas to limit something over some time
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
22 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
23 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
24 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
25 end
851885cb332d util.throttle: Generic module by waqas to limit something over some time
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
26
851885cb332d util.throttle: Generic module by waqas to limit something over some time
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
27 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
28 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
29 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
30 return true;
851885cb332d util.throttle: Generic module by waqas to limit something over some time
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
31 else
851885cb332d util.throttle: Generic module by waqas to limit something over some time
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
32 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
33 if split then
851885cb332d util.throttle: Generic module by waqas to limit something over some time
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
34 self.balance = 0;
851885cb332d util.throttle: Generic module by waqas to limit something over some time
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
35 end
851885cb332d util.throttle: Generic module by waqas to limit something over some time
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
36 return false, balance, (cost-self.balance);
851885cb332d util.throttle: Generic module by waqas to limit something over some time
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
37 end
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
851885cb332d util.throttle: Generic module by waqas to limit something over some time
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
40 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
41 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
42 end
851885cb332d util.throttle: Generic module by waqas to limit something over some time
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
43
851885cb332d util.throttle: Generic module by waqas to limit something over some time
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
44 return _M;