ENV: Debian 8, OpenResty 1.9.3.2, PostgreSQL 9.4, LuaRocks, Pgmoon module.
basic structure:
http {
keepalive_timeout 5;
server{
listen 80;
server_name www.mydomain.net mydomain.net;
location /file {
rewrite_by_lua_block {
local pgmoon = require("pgmoon");
local pg = pgmoon.new({
host = "127.0.0.1",
port = "5432",
database = "mypgdb",
user = "mypgdb",
password = "password"
});
-- do stuff ...
local res = pg:query("select stuff");
local derive = ... -- processing res
-- do more stuff
local res2 = pg:query("select more stuff");
local derive2 = ... -- processing res2
pg:disconnect();
ngx.ctx.derive = derive;
ngx.ctx.derive2 = derive2;
}
header_filter_by_lua_block {
local derive = ngx.ctx.derive;
ngx.header["Set-Cookie"] = "derive=" .. derive .. "; Domain=" .. ngx.var.host .. ";Path=/; Expires=" .. ngx.cookie_time(ngx.time() + 31436000);
}
body_filter_by_lua_block {
local derive2 = ngx.ctx.derive2;
ngx.arg[1] = derive2;
ngx.arg[2] = "./";
}
} # end location
}#end server
}#end http
This used to filter body with my modifications immediately ( 250ms) and send output, including 1 initial call to psql using ngx_postgres module... that included processing the response using basic Lua code inside the blocks.
Now that I've developed a solution that uses multiple calls to db, plus many other mods, my results are constantly held up by the keepalive_timeout. Without explicit keepalive_timeout a remote domain waits (the default) 75 seconds + ~500ms more (code execution time) to receive the body data. Otherwise it matches 500ms + the keepalive time I specify. I want it to be 15, but have set it to 5 (and to 0, to 30 and 60) to confirm my findings. After 50 tests, it always works the same, related to the keepalive_timeout.
Because I've added so much code, and I'm kinda new to OpenResty and Lua (although more familiar with Nginx), I don't know which way to begin troubleshooting.
Since I just now found a post on Nginx forum talking about "Content-Encoding: gzip" header, (
https://forum.nginx.org/read.php?2,170139,209671#msg-209671 ) and the zlib.output_compression being set by PHP.. While I don't use PHP , I am utilizing my own zlib compression via FFI bindings with LuaJIT in my code... so I will begin troubleshooting there.
Interesting because I was using these bindings before adding any Postgres calls, and this did not have such effect. Still, I think that condition sounds like a good fit.
SOOO my questions, based on imagination + ignorance are:
1. Is there some latency issue with data binding when is involved? What about when is passed between Lua blocks?
2. Has someone else solved a similar problem re: content hangs until keepalive_timeout is reached, and can perhaps add general advice here? I know I am not providing a lot of code, but what if, for example, I could manually trigger the end of body conditions.
So I'm just looking for what might be obvious and fundamental- I would imagine many people who are new have encountered similar issues during development.
I will return to post as I'm going to attempt a solution now... in the meantime any help would be appreciated.