aboutsummaryrefslogtreecommitdiffstats
path: root/util/queue.lua
diff options
context:
space:
mode:
authorMatthew Wild <mwild1@gmail.com>2015-06-03 15:51:07 +0100
committerMatthew Wild <mwild1@gmail.com>2015-06-03 15:51:07 +0100
commita67c2d33e50a9d154ecf5fef1463279716bba5d6 (patch)
tree9664e2cfdb0606d7206284d9afeb9277705954aa /util/queue.lua
parentbfba36b194c59ab0a54d78e394b2429ab3e4b6fc (diff)
downloadprosody-a67c2d33e50a9d154ecf5fef1463279716bba5d6.tar.gz
prosody-a67c2d33e50a9d154ecf5fef1463279716bba5d6.zip
util.queue: Allow optional wrap-around when pushing, overwriting oldest unread item
Diffstat (limited to 'util/queue.lua')
-rw-r--r--util/queue.lua9
1 files changed, 7 insertions, 2 deletions
diff --git a/util/queue.lua b/util/queue.lua
index afdcaf45..203da0e3 100644
--- a/util/queue.lua
+++ b/util/queue.lua
@@ -11,7 +11,7 @@
local have_utable, utable = pcall(require, "util.table"); -- For pre-allocation of table
-local function new(size)
+local function new(size, allow_wrapping)
-- Head is next insert, tail is next read
local head, tail = 1, 1;
local items = 0; -- Number of stored items
@@ -22,7 +22,12 @@ local function new(size)
count = function (self) return items; end;
push = function (self, item)
if items >= size then
- return nil, "queue full";
+ if allow_wrapping then
+ tail = (tail%size)+1; -- Advance to next oldest item
+ items = items - 1;
+ else
+ return nil, "queue full";
+ end
end
t[head] = item;
items = items + 1;