Software /
code /
prosody
Annotate
util/throttle.lua @ 5085:cbc7eb5cfa8c
util.array: Accept an iterator to the array constructor
author | Matthew Wild <mwild1@gmail.com> |
---|---|
date | Wed, 08 Aug 2012 11:49:31 +0100 |
parent | 4952:0e9a5b63206a |
child | 6777:5de6b93d0190 |
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; |
4466
28e0bf9cf0f5
util.throttle: Import setmetatable
Matthew Wild <mwild1@gmail.com>
parents:
4362
diff
changeset
|
3 local setmetatable = setmetatable; |
4952
0e9a5b63206a
util.throttle: floor() internal balance calculation
Matthew Wild <mwild1@gmail.com>
parents:
4467
diff
changeset
|
4 local floor = math.floor; |
4362
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 module "throttle" |
851885cb332d
util.throttle: Generic module by waqas to limit something over some time
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
7 |
851885cb332d
util.throttle: Generic module by waqas to limit something over some time
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
8 local throttle = {}; |
851885cb332d
util.throttle: Generic module by waqas to limit something over some time
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
9 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
|
10 |
851885cb332d
util.throttle: Generic module by waqas to limit something over some time
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
11 function throttle:update() |
851885cb332d
util.throttle: Generic module by waqas to limit something over some time
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
12 local newt = gettime(); |
851885cb332d
util.throttle: Generic module by waqas to limit something over some time
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
13 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
|
14 self.t = newt; |
4952
0e9a5b63206a
util.throttle: floor() internal balance calculation
Matthew Wild <mwild1@gmail.com>
parents:
4467
diff
changeset
|
15 local balance = floor(self.rate * elapsed) + self.balance; |
4362
851885cb332d
util.throttle: Generic module by waqas to limit something over some time
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
16 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
|
17 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
|
18 else |
851885cb332d
util.throttle: Generic module by waqas to limit something over some time
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
19 self.balance = 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 return self.balance; |
851885cb332d
util.throttle: Generic module by waqas to limit something over some time
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
22 end |
851885cb332d
util.throttle: Generic module by waqas to limit something over some time
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
23 |
851885cb332d
util.throttle: Generic module by waqas to limit something over some time
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
24 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
|
25 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
|
26 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
|
27 end |
851885cb332d
util.throttle: Generic module by waqas to limit something over some time
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
28 |
851885cb332d
util.throttle: Generic module by waqas to limit something over some time
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
29 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
|
30 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
|
31 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
|
32 return true; |
851885cb332d
util.throttle: Generic module by waqas to limit something over some time
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
33 else |
851885cb332d
util.throttle: Generic module by waqas to limit something over some time
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
34 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
|
35 if split then |
851885cb332d
util.throttle: Generic module by waqas to limit something over some time
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
36 self.balance = 0; |
851885cb332d
util.throttle: Generic module by waqas to limit something over some time
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
37 end |
4467
fc8a22936b3c
util.throttle: Fix 'outstanding' return value
Matthew Wild <mwild1@gmail.com>
parents:
4466
diff
changeset
|
38 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
|
39 end |
851885cb332d
util.throttle: Generic module by waqas to limit something over some time
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
40 end |
851885cb332d
util.throttle: Generic module by waqas to limit something over some time
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
41 |
851885cb332d
util.throttle: Generic module by waqas to limit something over some time
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
42 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
|
43 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
|
44 end |
851885cb332d
util.throttle: Generic module by waqas to limit something over some time
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
45 |
851885cb332d
util.throttle: Generic module by waqas to limit something over some time
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
46 return _M; |