Mercurial > prosody-hg
comparison 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 |
comparison
equal
deleted
inserted
replaced
| 13993:a8c1dd9154d1 | 13994:8baaefc4db79 |
|---|---|
| 1 -- Represents an outgoing reliable stanza queue | |
| 2 | |
| 1 local queue = require("prosody.util.queue"); | 3 local queue = require("prosody.util.queue"); |
| 2 | 4 |
| 3 local lib = { smqueue = {} } | 5 -- SM queue methods |
| 4 | 6 local smqueue = {}; |
| 5 local smqueue = lib.smqueue; | |
| 6 | 7 |
| 7 function smqueue:push(v) | 8 function smqueue:push(v) |
| 8 self._head = self._head + 1; | 9 self._head = self._head + 1; |
| 9 | 10 |
| 10 assert(self._queue:push(v)); | 11 assert(self._queue:push(v)); |
| 11 end | 12 end |
| 12 | 13 |
| 14 -- Call when receiver has acknowledged some stanzas | |
| 15 -- `h` (sent by the receiver) is the running count of | |
| 16 -- successfully received stanzas | |
| 13 function smqueue:ack(h) | 17 function smqueue:ack(h) |
| 14 if h < self._tail then | 18 if h < self._tail then |
| 19 -- h is less than a previous h | |
| 15 return nil, "tail" | 20 return nil, "tail" |
| 16 elseif h > self._head then | 21 elseif h > self._head then |
| 22 -- h is greater than the number of stanzas we sent | |
| 17 return nil, "head" | 23 return nil, "head" |
| 18 end | 24 end |
| 19 | 25 |
| 20 local acked = {}; | 26 local acked = {}; |
| 21 self._tail = h; | 27 self._tail = h; |
| 23 while expect < self._queue:count() do | 29 while expect < self._queue:count() do |
| 24 local v = self._queue:pop(); | 30 local v = self._queue:pop(); |
| 25 if not v then return nil, "pop" end | 31 if not v then return nil, "pop" end |
| 26 table.insert(acked, v); | 32 table.insert(acked, v); |
| 27 end | 33 end |
| 34 | |
| 28 return acked | 35 return acked |
| 29 end | 36 end |
| 30 | 37 |
| 31 function smqueue:count_unacked() return self._head - self._tail end | 38 function smqueue:count_unacked() return self._head - self._tail end |
| 32 | 39 |
| 42 local t = {}; | 49 local t = {}; |
| 43 for i, v in self:resume() do t[i] = v; end | 50 for i, v in self:resume() do t[i] = v; end |
| 44 return t | 51 return t |
| 45 end | 52 end |
| 46 | 53 |
| 47 local function freeze(q) return { head = q._head; tail = q._tail } end | 54 local smqueue_mt = { |
| 55 __name = "smqueue"; | |
| 56 __index = smqueue; | |
| 57 __len = smqueue.count_unacked; | |
| 58 -- Return a simple object for serialization | |
| 59 __freeze = function (q) return { head = q._head; tail = q._tail } end; | |
| 60 }; | |
| 48 | 61 |
| 49 local queue_mt = { __name = "smqueue"; __index = smqueue; __len = smqueue.count_unacked; __freeze = freeze } | 62 local lib = {}; |
| 50 | 63 |
| 51 function lib.new(size) | 64 function lib.new(size) |
| 52 assert(size > 0); | 65 assert(size > 0); |
| 53 return setmetatable({ _head = 0; _tail = 0; _queue = queue.new(size, true) }, queue_mt) | 66 return setmetatable({ |
| 67 _head = 0; | |
| 68 _tail = 0; | |
| 69 _queue = queue.new(size, true); | |
| 70 }, smqueue_mt) | |
| 54 end | 71 end |
| 55 | 72 |
| 56 return lib | 73 return lib |
