From 6b32ac086e1fa26dcbd46c56432d6a080bde37fd Mon Sep 17 00:00:00 2001 From: Roberto De Ioris Date: Sun, 7 Oct 2012 08:45:50 +0200 Subject: [PATCH] improved lua plugin performance and status management --- core/daemons.c | 14 ++++-- plugins/lua/lua_plugin.c | 87 ++++++++++++-------------------------- plugins/lua/uwsgiplugin.py | 2 +- 3 files changed, 39 insertions(+), 64 deletions(-) diff --git a/core/daemons.c b/core/daemons.c index a7527f48..7d26028d 100644 --- a/core/daemons.c +++ b/core/daemons.c @@ -215,11 +215,13 @@ void uwsgi_spawn_daemon(struct uwsgi_daemon *ud) { exit(1); } + if (!ud->pidfile) { #ifdef __linux__ - if (prctl(PR_SET_PDEATHSIG, SIGKILL, 0, 0, 0)) { - uwsgi_error("prctl()"); - } + if (prctl(PR_SET_PDEATHSIG, SIGKILL, 0, 0, 0)) { + uwsgi_error("prctl()"); + } #endif + } if (throttle) { @@ -256,6 +258,12 @@ void uwsgi_opt_add_daemon(char *opt, char *value, void *none) { } *space = 0; pidfile = command; + // check for freq + char *comma = strchr(pidfile, ','); + if (comma) { + *comma = 0; + freq = atoi(comma+1); + } command = space + 1; if (!strcmp(opt, "smart-attach-daemon2")) { daemonize = 1; diff --git a/plugins/lua/lua_plugin.c b/plugins/lua/lua_plugin.c index 3f7918d4..2cd27bb5 100644 --- a/plugins/lua/lua_plugin.c +++ b/plugins/lua/lua_plugin.c @@ -13,6 +13,8 @@ struct uwsgi_lua { } ulua; #define lca(L, n) ulua_check_args(L, __FUNCTION__, n) +#define response_append(x, y) if (uwsgi_buffer_append(status_and_headers, x, y)) { uwsgi_buffer_destroy(status_and_headers); lua_pushvalue(L, -1); goto clear;} +#define response_append_header(x, y) if (uwsgi_buffer_append(status_and_headers, x, y)) { uwsgi_buffer_destroy(status_and_headers); lua_pop(L, 2); lua_pushvalue(L, -1); goto clear;} struct uwsgi_option uwsgi_lua_options[] = { @@ -391,7 +393,6 @@ void uwsgi_lua_app() { int uwsgi_lua_request(struct wsgi_request *wsgi_req) { int i; - int raw; const char *http; size_t slen; ssize_t rlen; @@ -467,38 +468,24 @@ int uwsgi_lua_request(struct wsgi_request *wsgi_req) { //uwsgi_log("%d %s %s %s\n",i,lua_typename(L, lua_type(L, -3)), lua_typename(L, lua_type(L, -2)) , lua_typename(L, lua_type(L, -1))); - raw = 0; + // this buffer will contains the whole headers (+status) + struct uwsgi_buffer *status_and_headers = uwsgi_buffer_new(4096); + // send status if (lua_type(L, -3) == LUA_TSTRING || lua_type(L, -3) == LUA_TNUMBER) { http = lua_tolstring(L, -3, &slen); - if ( (rlen = wsgi_req->socket->proto_write_header(wsgi_req, wsgi_req->protocol, wsgi_req->protocol_len)) != wsgi_req->protocol_len) { - lua_pushvalue(L, -1); - goto clear; - } - wsgi_req->headers_size += rlen; - if ( (rlen = wsgi_req->socket->proto_write_header(wsgi_req, " ", 1)) != 1) { - lua_pushvalue(L, -1); - goto clear; - } - wsgi_req->headers_size += rlen; - if ( (rlen = wsgi_req->socket->proto_write_header(wsgi_req, (char *)http, slen)) != (ssize_t) slen) { - lua_pushvalue(L, -1); - goto clear; - } - wsgi_req->headers_size += rlen; - // a performance hack - ptrbuf = (char *) http; - ptrbuf[3] = 0; - wsgi_req->status = atoi(ptrbuf); - if ( (rlen = wsgi_req->socket->proto_write_header(wsgi_req, "\r\n", 2)) != 2) { - lua_pushvalue(L, -1); - goto clear; - } - wsgi_req->headers_size += rlen; + + response_append(wsgi_req->protocol, wsgi_req->protocol_len); + response_append(" ", 1); + response_append((char *) http, slen); + response_append("\r\n", 2); + + // transform the first 3 bytes of the string in a number + wsgi_req->status = uwsgi_str3_num((char *)http); } else { - raw = 1; - wsgi_req->status = -1; + uwsgi_log("[uwsgi-lua] invalid response status !!!\n"); + // let's continue } // send headers @@ -506,42 +493,23 @@ int uwsgi_lua_request(struct wsgi_request *wsgi_req) { lua_pushnil(L); while(lua_next(L, -3) != 0) { http = lua_tolstring(L, -2, &slen); - if ( (rlen = wsgi_req->socket->proto_write_header(wsgi_req, (char *)http, slen)) != (ssize_t) slen) { - lua_pop(L, 2); - lua_pushvalue(L, -1); - goto clear; - } - wsgi_req->headers_size += rlen; - if ( (rlen = wsgi_req->socket->proto_write_header(wsgi_req, ": ", 2)) != 2) { - lua_pop(L, 2); - lua_pushvalue(L, -1); - goto clear; - } - wsgi_req->headers_size += rlen; + + response_append_header((char *)http, slen); + response_append_header(": ", 2); + http = lua_tolstring(L, -1, &slen); - if ( (rlen = wsgi_req->socket->proto_write_header(wsgi_req, (char *)http, slen)) != (ssize_t) slen) { - lua_pop(L, 2); - lua_pushvalue(L, -1); - goto clear; - } - wsgi_req->headers_size += rlen; - if ( (rlen = wsgi_req->socket->proto_write_header(wsgi_req, "\r\n", 2)) != 2) { - lua_pop(L, 2); - lua_pushvalue(L, -1); - goto clear; - } - wsgi_req->headers_size += rlen; + + response_append_header((char *)http, slen); + response_append_header("\r\n", 2); + lua_pop(L, 1); wsgi_req->header_cnt++; } - if (!raw) { - if ( (rlen = wsgi_req->socket->proto_write_header(wsgi_req, "\r\n", 2)) != 2) { - lua_pushvalue(L, -1); - goto clear; - } - wsgi_req->headers_size += rlen; - } + response_append("\r\n", 2); + + wsgi_req->headers_size = wsgi_req->socket->proto_write_header(wsgi_req, status_and_headers->buf, status_and_headers->pos); + uwsgi_buffer_destroy(status_and_headers); // send body with coroutine lua_pushvalue(L, -1); @@ -566,7 +534,6 @@ int uwsgi_lua_request(struct wsgi_request *wsgi_req) { } clear: - lua_pop(L, 4); clear2: diff --git a/plugins/lua/uwsgiplugin.py b/plugins/lua/uwsgiplugin.py index ff77d905..ea129263 100644 --- a/plugins/lua/uwsgiplugin.py +++ b/plugins/lua/uwsgiplugin.py @@ -3,7 +3,7 @@ import os,sys try: LUALIB = os.environ['UWSGICONFIG_LUALIB'] except: - LUALIB = 'lua' + LUALIB = 'lua5.1' try: LUAINC = os.environ['UWSGICONFIG_LUAINC']