Implement idempotency for command handling to skip duplicates and enhance request processing

This commit is contained in:
MayaTheShy
2026-03-22 02:12:50 -04:00
parent bbae2740a7
commit 24683f23a5

View File

@@ -2835,6 +2835,12 @@ local function main()
while true do while true do
local event, side, channel, replyChannel, message, distance = os.pullEvent("modem_message") local event, side, channel, replyChannel, message, distance = os.pullEvent("modem_message")
if channel == ORDER_CHANNEL and type(message) == "table" then if channel == ORDER_CHANNEL and type(message) == "table" then
-- Idempotency: skip duplicate commands
if isCommandDuplicate(message.commandId) then
log.debug("NET", "Duplicate command skipped: %s", tostring(message.commandId))
else
recordCommandId(message.commandId)
cleanupCommandIds()
local handlerOk, handlerErr = pcall(function() local handlerOk, handlerErr = pcall(function()
if message.type == "order" and message.itemName and message.amount then if message.type == "order" and message.itemName and message.amount then
log.info("NET", "Order: %s x%d", message.itemName, message.amount) log.info("NET", "Order: %s x%d", message.itemName, message.amount)
@@ -2849,6 +2855,7 @@ local function main()
pcall(function() pcall(function()
networkModem.transmit(replyChannel, ORDER_CHANNEL, { networkModem.transmit(replyChannel, ORDER_CHANNEL, {
type = "order_result", type = "order_result",
commandId = message.commandId,
success = success, success = success,
message = statusMessage, message = statusMessage,
color = statusColor, color = statusColor,
@@ -2942,6 +2949,7 @@ local function main()
pcall(function() pcall(function()
networkModem.transmit(replyChannel, ORDER_CHANNEL, { networkModem.transmit(replyChannel, ORDER_CHANNEL, {
type = "craft_result", type = "craft_result",
commandId = message.commandId,
success = ok, success = ok,
error = err, error = err,
}) })
@@ -2954,6 +2962,7 @@ local function main()
if not handlerOk then if not handlerOk then
log.error("NET", "Handler error: %s", tostring(handlerErr)) log.error("NET", "Handler error: %s", tostring(handlerErr))
end end
end -- end idempotency else
end end
end end
end end