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

refactor(plugins/grpc-gateway): use string buffer to concat strings #11602

Merged
merged 2 commits into from
Sep 21, 2023
Merged
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
11 changes: 6 additions & 5 deletions kong/plugins/grpc-gateway/deco.lua
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
-- Copyright (c) Kong Inc. 2020

local cjson = require "cjson"
local buffer = require "string.buffer"
local pb = require "pb"
local grpc_tools = require "kong.tools.grpc"
local grpc_frame = grpc_tools.frame
Expand All @@ -14,6 +15,7 @@ local re_match = ngx.re.match
local re_gmatch = ngx.re.gmatch

local encode_json = cjson.encode
local decode_json = cjson.decode
local pcall = pcall

local deco = {}
Expand Down Expand Up @@ -225,7 +227,7 @@ function deco:upstream(body)
local body_variable = self.endpoint.body_variable
if body_variable then
if body and #body > 0 then
local body_decoded = cjson.decode(body)
local body_decoded = decode_json(body)
if body_variable ~= "*" then
--[[
// For HTTP methods that allow a request body, the `body` field
Expand Down Expand Up @@ -285,19 +287,18 @@ end
function deco:downstream(chunk)
local body = (self.downstream_body or "") .. chunk

local out, n = {}, 1
local out = buffer.new()
local msg, body = grpc_unframe(body)

while msg do
msg = encode_json(pb.decode(self.endpoint.output_type, msg))

out[n] = msg
n = n + 1
out:put(msg)
msg, body = grpc_unframe(body)
end

self.downstream_body = body
chunk = table.concat(out)
chunk = out:get()

return chunk
end
Expand Down