Skip to content

Commit

Permalink
Wrapping block I/O call in a coroutine
Browse files Browse the repository at this point in the history
  • Loading branch information
tkan145 committed Jan 19, 2024
1 parent 5e05612 commit 2bfc227
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 10 deletions.
2 changes: 1 addition & 1 deletion gateway/src/apicast/http_proxy.lua
Original file line number Diff line number Diff line change
Expand Up @@ -170,7 +170,7 @@ local function forward_https_request(proxy_uri, uri, proxy_opts)
-- set by openresty based on the size of the buffer. However, when the body is rendered
-- to a file, we will need to calculate and manually set the Content-Length header based
-- on the file size
local contentLength, err = file_size(temp_file_path)
local contentLength, err = file_size(temp_file_path)()
if err then
ngx.log(ngx.ERR, "HTTPS proxy: Failed to set content length, err: ", err)
ngx.exit(ngx.HTTP_INTERNAL_SERVER_ERROR)
Expand Down
21 changes: 12 additions & 9 deletions gateway/src/resty/file.lua
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
local co_yield = coroutine._yield
local co_wrap = coroutine._wrap
local open = io.open

local co_wrap_iter = require("resty.coroutines").co_wrap_iter
Expand Down Expand Up @@ -29,19 +30,21 @@ function _M.file_reader(filename)
end

function _M.file_size(filename)
local handle, err = open(filename)
return co_wrap(function()
local handle, err = open(filename)

if err then
return nil, err
end
if err then
return nil, err
end

local current = handle:seek()
local size = handle:seek("end")
local current = handle:seek()
local size = handle:seek("end")

handle:seek("set", current)
handle:close()
handle:seek("set", current)
handle:close()

return size
return size
end)
end

return _M

0 comments on commit 2bfc227

Please sign in to comment.