Software /
code /
prosody
Annotate
util/throttle.lua @ 8791:8da11142fabf
muc: Allow clients to change multiple affiliations or roles at once (#345)
According to XEP-0045 sections 9.2, 9.5 and 9.8 affiliation lists and role
lists should allow mass-modification. Prosody however would just use the
first entry of the list and ignore the rest. This is fixed by introducing
a `for` loop to `set` stanzas of the respective `muc#admin` namespace.
In order for this loop to work, the error handling was changed a little.
Prosody no longer returns after the first error. Instead, an error reply
is sent for each malformed or otherwise wrong entry, but the loop keeps
going over the other entries. This may lead to multiple error messages
being sent for one client request. A notable exception from this is when
the XML Schema for `muc#admin` requests is violated. In that case the loop
is aborted with an error message to the client.
The change is a bit bigger than that in order to have the loop only for
`set` stanzas without changing the behaviour of the `get` stanzas. This is
now more in line with trunk, where there are separate methods for each
stanza type.
References: #345
author | Lennart Sauerbeck <devel@lennart.sauerbeck.org> |
---|---|
date | Sat, 18 Mar 2017 18:47:28 +0100 |
parent | 8272:64165865b131 |
child | 8555:4f0f5b49bb03 |
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 |
7988
dc758422d896
util.statistics,statsd,throttle,timer: Replace dependency on LuaSockect with util.time for precision time
Kim Alvefur <zash@zash.se>
parents:
6777
diff
changeset
|
2 local gettime = require "util.time".now |
4466
28e0bf9cf0f5
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 |
6777
5de6b93d0190
util.*: Remove use of module() function, make all module functions local and return them in a table at the end
Kim Alvefur <zash@zash.se>
parents:
4952
diff
changeset
|
5 local _ENV = nil; |
4362
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; |
8254
e3f7b6fa46ba
util.throttle: Fix initial time setting (double accounting the first time) and fractional balance updates (0.1*10 was not the same as 1*1)
Waqas Hussain <waqas20@gmail.com>
parents:
7988
diff
changeset
|
14 local balance = (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
|
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 |
4467
fc8a22936b3c
util.throttle: Fix 'outstanding' return value
Matthew Wild <mwild1@gmail.com>
parents:
4466
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 |
6777
5de6b93d0190
util.*: Remove use of module() function, make all module functions local and return them in a table at the end
Kim Alvefur <zash@zash.se>
parents:
4952
diff
changeset
|
41 local function create(max, period) |
8254
e3f7b6fa46ba
util.throttle: Fix initial time setting (double accounting the first time) and fractional balance updates (0.1*10 was not the same as 1*1)
Waqas Hussain <waqas20@gmail.com>
parents:
7988
diff
changeset
|
42 return setmetatable({ rate = max / period, max = max, t = gettime(), balance = max }, throttle_mt); |
4362
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 |
6777
5de6b93d0190
util.*: Remove use of module() function, make all module functions local and return them in a table at the end
Kim Alvefur <zash@zash.se>
parents:
4952
diff
changeset
|
45 return { |
5de6b93d0190
util.*: Remove use of module() function, make all module functions local and return them in a table at the end
Kim Alvefur <zash@zash.se>
parents:
4952
diff
changeset
|
46 create = create; |
5de6b93d0190
util.*: Remove use of module() function, make all module functions local and return them in a table at the end
Kim Alvefur <zash@zash.se>
parents:
4952
diff
changeset
|
47 }; |