Skip to content

Commit

Permalink
refactor(plugins/grpc-gateway): use string buffer to concat strings (#…
Browse files Browse the repository at this point in the history
  • Loading branch information
chronolaw authored Sep 21, 2023
1 parent 83721e6 commit b75b103
Showing 1 changed file with 6 additions and 5 deletions.
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

1 comment on commit b75b103

@khcp-gha-bot
Copy link

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:b75b103146db7e6704f21a169aca8278f9c0553c
Artifacts available https://github.com/Kong/kong/actions/runs/6258731557

Please sign in to comment.