Skip to content

Commit

Permalink
fix(queue): function Queue.can_enqueue should handle non-existing …
Browse files Browse the repository at this point in the history
…queue instance correctly (#13198)

Fixup #13164

KAG-4270
  • Loading branch information
ADD-SP authored Jun 13, 2024
1 parent fbadd5a commit 50f5a37
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 4 deletions.
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

1 comment on commit 50f5a37

@github-actions
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Bazel Build

Docker image available kong/kong:50f5a37a41325f92a367f25bcdf6cd58fd399960
Artifacts available https://github.com/Kong/kong/actions/runs/9493987898

Please sign in to comment.