From 44e4c4a70c229491008a501323ece7eac25500e4 Mon Sep 17 00:00:00 2001 From: Gregor Hartmann Date: Sun, 25 Feb 2024 09:00:08 +0100 Subject: [PATCH] Fifo fixes (#3638) --- lua_modules/fifo/fifo.lua | 34 +++++++++++++++++++--------------- lua_modules/fifo/fifosock.lua | 10 +++++++++- 2 files changed, 28 insertions(+), 16 deletions(-) diff --git a/lua_modules/fifo/fifo.lua b/lua_modules/fifo/fifo.lua index d21b1dab..1d2ea165 100644 --- a/lua_modules/fifo/fifo.lua +++ b/lua_modules/fifo/fifo.lua @@ -1,6 +1,6 @@ -- A generic fifo module. See docs/lua-modules/fifo.md for use examples. -local tr, ti = table.remove, table.insert +local tableRemove, tableInsert = table.remove, table.insert -- Remove an element and pass it to k, together with a boolean indicating that -- this is the last element in the queue; if that returns a value, leave that @@ -17,29 +17,33 @@ local tr, ti = table.remove, table.insert -- -- Returns 'true' if the queue contained at least one non-phantom entry, -- 'false' otherwise. -local function dequeue(q,k) - if #q > 0 - then - local new, again = k(q[1], #q == 1) - if new == nil - then tr(q,1) - if again then return dequeue(q, k) end -- note tail call - else q[1] = new - end - return true - else q._go = true ; return false +local function dequeue(q, k) + if #q > 0 then + local new, again = k(q[1], #q == 1) + if new == nil then + tableRemove(q, 1) + else + q[1] = new + end + if again then + return dequeue(q, k) + end -- note tail call + return true + else + q._go = true + return false end end -- Queue a on queue q and dequeue with `k` if the fifo had previously emptied. local function queue(q,a,k) - ti(q,a) + tableInsert(q,a) if k ~= nil and q._go then q._go = false; dequeue(q, k) end end -- return a table containing just the FIFO constructor return { ['new'] = function() - return { ['_go'] = true ; ['queue'] = queue ; ['dequeue'] = dequeue } - end + return { ['_go'] = true ; ['queue'] = queue ; ['dequeue'] = dequeue } + end } diff --git a/lua_modules/fifo/fifosock.lua b/lua_modules/fifo/fifosock.lua index c453fb13..30b0f0c7 100644 --- a/lua_modules/fifo/fifosock.lua +++ b/lua_modules/fifo/fifosock.lua @@ -58,7 +58,15 @@ local function wrap(sock) -- Either that was a function or we've hit our coalescing limit or -- we didn't ship above. Ship now, if there's something to ship. if s ~= nil then - if sslan == 0 then sock:send(s) else sock:send(ssla .. s) end + if sslan == 0 then + if #s > 0 then + sock:send(s) + else + return ns or nil, true + end + else + sock:send(ssla .. s) + end ssla, sslan = nil, 0; gc() return ns or nil, false elseif sslan ~= 0 then