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))