|
|
@@ -104,8 +104,17 @@ function buffer.save()
|
|
|
end
|
|
|
|
|
|
function buffer.add(json_event)
|
|
|
+ -- Warn if buffer is getting full
|
|
|
+ if #buffer.events > cfg.max_buffer * 0.8 then
|
|
|
+ log_err("Buffer almost full: " .. #buffer.events .. "/" .. cfg.max_buffer)
|
|
|
+ end
|
|
|
+
|
|
|
table.insert(buffer.events, json_event)
|
|
|
- if #buffer.events > cfg.max_buffer then table.remove(buffer.events, 1) end
|
|
|
+ if #buffer.events > cfg.max_buffer then
|
|
|
+ -- Buffer full - drop oldest to make room
|
|
|
+ table.remove(buffer.events, 1)
|
|
|
+ log_err("Buffer overflow: dropping oldest event")
|
|
|
+ end
|
|
|
buffer.dirty = true
|
|
|
end
|
|
|
|
|
|
@@ -380,6 +389,14 @@ local function co_connect()
|
|
|
local max_delay = 300 -- Max 5 minutes
|
|
|
|
|
|
while true do
|
|
|
+ -- Urgency based on buffer state
|
|
|
+ local buffer_fullness = #buffer.events / cfg.max_buffer
|
|
|
+
|
|
|
+ if buffer_fullness > 0.8 then
|
|
|
+ log_err("Buffer critical at " .. string.format("%.0f%%", buffer_fullness * 100) .. " - aggressive reconnect")
|
|
|
+ retry_delay = 10 -- Aggressive when buffer filling
|
|
|
+ end
|
|
|
+
|
|
|
if not ws.connected then
|
|
|
log_info("Connecting to " .. cfg.server_url .. "...")
|
|
|
|
|
|
@@ -396,7 +413,9 @@ local function co_connect()
|
|
|
retry_delay = math.min(retry_delay * 2, max_delay)
|
|
|
end
|
|
|
else
|
|
|
- coroutine.yield(30) -- Still connected, check every 30s
|
|
|
+ -- Even when connected, periodically flush to clear buffer buildup
|
|
|
+ buffer.flush(function(d) return ws.send(d) end)
|
|
|
+ coroutine.yield(30)
|
|
|
end
|
|
|
end
|
|
|
end
|