From 4c8c7158a4663b40d25daeae2c46e09e137c6b4a Mon Sep 17 00:00:00 2001 From: Qi Date: Thu, 13 Jun 2024 02:52:56 +0000 Subject: [PATCH 1/2] fix(queue): function `Queue.can_enqueue` should handle non-existing queue instance correctly --- 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..82359ede0b88 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 needs a Queue instance to work correctly. + -- consturcting 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)) From b63ddbbb4ce3cdbc32e92181e1368c144ebfe3f7 Mon Sep 17 00:00:00 2001 From: Qi Date: Thu, 13 Jun 2024 03:43:33 +0000 Subject: [PATCH 2/2] fixup! fix(queue): function `Queue.can_enqueue` should handle non-existing queue instance correctly --- kong/tools/queue.lua | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/kong/tools/queue.lua b/kong/tools/queue.lua index 82359ede0b88..dc6a8fb6a108 100644 --- a/kong/tools/queue.lua +++ b/kong/tools/queue.lua @@ -302,8 +302,8 @@ function Queue.can_enqueue(queue_conf, entry) -- 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 needs a Queue instance to work correctly. - -- consturcting a Queue instance just for this function is not efficient, + -- 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