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(response-transformer) accumulate chunks of response using table #2977

Merged
merged 4 commits into from
Oct 29, 2017
Merged

perf(response-transformer) accumulate chunks of response using table #2977

merged 4 commits into from
Oct 29, 2017

Conversation

r-alekseev
Copy link
Contributor

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

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
Tieske
Tieske previously requested changes Oct 22, 2017
Copy link
Member

@Tieske Tieske left a comment

Choose a reason for hiding this comment

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

good catch!

mind fixing the comments? 👍

@@ -14,7 +14,8 @@ end

function ResponseTransformerHandler:access(conf)
ResponseTransformerHandler.super.access(self)
ngx.ctx.buffer = ""
ngx.ctx.chunks = {}
ngx.ctx.chunk_number = 1
Copy link
Member

Choose a reason for hiding this comment

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

though it also applied on the original code, my comment would be to use a better namespaced name for these variables. The ngx.ctx table is shared for the lifetime of the request and hence has a risk of name colissions, especially with very generic names as chunks.

So maybe ngx.ctx.rt_body_chunks ?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

renamed

Copy link
Contributor Author

@r-alekseev r-alekseev Oct 26, 2017

Choose a reason for hiding this comment

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

By the way, perhaps, better kind of namespacing is to create a separate table for each plugin.
f.e. ctx.response_transformer = { body_chunks = {}, body_chunk_number = 1 }
But this is breaking change and need to be tested hard if plugins get access to shared data.

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
Copy link
Member

Choose a reason for hiding this comment

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

in this code there are a lot of ngx.ctx accesses. Since the lookup of ctx in ngx is rather expensive (it has some meta table magic), it would be better to cache it in a local.

-- at the top
local ctx = ngx.ctx`

and then replace all ngx.ctx.whatever to ctx.whatever

Copy link
Contributor Author

@r-alekseev r-alekseev Oct 25, 2017

Choose a reason for hiding this comment

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

context cached in local variables in both ('access' and 'body_filter') functions

@hishamhm hishamhm added the pr/changes requested Changes were requested to this PR by a maintainer. Please address them and ping back once done. label Oct 23, 2017
@r-alekseev
Copy link
Contributor Author

r-alekseev commented Oct 24, 2017

mind fixing the comments?

Yes, ready to push fixes. Which is more acceptable - add a new commit or replace existing one?

@hishamhm
Copy link
Contributor

@r-alekseev Since this PR is atomic enough so that it will result in a single commit, you can just add new commits and we'll squash-merge them later.

r-alekseev and others added 3 commits October 25, 2017 20:42
fixing review comments:
cache ngx.ctx in local
rename context variables to avoid collisions
@Tieske Tieske dismissed their stale review October 29, 2017 01:44

comments fixed

@Tieske Tieske merged commit 166f880 into Kong:master Oct 29, 2017
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
pr/changes requested Changes were requested to this PR by a maintainer. Please address them and ping back once done.
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants