X-Git-Url: https://git.enpas.org/?a=blobdiff_plain;f=util%2Fqueue.lua;h=203da0e3f6ee590a7dc3ec3631a7a90b5de0fbb9;hb=79a152bb61900a86c486b62d59be60fa845f8afc;hp=afdcaf453e22d7d7898de4e28e6655edce16b0d6;hpb=774d985d6a690539af5086956f2c9fb89c41ad26;p=prosody.git 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;