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

perf(tools): yield when gzip or gunzip #13338

Merged
merged 3 commits into from
Jul 23, 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
3 changes: 3 additions & 0 deletions changelog/unreleased/kong/yield-in-gzip.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
message: Improved latency performace when gzip/gunzip large size data (such as CP/DP config data).
type: performance
scope: Core
10 changes: 10 additions & 0 deletions kong/tools/gzip.lua
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
local buffer = require "string.buffer"
local zlib = require "ffi-zlib"
local yield = require("kong.tools.yield").yield


local inflate_gzip = zlib.inflateGzip
Expand All @@ -15,7 +16,16 @@ local GZIP_CHUNK_SIZE = 65535


local function read_input_buffer(input_buffer)
local count = 0
local yield_size = GZIP_CHUNK_SIZE * 2
Copy link
Contributor

Choose a reason for hiding this comment

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

Is there any consideration to yield for 2 GZIP_CHUNK_SIZE?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

We don't have enough data to support this number, so GZIP_CHUNK_SIZE * 2 is just a estimated value.


return function(size)
count = count + size
if count > yield_size then
count = 0
yield()
end

local data = input_buffer:get(size)
Copy link
Contributor

Choose a reason for hiding this comment

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

I am thinking whether input_buffer:get() itself can yield if the input_buffer is set to a socket. If it is a yieldable socket, then the new added yield() seems redundant.

Because according to the buffer:set() in https://luajit.org/ext_buffer.html, it supports string and cdata. Is it possible if the buffer is set to a cdata type, which is a socket?

Copy link
Contributor Author

@chronolaw chronolaw Jul 8, 2024

Choose a reason for hiding this comment

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

Currently (in kong gatway), we are using gzip/gunzip only for string data (CP/DP config), so far as I know no socket to be applied to gzip/gunzip.

return data ~= "" and data or nil
end
Expand Down
Loading