From 50f5a37a41325f92a367f25bcdf6cd58fd399960 Mon Sep 17 00:00:00 2001 From: Qi <44437200+ADD-SP@users.noreply.github.com> Date: Thu, 13 Jun 2024 13:02:33 +0800 Subject: [PATCH] fix(queue): function `Queue.can_enqueue` should handle non-existing queue instance correctly (#13198) Fixup https://github.com/Kong/kong/pull/13164 KAG-4270 --- kong/tools/queue.lua | 14 ++++++++++++-- spec/01-unit/27-queue_spec.lua | 13 +++++++++++-- 2 files changed, 23 insertions(+), 4 deletions(-) diff --git a/kong/tools/queue.lua b/kong/tools/queue.lua index 725db7ddbf14..dc6a8fb6a108 100644 --- a/kong/tools/queue.lua +++ b/kong/tools/queue.lua @@ -297,8 +297,18 @@ end function Queue.can_enqueue(queue_conf, entry) local queue = queues[_make_queue_key(queue_conf.name)] if not queue then - -- treat non-existing queues as not full as they will be created on demand - return false + -- treat non-existing queues having enough capacity. + -- WARNING: The limitation is that if the `entry` is a string and the `queue.max_bytes` is set, + -- and also the `#entry` is larger than `queue.max_bytes`, + -- this function will incorrectly return `true` instead of `false`. + -- This is a limitation of the current implementation. + -- All capacity checking functions need a Queue instance to work correctly. + -- constructing a Queue instance just for this function is not efficient, + -- so we just return `true` here. + -- This limitation should not happen in normal usage, + -- as user should be aware of the queue capacity settings + -- to avoid such situation. + return true end return _can_enqueue(queue, entry) diff --git a/spec/01-unit/27-queue_spec.lua b/spec/01-unit/27-queue_spec.lua index ec166c295a4e..548be6b42bbe 100644 --- a/spec/01-unit/27-queue_spec.lua +++ b/spec/01-unit/27-queue_spec.lua @@ -812,8 +812,11 @@ describe("plugin queue", function() ) end + -- should be true if the queue does not exist + assert.is_true(Queue.can_enqueue(queue_conf)) + assert.is_false(Queue.is_full(queue_conf)) - assert.is_false(Queue.can_enqueue(queue_conf, "One")) + assert.is_true(Queue.can_enqueue(queue_conf, "One")) enqueue(queue_conf, "One") assert.is_false(Queue.is_full(queue_conf)) @@ -835,8 +838,11 @@ describe("plugin queue", function() max_retry_delay = 60, } + -- should be true if the queue does not exist + assert.is_true(Queue.can_enqueue(queue_conf)) + assert.is_false(Queue.is_full(queue_conf)) - assert.is_false(Queue.can_enqueue(queue_conf, "1")) + assert.is_true(Queue.can_enqueue(queue_conf, "1")) enqueue(queue_conf, "1") assert.is_false(Queue.is_full(queue_conf)) @@ -857,6 +863,9 @@ describe("plugin queue", function() max_retry_delay = 60, } + -- should be true if the queue does not exist + assert.is_true(Queue.can_enqueue(queue_conf)) + enqueue(queue_conf, "1") assert.is_false(Queue.is_full(queue_conf))