Permalink
Browse files

now we recycle the downstream output buffers to save memory and dynam…

…ic allocation times for long-running requests.
  • Loading branch information...
1 parent 11a13f0 commit fc7893d05fc096ceaca1ad3697d0f4d4faadf879 @agentzh agentzh committed Feb 15, 2012
View
@@ -48,6 +48,7 @@ ngx_http_lua_ngx_echo(lua_State *L, unsigned newline)
int nargs;
int type;
const char *msg;
+ ngx_buf_tag_t tag;
lua_getglobal(L, GLOBALS_SYMBOL_REQUEST);
r = lua_touserdata(L, -1);
@@ -137,11 +138,17 @@ ngx_http_lua_ngx_echo(lua_State *L, unsigned newline)
return 0;
}
- b = ngx_create_temp_buf(r->pool, size);
- if (b == NULL) {
+ tag = (ngx_buf_tag_t) &ngx_http_lua_module;
+
+ cl = ngx_http_lua_chains_get_free_buf(r->connection->log, r->pool,
+ &ctx->free_bufs, size, tag);
+
+ if (cl == NULL) {
return luaL_error(L, "out of memory");
}
+ b = cl->buf;
+
for (i = 1; i <= nargs; i++) {
type = lua_type(L, i);
switch (type) {
@@ -194,17 +201,11 @@ ngx_http_lua_ngx_echo(lua_State *L, unsigned newline)
*b->last++ = '\n';
}
+#if 0
if (b->last != b->end) {
return luaL_error(L, "buffer error: %p != %p", b->last, b->end);
}
-
- cl = ngx_alloc_chain_link(r->pool);
- if (cl == NULL) {
- return luaL_error(L, "out of memory");
- }
-
- cl->next = NULL;
- cl->buf = b;
+#endif
ngx_log_debug0(NGX_LOG_DEBUG_HTTP, r->connection->log, 0,
newline ? "lua say response" : "lua print response");
@@ -215,6 +216,22 @@ ngx_http_lua_ngx_echo(lua_State *L, unsigned newline)
return luaL_error(L, "failed to send data through the output filters");
}
+ dd("downstream write: %d, buf len: %d", (int) rc,
+ (int) (b->last - b->pos));
+
+ if (!ctx->out) {
+#if nginx_version >= 1001004
+ ngx_chain_update_chains(r->pool,
+#else
+ ngx_chain_update_chains(
+#endif
+ &ctx->free_bufs, &ctx->busy_bufs, &cl, tag);
+
+ dd("out lua buf tag: %p, buffered: %x, busy bufs: %p",
+ &ngx_http_lua_module, (int) r->connection->buffered,
+ ctx->busy_bufs);
+ }
+
return 0;
}
View
@@ -1293,9 +1293,19 @@ ngx_http_lua_socket_read(ngx_http_request_t *r,
}
}
+#if 0
+ if (rev->active && !rev->ready) {
+ rc = NGX_AGAIN;
+ break;
+ }
+#endif
+
/* try to read the socket */
n = c->recv(c, b->last, size);
+
+ dd("read event ready: %d", (int) c->read->ready);
+
read = 1;
ngx_log_debug3(NGX_LOG_DEBUG_HTTP, r->connection->log, 0,
@@ -3071,6 +3081,11 @@ ngx_http_lua_socket_keepalive_close_handler(ngx_event_t *ev)
goto close;
}
+ dd("read event ready: %d", (int) c->read->ready);
+
+ ngx_log_debug0(NGX_LOG_DEBUG_HTTP, ev->log, 0,
+ "lua socket keepalive close handler check stale events");
+
n = recv(c->fd, buf, 1, MSG_PEEK);
if (n == -1 && ngx_socket_errno == NGX_EAGAIN) {
View
@@ -903,6 +903,7 @@ ngx_http_lua_wev_handler(ngx_http_request_t *r)
ngx_connection_t *c;
ngx_event_t *wev;
ngx_http_core_loc_conf_t *clcf;
+ ngx_chain_t *cl;
ngx_http_lua_socket_upstream_t *u;
@@ -1034,6 +1035,23 @@ ngx_http_lua_wev_handler(ngx_http_request_t *r)
return rc;
}
+ if (ctx->busy_bufs) {
+ cl = NULL;
+
+ dd("updating chains...");
+
+#if nginx_version >= 1001004
+ ngx_chain_update_chains(r->pool,
+#else
+ ngx_chain_update_chains(
+#endif
+ &ctx->free_bufs, &ctx->busy_bufs, &cl,
+ (ngx_buf_tag_t) &ngx_http_lua_module);
+
+ dd("update lua buf tag: %p, buffered: %x, busy bufs: %p",
+ &ngx_http_lua_module, (int) c->buffered, ctx->busy_bufs);
+ }
+
if (c->buffered) {
if (!wev->delayed) {
View
@@ -56,8 +56,11 @@ GET /lua
}
--- request
GET /lua
---- error_code:
---- response_body:
+--- error_log
+attempt to call ngx.exit after sending out the headers
+--- no_error_log
+alert
+--- ignore_response
View
@@ -274,10 +274,11 @@ Fooy: cony1, cony2
}
--- request
GET /lua
---- response_headers
-Content-Type:
---- error_code:
---- response_body:
+--- ignore_response
+--- error_log
+attempt to set ngx.header.HEADER after sending out response headers
+--- no_error_log eval
+["alert", "warn"]
View
@@ -4,15 +4,16 @@ use lib 'lib';
use Test::Nginx::Socket;
#repeat_each(20000);
-#repeat_each(1);
+
repeat_each(2);
+
#master_on();
#workers(1);
#log_level('debug');
#log_level('warn');
#worker_connections(1024);
-plan tests => blocks() * repeat_each() * 2;
+plan tests => repeat_each() * (blocks() * 2);
$ENV{TEST_NGINX_MEMCACHED_PORT} ||= 11211;
$ENV{TEST_NGINX_MYSQL_PORT} ||= 3306;
@@ -60,8 +61,11 @@ GET /lua
}
--- request
GET /lua
---- error_code:
---- response_body:
+--- error_log
+attempt to call ngx.exit after sending out the headers
+--- no_error_log
+[alert]
+--- ignore_response
View
@@ -4,15 +4,15 @@ use lib 'lib';
use Test::Nginx::Socket;
#repeat_each(20000);
-#repeat_each(1);
repeat_each(2);
+
#master_on();
#workers(1);
#log_level('debug');
#log_level('warn');
#worker_connections(1024);
-plan tests => blocks() * repeat_each() * 2;
+plan tests => repeat_each() * (blocks() * 2);
$ENV{TEST_NGINX_MEMCACHED_PORT} ||= 11211;
$ENV{TEST_NGINX_MYSQL_PORT} ||= 3306;
@@ -59,8 +59,11 @@ GET /lua
}
--- request
GET /lua
---- error_code:
---- response_body:
+--- ignore_response
+--- no_error_log
+[alert]
+--- error_log
+attempt to call ngx.exit after sending out the headers
View
@@ -8,9 +8,8 @@ use Test::Nginx::Socket;
#log_level('warn');
repeat_each(2);
-#repeat_each(1);
-plan tests => repeat_each() * (blocks() * 2);
+plan tests => repeat_each() * (blocks() * 2 + 1);
#no_diff();
#no_long_string();
@@ -32,6 +31,8 @@ GET /test
--- response_body
hello, world
hiya
+--- error_log
+lua reuse free buf memory 13 >= 5
@@ -103,6 +104,7 @@ GET /test HTTP/1.0
--- response_body
hello, world
hiya
+--- timeout: 5
View
@@ -33,8 +33,6 @@ __DATA__
test.go(port)
';
}
---- request
-GET /t
--- user_files
>>> test.lua
module("test", package.seeall)
@@ -71,6 +69,8 @@ function go(port)
ngx.say("failed to set reusable: ", err)
end
end
+--- request
+GET /t
--- response_body_like
^connected: 1, reused: \d+
request sent: 11
@@ -304,7 +304,7 @@ received response of 156 bytes
done
--- no_error_log eval
["[error]",
-"lua socket keepalive close handler",
+"lua socket keepalive close handler: fd:",
"lua socket keepalive: free connection pool for "]
--- timeout: 4

0 comments on commit fc7893d

Please sign in to comment.