Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix(queue): function Queue.can_enqueue should handle non-existing queue instance correctly #13198

Merged
merged 2 commits into from
Jun 13, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 12 additions & 2 deletions kong/tools/queue.lua
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
13 changes: 11 additions & 2 deletions spec/01-unit/27-queue_spec.lua
Original file line number Diff line number Diff line change
Expand Up @@ -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))

Expand All @@ -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))

Expand All @@ -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))
Expand Down
Loading