annotate util/smqueue.lua @ 13994:8baaefc4db79

util.smqueue: Minor reformatting for code style, add some comments The `smqueue` table (of methods) is no longer exported, as I couldn't see it being used anywhere and it is unconventional (only new() is exported, and methods should be called on the objects returned from that).
author Matthew Wild <mwild1@gmail.com>
date Thu, 04 Dec 2025 11:39:34 +0000
parents d10957394a3c
children df970d2dac45
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
13994
8baaefc4db79 util.smqueue: Minor reformatting for code style, add some comments
Matthew Wild <mwild1@gmail.com>
parents: 12975
diff changeset
1 -- Represents an outgoing reliable stanza queue
8baaefc4db79 util.smqueue: Minor reformatting for code style, add some comments
Matthew Wild <mwild1@gmail.com>
parents: 12975
diff changeset
2
12975
d10957394a3c util: Prefix module imports with prosody namespace
Kim Alvefur <zash@zash.se>
parents: 12058
diff changeset
3 local queue = require("prosody.util.queue");
12055
daced16154fa util.smqueue: Abstract queue with acknowledgements and overflow
Kim Alvefur <zash@zash.se>
parents:
diff changeset
4
13994
8baaefc4db79 util.smqueue: Minor reformatting for code style, add some comments
Matthew Wild <mwild1@gmail.com>
parents: 12975
diff changeset
5 -- SM queue methods
8baaefc4db79 util.smqueue: Minor reformatting for code style, add some comments
Matthew Wild <mwild1@gmail.com>
parents: 12975
diff changeset
6 local smqueue = {};
12055
daced16154fa util.smqueue: Abstract queue with acknowledgements and overflow
Kim Alvefur <zash@zash.se>
parents:
diff changeset
7
daced16154fa util.smqueue: Abstract queue with acknowledgements and overflow
Kim Alvefur <zash@zash.se>
parents:
diff changeset
8 function smqueue:push(v)
daced16154fa util.smqueue: Abstract queue with acknowledgements and overflow
Kim Alvefur <zash@zash.se>
parents:
diff changeset
9 self._head = self._head + 1;
daced16154fa util.smqueue: Abstract queue with acknowledgements and overflow
Kim Alvefur <zash@zash.se>
parents:
diff changeset
10
daced16154fa util.smqueue: Abstract queue with acknowledgements and overflow
Kim Alvefur <zash@zash.se>
parents:
diff changeset
11 assert(self._queue:push(v));
daced16154fa util.smqueue: Abstract queue with acknowledgements and overflow
Kim Alvefur <zash@zash.se>
parents:
diff changeset
12 end
daced16154fa util.smqueue: Abstract queue with acknowledgements and overflow
Kim Alvefur <zash@zash.se>
parents:
diff changeset
13
13994
8baaefc4db79 util.smqueue: Minor reformatting for code style, add some comments
Matthew Wild <mwild1@gmail.com>
parents: 12975
diff changeset
14 -- Call when receiver has acknowledged some stanzas
8baaefc4db79 util.smqueue: Minor reformatting for code style, add some comments
Matthew Wild <mwild1@gmail.com>
parents: 12975
diff changeset
15 -- `h` (sent by the receiver) is the running count of
8baaefc4db79 util.smqueue: Minor reformatting for code style, add some comments
Matthew Wild <mwild1@gmail.com>
parents: 12975
diff changeset
16 -- successfully received stanzas
12055
daced16154fa util.smqueue: Abstract queue with acknowledgements and overflow
Kim Alvefur <zash@zash.se>
parents:
diff changeset
17 function smqueue:ack(h)
daced16154fa util.smqueue: Abstract queue with acknowledgements and overflow
Kim Alvefur <zash@zash.se>
parents:
diff changeset
18 if h < self._tail then
13994
8baaefc4db79 util.smqueue: Minor reformatting for code style, add some comments
Matthew Wild <mwild1@gmail.com>
parents: 12975
diff changeset
19 -- h is less than a previous h
12055
daced16154fa util.smqueue: Abstract queue with acknowledgements and overflow
Kim Alvefur <zash@zash.se>
parents:
diff changeset
20 return nil, "tail"
daced16154fa util.smqueue: Abstract queue with acknowledgements and overflow
Kim Alvefur <zash@zash.se>
parents:
diff changeset
21 elseif h > self._head then
13994
8baaefc4db79 util.smqueue: Minor reformatting for code style, add some comments
Matthew Wild <mwild1@gmail.com>
parents: 12975
diff changeset
22 -- h is greater than the number of stanzas we sent
12055
daced16154fa util.smqueue: Abstract queue with acknowledgements and overflow
Kim Alvefur <zash@zash.se>
parents:
diff changeset
23 return nil, "head"
daced16154fa util.smqueue: Abstract queue with acknowledgements and overflow
Kim Alvefur <zash@zash.se>
parents:
diff changeset
24 end
daced16154fa util.smqueue: Abstract queue with acknowledgements and overflow
Kim Alvefur <zash@zash.se>
parents:
diff changeset
25
daced16154fa util.smqueue: Abstract queue with acknowledgements and overflow
Kim Alvefur <zash@zash.se>
parents:
diff changeset
26 local acked = {};
daced16154fa util.smqueue: Abstract queue with acknowledgements and overflow
Kim Alvefur <zash@zash.se>
parents:
diff changeset
27 self._tail = h;
daced16154fa util.smqueue: Abstract queue with acknowledgements and overflow
Kim Alvefur <zash@zash.se>
parents:
diff changeset
28 local expect = self._head - self._tail;
daced16154fa util.smqueue: Abstract queue with acknowledgements and overflow
Kim Alvefur <zash@zash.se>
parents:
diff changeset
29 while expect < self._queue:count() do
daced16154fa util.smqueue: Abstract queue with acknowledgements and overflow
Kim Alvefur <zash@zash.se>
parents:
diff changeset
30 local v = self._queue:pop();
daced16154fa util.smqueue: Abstract queue with acknowledgements and overflow
Kim Alvefur <zash@zash.se>
parents:
diff changeset
31 if not v then return nil, "pop" end
daced16154fa util.smqueue: Abstract queue with acknowledgements and overflow
Kim Alvefur <zash@zash.se>
parents:
diff changeset
32 table.insert(acked, v);
daced16154fa util.smqueue: Abstract queue with acknowledgements and overflow
Kim Alvefur <zash@zash.se>
parents:
diff changeset
33 end
13994
8baaefc4db79 util.smqueue: Minor reformatting for code style, add some comments
Matthew Wild <mwild1@gmail.com>
parents: 12975
diff changeset
34
12055
daced16154fa util.smqueue: Abstract queue with acknowledgements and overflow
Kim Alvefur <zash@zash.se>
parents:
diff changeset
35 return acked
daced16154fa util.smqueue: Abstract queue with acknowledgements and overflow
Kim Alvefur <zash@zash.se>
parents:
diff changeset
36 end
daced16154fa util.smqueue: Abstract queue with acknowledgements and overflow
Kim Alvefur <zash@zash.se>
parents:
diff changeset
37
daced16154fa util.smqueue: Abstract queue with acknowledgements and overflow
Kim Alvefur <zash@zash.se>
parents:
diff changeset
38 function smqueue:count_unacked() return self._head - self._tail end
daced16154fa util.smqueue: Abstract queue with acknowledgements and overflow
Kim Alvefur <zash@zash.se>
parents:
diff changeset
39
daced16154fa util.smqueue: Abstract queue with acknowledgements and overflow
Kim Alvefur <zash@zash.se>
parents:
diff changeset
40 function smqueue:count_acked() return self._tail end
daced16154fa util.smqueue: Abstract queue with acknowledgements and overflow
Kim Alvefur <zash@zash.se>
parents:
diff changeset
41
daced16154fa util.smqueue: Abstract queue with acknowledgements and overflow
Kim Alvefur <zash@zash.se>
parents:
diff changeset
42 function smqueue:resumable() return self._queue:count() >= (self._head - self._tail) end
daced16154fa util.smqueue: Abstract queue with acknowledgements and overflow
Kim Alvefur <zash@zash.se>
parents:
diff changeset
43
daced16154fa util.smqueue: Abstract queue with acknowledgements and overflow
Kim Alvefur <zash@zash.se>
parents:
diff changeset
44 function smqueue:resume() return self._queue:items() end
daced16154fa util.smqueue: Abstract queue with acknowledgements and overflow
Kim Alvefur <zash@zash.se>
parents:
diff changeset
45
daced16154fa util.smqueue: Abstract queue with acknowledgements and overflow
Kim Alvefur <zash@zash.se>
parents:
diff changeset
46 function smqueue:consume() return self._queue:consume() end
daced16154fa util.smqueue: Abstract queue with acknowledgements and overflow
Kim Alvefur <zash@zash.se>
parents:
diff changeset
47
12058
4860da718e87 util.smqueue: Simplify compat table, fix dependent modules (thanks Martin)
Kim Alvefur <zash@zash.se>
parents: 12055
diff changeset
48 function smqueue:table()
4860da718e87 util.smqueue: Simplify compat table, fix dependent modules (thanks Martin)
Kim Alvefur <zash@zash.se>
parents: 12055
diff changeset
49 local t = {};
4860da718e87 util.smqueue: Simplify compat table, fix dependent modules (thanks Martin)
Kim Alvefur <zash@zash.se>
parents: 12055
diff changeset
50 for i, v in self:resume() do t[i] = v; end
4860da718e87 util.smqueue: Simplify compat table, fix dependent modules (thanks Martin)
Kim Alvefur <zash@zash.se>
parents: 12055
diff changeset
51 return t
12055
daced16154fa util.smqueue: Abstract queue with acknowledgements and overflow
Kim Alvefur <zash@zash.se>
parents:
diff changeset
52 end
daced16154fa util.smqueue: Abstract queue with acknowledgements and overflow
Kim Alvefur <zash@zash.se>
parents:
diff changeset
53
13994
8baaefc4db79 util.smqueue: Minor reformatting for code style, add some comments
Matthew Wild <mwild1@gmail.com>
parents: 12975
diff changeset
54 local smqueue_mt = {
8baaefc4db79 util.smqueue: Minor reformatting for code style, add some comments
Matthew Wild <mwild1@gmail.com>
parents: 12975
diff changeset
55 __name = "smqueue";
8baaefc4db79 util.smqueue: Minor reformatting for code style, add some comments
Matthew Wild <mwild1@gmail.com>
parents: 12975
diff changeset
56 __index = smqueue;
8baaefc4db79 util.smqueue: Minor reformatting for code style, add some comments
Matthew Wild <mwild1@gmail.com>
parents: 12975
diff changeset
57 __len = smqueue.count_unacked;
8baaefc4db79 util.smqueue: Minor reformatting for code style, add some comments
Matthew Wild <mwild1@gmail.com>
parents: 12975
diff changeset
58 -- Return a simple object for serialization
8baaefc4db79 util.smqueue: Minor reformatting for code style, add some comments
Matthew Wild <mwild1@gmail.com>
parents: 12975
diff changeset
59 __freeze = function (q) return { head = q._head; tail = q._tail } end;
8baaefc4db79 util.smqueue: Minor reformatting for code style, add some comments
Matthew Wild <mwild1@gmail.com>
parents: 12975
diff changeset
60 };
12055
daced16154fa util.smqueue: Abstract queue with acknowledgements and overflow
Kim Alvefur <zash@zash.se>
parents:
diff changeset
61
13994
8baaefc4db79 util.smqueue: Minor reformatting for code style, add some comments
Matthew Wild <mwild1@gmail.com>
parents: 12975
diff changeset
62 local lib = {};
12055
daced16154fa util.smqueue: Abstract queue with acknowledgements and overflow
Kim Alvefur <zash@zash.se>
parents:
diff changeset
63
daced16154fa util.smqueue: Abstract queue with acknowledgements and overflow
Kim Alvefur <zash@zash.se>
parents:
diff changeset
64 function lib.new(size)
daced16154fa util.smqueue: Abstract queue with acknowledgements and overflow
Kim Alvefur <zash@zash.se>
parents:
diff changeset
65 assert(size > 0);
13994
8baaefc4db79 util.smqueue: Minor reformatting for code style, add some comments
Matthew Wild <mwild1@gmail.com>
parents: 12975
diff changeset
66 return setmetatable({
8baaefc4db79 util.smqueue: Minor reformatting for code style, add some comments
Matthew Wild <mwild1@gmail.com>
parents: 12975
diff changeset
67 _head = 0;
8baaefc4db79 util.smqueue: Minor reformatting for code style, add some comments
Matthew Wild <mwild1@gmail.com>
parents: 12975
diff changeset
68 _tail = 0;
8baaefc4db79 util.smqueue: Minor reformatting for code style, add some comments
Matthew Wild <mwild1@gmail.com>
parents: 12975
diff changeset
69 _queue = queue.new(size, true);
8baaefc4db79 util.smqueue: Minor reformatting for code style, add some comments
Matthew Wild <mwild1@gmail.com>
parents: 12975
diff changeset
70 }, smqueue_mt)
12055
daced16154fa util.smqueue: Abstract queue with acknowledgements and overflow
Kim Alvefur <zash@zash.se>
parents:
diff changeset
71 end
daced16154fa util.smqueue: Abstract queue with acknowledgements and overflow
Kim Alvefur <zash@zash.se>
parents:
diff changeset
72
daced16154fa util.smqueue: Abstract queue with acknowledgements and overflow
Kim Alvefur <zash@zash.se>
parents:
diff changeset
73 return lib