From db7166181ecdfc87c449816bc249a4dacf0b4557 Mon Sep 17 00:00:00 2001 From: r-alekseev Date: Mon, 23 Oct 2017 00:16:41 +0400 Subject: [PATCH 1/4] perf(response-transformer) accumulate chunks of response using table MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Concatenate strings in buffer every time body_filter occurs when receiving response from an upstream can lead strong overhead. The right way would be to add chunks to a table, and do a concat when eof occurs. Programming in Lua - 11.6 – String Buffers: https://www.lua.org/pil/11.6.html Discussion in kong mailing list: https://groups.google.com/forum/#!topic/konglayer/vWU5Y7Qsb_s --- kong/plugins/response-transformer/handler.lua | 8 +++++--- 1 file changed, 5 insertions(+), 3 deletions(-) diff --git a/kong/plugins/response-transformer/handler.lua b/kong/plugins/response-transformer/handler.lua index 792fe362650d..6a5494b555ad 100644 --- a/kong/plugins/response-transformer/handler.lua +++ b/kong/plugins/response-transformer/handler.lua @@ -14,7 +14,8 @@ end function ResponseTransformerHandler:access(conf) ResponseTransformerHandler.super.access(self) - ngx.ctx.buffer = "" + ngx.ctx.chunks = {} + ngx.ctx.chunk_number = 1 end function ResponseTransformerHandler:header_filter(conf) @@ -28,10 +29,11 @@ function ResponseTransformerHandler:body_filter(conf) if is_body_transform_set(conf) and is_json_body(ngx.header["content-type"]) then local chunk, eof = ngx.arg[1], ngx.arg[2] if eof then - local body = body_filter.transform_json_body(conf, ngx.ctx.buffer) + local body = body_filter.transform_json_body(conf, table.concat(ngx.ctx.chunks)) ngx.arg[1] = body else - ngx.ctx.buffer = ngx.ctx.buffer .. chunk + ngx.ctx.chunks[ngx.ctx.chunk_number] = chunk + ngx.ctx.chunk_number = ngx.ctx.chunk_number + 1 ngx.arg[1] = nil end end From c29eaf16b0e14e60b87f419374a2f3f37f178279 Mon Sep 17 00:00:00 2001 From: r-alekseev Date: Wed, 25 Oct 2017 20:41:12 +0400 Subject: [PATCH 2/4] perf(response-transformer) accumulate chunks of response using table fixing review comments: cache ngx.ctx in local rename context variables to avoid collisions --- kong/plugins/response-transformer/handler.lua | 17 +++++++++++------ 1 file changed, 11 insertions(+), 6 deletions(-) diff --git a/kong/plugins/response-transformer/handler.lua b/kong/plugins/response-transformer/handler.lua index 6a5494b555ad..e35576a49acc 100644 --- a/kong/plugins/response-transformer/handler.lua +++ b/kong/plugins/response-transformer/handler.lua @@ -14,8 +14,11 @@ end function ResponseTransformerHandler:access(conf) ResponseTransformerHandler.super.access(self) - ngx.ctx.chunks = {} - ngx.ctx.chunk_number = 1 + + local ctx = ngx.ctx + + ctx.rt_body_chunks = {} + ctx.rt_body_chunk_number = 1 end function ResponseTransformerHandler:header_filter(conf) @@ -26,14 +29,16 @@ end function ResponseTransformerHandler:body_filter(conf) ResponseTransformerHandler.super.body_filter(self) + local ctx = ngx.ctx + if is_body_transform_set(conf) and is_json_body(ngx.header["content-type"]) then local chunk, eof = ngx.arg[1], ngx.arg[2] if eof then - local body = body_filter.transform_json_body(conf, table.concat(ngx.ctx.chunks)) + local body = body_filter.transform_json_body(conf, table.concat(ctx.rt_body_chunks)) ngx.arg[1] = body else - ngx.ctx.chunks[ngx.ctx.chunk_number] = chunk - ngx.ctx.chunk_number = ngx.ctx.chunk_number + 1 + ctx.rt_body_chunks[ctx.rt_body_chunk_number] = chunk + ctx.rt_body_chunk_number = ctx.rt_body_chunk_number + 1 ngx.arg[1] = nil end end @@ -41,4 +46,4 @@ end ResponseTransformerHandler.PRIORITY = 800 -return ResponseTransformerHandler +return ResponseTransformerHandler \ No newline at end of file From fcb1a223a311399b421d81d5e622724ca3c526f1 Mon Sep 17 00:00:00 2001 From: r-alekseev Date: Thu, 26 Oct 2017 11:36:52 +0400 Subject: [PATCH 3/4] perf(response-transformer) accumulate chunks of response using table * move ctx local closer to usages --- kong/plugins/response-transformer/handler.lua | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/kong/plugins/response-transformer/handler.lua b/kong/plugins/response-transformer/handler.lua index e35576a49acc..28892d221ebc 100644 --- a/kong/plugins/response-transformer/handler.lua +++ b/kong/plugins/response-transformer/handler.lua @@ -29,9 +29,8 @@ end function ResponseTransformerHandler:body_filter(conf) ResponseTransformerHandler.super.body_filter(self) - local ctx = ngx.ctx - if is_body_transform_set(conf) and is_json_body(ngx.header["content-type"]) then + local ctx = ngx.ctx local chunk, eof = ngx.arg[1], ngx.arg[2] if eof then local body = body_filter.transform_json_body(conf, table.concat(ctx.rt_body_chunks)) From 0e41f43e01c1df824e541bcabfee17fce78fd5a5 Mon Sep 17 00:00:00 2001 From: Thijs Schreijer Date: Sun, 29 Oct 2017 02:43:23 +0100 Subject: [PATCH 4/4] minor tweak --- kong/plugins/response-transformer/handler.lua | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/kong/plugins/response-transformer/handler.lua b/kong/plugins/response-transformer/handler.lua index 28892d221ebc..904bc6a6fb10 100644 --- a/kong/plugins/response-transformer/handler.lua +++ b/kong/plugins/response-transformer/handler.lua @@ -4,6 +4,7 @@ local header_filter = require "kong.plugins.response-transformer.header_transfor local is_body_transform_set = header_filter.is_body_transform_set local is_json_body = header_filter.is_json_body +local table_concat = table.concat local ResponseTransformerHandler = BasePlugin:extend() @@ -33,7 +34,7 @@ function ResponseTransformerHandler:body_filter(conf) local ctx = ngx.ctx local chunk, eof = ngx.arg[1], ngx.arg[2] if eof then - local body = body_filter.transform_json_body(conf, table.concat(ctx.rt_body_chunks)) + local body = body_filter.transform_json_body(conf, table_concat(ctx.rt_body_chunks)) ngx.arg[1] = body else ctx.rt_body_chunks[ctx.rt_body_chunk_number] = chunk @@ -45,4 +46,4 @@ end ResponseTransformerHandler.PRIORITY = 800 -return ResponseTransformerHandler \ No newline at end of file +return ResponseTransformerHandler