From da3738e6b50c16f298c2a5f53df076b72711e09d Mon Sep 17 00:00:00 2001 From: daurnimator Date: Thu, 15 Jan 2015 12:06:39 -0500 Subject: [PATCH 01/15] Add fd module Advanced interface that allows users to yield until a file descriptor polls ready (or timeout elapses) As discussed in "Openresty + cqueues" mailing list thread. --- config | 2 + src/ngx_http_lua_fd.c | 193 ++++++++++++++++++++++++++++++++++++++++ src/ngx_http_lua_fd.h | 12 +++ src/ngx_http_lua_util.c | 3 +- 4 files changed, 209 insertions(+), 1 deletion(-) create mode 100644 src/ngx_http_lua_fd.c create mode 100644 src/ngx_http_lua_fd.h diff --git a/config b/config index 4b770d7da3..7f0df95949 100644 --- a/config +++ b/config @@ -351,6 +351,7 @@ NGX_ADDON_SRCS="$NGX_ADDON_SRCS \ $ngx_addon_dir/src/ngx_http_lua_config.c \ $ngx_addon_dir/src/ngx_http_lua_worker.c \ $ngx_addon_dir/src/ngx_http_lua_lex.c \ + $ngx_addon_dir/src/ngx_http_lua_fd.c \ " NGX_ADDON_DEPS="$NGX_ADDON_DEPS \ @@ -405,6 +406,7 @@ NGX_ADDON_DEPS="$NGX_ADDON_DEPS \ $ngx_addon_dir/src/ngx_http_lua_config.h \ $ngx_addon_dir/src/ngx_http_lua_worker.h \ $ngx_addon_dir/src/ngx_http_lua_lex.h \ + $ngx_addon_dir/src/ngx_http_lua_fd.h \ " CFLAGS="$CFLAGS -DNDK_SET_VAR" diff --git a/src/ngx_http_lua_fd.c b/src/ngx_http_lua_fd.c new file mode 100644 index 0000000000..f8559f45f8 --- /dev/null +++ b/src/ngx_http_lua_fd.c @@ -0,0 +1,193 @@ +/* + * Copyright (C) 2014-2015 Daurnimator + */ + +/* need to include DDEBUG so that ngx_http_lua_util.h works */ +#ifndef DDEBUG +#define DDEBUG 0 +#endif +#include "ddebug.h" + +#include "ngx_http_lua_fd.h" + +#include /* HUGE_VAL */ +#include /* calloc, free */ + +#include +#include + +#include "ngx_core.h" +#include "ngx_http.h" /* ngx_http_request_t, ngx_http_run_posted_requests */ +#include "ngx_http_lua_common.h" /* ngx_http_lua_module, ngx_http_lua_co_ctx_t */ + +#include "ngx_http_lua_util.h" /* ngx_http_lua_get_lua_vm, ngx_http_lua_run_thread, ngx_http_lua_finalize_request */ +#include "ngx_http_lua_contentby.h" /* ngx_http_lua_content_wev_handler */ + +typedef struct { + ngx_http_request_t *request; + ngx_http_lua_co_ctx_t *co_ctx; + ngx_connection_t *conn; +} ngx_http_lua_udata_t; + +static ngx_int_t ngx_http_lua_fd_resume_request(ngx_http_request_t *r) { + ngx_http_lua_ctx_t *ctx; + lua_State *vm; + ngx_int_t rc; + if ((ctx = ngx_http_get_module_ctx(r, ngx_http_lua_module)) == NULL) { + return NGX_ERROR; + } + /* restore normal resume handler */ + ctx->resume_handler = ngx_http_lua_wev_handler; + /* resume lua thread */ + vm = ngx_http_lua_get_lua_vm(r, ctx); + rc = ngx_http_lua_run_thread(vm, r, ctx, 0 /*nret*/); + switch (rc) { + case NGX_DONE: /* coroutine finished */ + ngx_http_lua_finalize_request(r, NGX_DONE); + /* fall-through */ + case NGX_AGAIN: /* coroutine yielded */ + return ngx_http_lua_run_posted_threads(r->connection, vm, r, ctx); + default: /* NGX_ERROR: coroutine failed */ + if (ctx->entered_content_phase) { + ngx_http_lua_finalize_request(r, rc); + return NGX_DONE; + } + return rc; + } +} + +static void ngx_http_lua_fd_rev_handler(ngx_event_t *ev) { + ngx_connection_t *conn = ev->data; + ngx_http_lua_udata_t *u = conn->data; + ngx_http_request_t *r = u->request; + ngx_http_lua_co_ctx_t *co_ctx = u->co_ctx; + ngx_http_lua_ctx_t *ctx; + + ngx_http_lua_cleanup_pending_operation(co_ctx); + ev = NULL, conn = NULL, u = NULL; /* now invalidated */ + + if ((ctx = ngx_http_get_module_ctx(r, ngx_http_lua_module)) != NULL) { + /* set current coroutine to the one that had the event */ + ctx->cur_co_ctx = co_ctx; + ctx->resume_handler = ngx_http_lua_fd_resume_request; + /* queue/fire off handler */ + r->write_event_handler(r); + } + + ngx_http_run_posted_requests(r->connection); +} + +static void ngx_http_lua_fd_cleanup(ngx_http_lua_co_ctx_t *co_ctx) { + ngx_http_lua_udata_t *u = co_ctx->data; + if (u->conn != NULL) { + /* can't use ngx_close_connection here, + as it closes the file descriptor unconditionally */ + + /* cancel timeout timer */ + if (u->conn->read->timer_set) { + ngx_del_timer(u->conn->read); + } + + if (u->conn->fd != -1) { + /* remove from mainloop; do not pass CLOSE_SOCKET flag */ + ngx_del_conn(u->conn, 0); + } + + /* delete any pending but not handled events */ +#if (NGX_THREADS) + ngx_mutex_lock(ngx_posted_events_mutex); +#endif +#if defined(nginx_version) && nginx_version >= 1007005 + if (u->conn->read->posted) { +#else + if (u->conn->read->prev) { +#endif + ngx_delete_posted_event(u->conn->read); + } +#if (NGX_THREADS) + ngx_unlock(&u->conn->lock); + u->conn->read->locked = 0; + + ngx_mutex_unlock(ngx_posted_events_mutex); +#endif + + /* not sure what this line does, the 0 means non-reusable */ + ngx_reusable_connection(u->conn, 0); + + /* invalidate connection object */ + u->conn->fd = -1; + ngx_free_connection(u->conn); + + u->conn = NULL; + } + free(u); + co_ctx->data = NULL; +} + +static int ngx_http_lua_fd_wait(lua_State *L) { + ngx_http_request_t *r; + ngx_http_lua_ctx_t *ctx; + ngx_http_lua_co_ctx_t *co_ctx; + ngx_http_lua_udata_t *u; + + ngx_socket_t fd = luaL_optint(L, 1, -1); /* -1 is invalid fd */ + double timeout = luaL_optnumber(L, 2, HUGE_VAL); /* default to infinite timeout */ + if (fd < 0 && timeout == HUGE_VAL) { + return luaL_error(L, "must provide a valid file descriptor or timeout"); + } + if ((r = ngx_http_lua_get_req(L)) == NULL) { + return luaL_error(L, "no request found"); + } + if ((ctx = ngx_http_get_module_ctx(r, ngx_http_lua_module)) == NULL) { + return luaL_error(L, "no request ctx found"); + } + if ((co_ctx = ctx->cur_co_ctx) == NULL) { + return luaL_error(L, "no co ctx found"); + } + if ((u = calloc(1, sizeof(ngx_http_lua_udata_t))) == NULL) { + return luaL_error(L, "no mem"); + } + /* always create a connection object (even if fd is < 0) */ + if ((u->conn = ngx_get_connection(fd, r->connection->log)) == NULL) { + free(u); + return luaL_error(L, "unable to get nginx connection"); + } + ngx_http_lua_cleanup_pending_operation(co_ctx); + + u->conn->data = u; + u->conn->read->handler = ngx_http_lua_fd_rev_handler; + u->conn->read->log = u->conn->log; + u->conn->number = ngx_atomic_fetch_add(ngx_connection_counter, 1); + + if (fd >= 0) { + if (ngx_handle_read_event(u->conn->read, NGX_LEVEL_EVENT) != NGX_OK) { + ngx_free_connection(u->conn); + free(u); + return luaL_error(L, "unable to add to nginx main loop"); + } + } + + u->request = r; + + u->co_ctx = co_ctx; + co_ctx->cleanup = (ngx_http_cleanup_pt)&ngx_http_lua_fd_cleanup; + co_ctx->data = u; + + if (timeout != HUGE_VAL) { /* no point adding an infinite timeout */ + ngx_add_timer(u->conn->read, (ngx_msec_t)(timeout * 1000)); + } + + /* make sure nginx knows what to do next */ + if (ctx->entered_content_phase) { + r->write_event_handler = ngx_http_lua_content_wev_handler; + } else { + r->write_event_handler = ngx_http_core_run_phases; + } + + return lua_yield(L, 0); +} + +void ngx_http_lua_inject_fd(lua_State *L) { + lua_pushcfunction(L, ngx_http_lua_fd_wait); + lua_setfield(L, -2, "wait"); +} diff --git a/src/ngx_http_lua_fd.h b/src/ngx_http_lua_fd.h new file mode 100644 index 0000000000..c5a7a1251c --- /dev/null +++ b/src/ngx_http_lua_fd.h @@ -0,0 +1,12 @@ +/* + * Copyright (C) 2014 Daurnimator + */ + +#ifndef _NGX_HTTP_LUA_FD_H_INCLUDED_ +#define _NGX_HTTP_LUA_FD_H_INCLUDED_ + +#include + +void ngx_http_lua_inject_fd(lua_State *L); + +#endif /* _NGX_HTTP_LUA_FD_H_INCLUDED_ */ diff --git a/src/ngx_http_lua_util.c b/src/ngx_http_lua_util.c index e9d40fc0e3..53db488549 100644 --- a/src/ngx_http_lua_util.c +++ b/src/ngx_http_lua_util.c @@ -49,7 +49,7 @@ #include "ngx_http_lua_config.h" #include "ngx_http_lua_worker.h" #include "ngx_http_lua_socket_tcp.h" - +#include "ngx_http_lua_fd.h" #if 1 #undef ngx_http_lua_probe_info @@ -737,6 +737,7 @@ ngx_http_lua_inject_ngx_api(lua_State *L, ngx_http_lua_main_conf_t *lmcf, ngx_http_lua_inject_subrequest_api(L); ngx_http_lua_inject_sleep_api(L); ngx_http_lua_inject_phase_api(L); + ngx_http_lua_inject_fd(L); #if (NGX_PCRE) ngx_http_lua_inject_regex_api(L); From ff0240a9d48487cc854cc0a74263edc7768b49aa Mon Sep 17 00:00:00 2001 From: daurnimator Date: Thu, 15 Jan 2015 12:35:48 -0500 Subject: [PATCH 02/15] fd module: Add support for 'event type' argument the event string may contain "r" and/or "w" --- src/ngx_http_lua_fd.c | 35 +++++++++++++++++++++++++++++++---- 1 file changed, 31 insertions(+), 4 deletions(-) diff --git a/src/ngx_http_lua_fd.c b/src/ngx_http_lua_fd.c index f8559f45f8..22f798e343 100644 --- a/src/ngx_http_lua_fd.c +++ b/src/ngx_http_lua_fd.c @@ -12,6 +12,7 @@ #include /* HUGE_VAL */ #include /* calloc, free */ +#include /* POLLIN, POLLOUT */ #include #include @@ -99,14 +100,23 @@ static void ngx_http_lua_fd_cleanup(ngx_http_lua_co_ctx_t *co_ctx) { #endif #if defined(nginx_version) && nginx_version >= 1007005 if (u->conn->read->posted) { + ngx_delete_posted_event(u->conn->read); + } + if (u->conn->write->posted) { + ngx_delete_posted_event(u->conn->write); + } #else if (u->conn->read->prev) { -#endif ngx_delete_posted_event(u->conn->read); } + if (u->conn->write->prev) { + ngx_delete_posted_event(u->conn->write); + } +#endif #if (NGX_THREADS) ngx_unlock(&u->conn->lock); u->conn->read->locked = 0; + u->conn->write->locked = 0; ngx_mutex_unlock(ngx_posted_events_mutex); #endif @@ -131,8 +141,17 @@ static int ngx_http_lua_fd_wait(lua_State *L) { ngx_http_lua_udata_t *u; ngx_socket_t fd = luaL_optint(L, 1, -1); /* -1 is invalid fd */ - double timeout = luaL_optnumber(L, 2, HUGE_VAL); /* default to infinite timeout */ - if (fd < 0 && timeout == HUGE_VAL) { + const char *events = luaL_optstring(L, 2, ""); + int poll_mask = 0; + double timeout = luaL_optnumber(L, 3, HUGE_VAL); /* default to infinite timeout */ + while (*events) { + if (*events == 'r') + poll_mask |= POLLIN; + else if (*events == 'w') + poll_mask |= POLLOUT; + events++; + } + if ((fd < 0 || !poll_mask) && timeout == HUGE_VAL) { return luaL_error(L, "must provide a valid file descriptor or timeout"); } if ((r = ngx_http_lua_get_req(L)) == NULL) { @@ -157,10 +176,18 @@ static int ngx_http_lua_fd_wait(lua_State *L) { u->conn->data = u; u->conn->read->handler = ngx_http_lua_fd_rev_handler; u->conn->read->log = u->conn->log; + u->conn->write->handler = ngx_http_lua_fd_rev_handler; + u->conn->write->log = u->conn->log; u->conn->number = ngx_atomic_fetch_add(ngx_connection_counter, 1); if (fd >= 0) { - if (ngx_handle_read_event(u->conn->read, NGX_LEVEL_EVENT) != NGX_OK) { + if ((poll_mask & POLLIN) && ngx_handle_read_event(u->conn->read, NGX_LEVEL_EVENT) != NGX_OK) { + ngx_free_connection(u->conn); + free(u); + return luaL_error(L, "unable to add to nginx main loop"); + } + if ((poll_mask & POLLOUT) && ngx_handle_write_event(u->conn->write, NGX_LEVEL_EVENT) != NGX_OK) { + if (poll_mask & POLLIN) ngx_del_event(u->conn->read, NGX_READ_EVENT, 0); ngx_free_connection(u->conn); free(u); return luaL_error(L, "unable to add to nginx main loop"); From 5893cd330c75d32a3529d7d2b353e3cf5457d25a Mon Sep 17 00:00:00 2001 From: daurnimator Date: Tue, 3 Feb 2015 17:49:01 -0500 Subject: [PATCH 03/15] Remove NGX_THREADS conditional. https://github.com/openresty/lua-nginx-module/pull/450#discussion_r24043626 --- src/ngx_http_lua_fd.c | 11 ----------- 1 file changed, 11 deletions(-) diff --git a/src/ngx_http_lua_fd.c b/src/ngx_http_lua_fd.c index 22f798e343..bae9bfc0ff 100644 --- a/src/ngx_http_lua_fd.c +++ b/src/ngx_http_lua_fd.c @@ -95,9 +95,6 @@ static void ngx_http_lua_fd_cleanup(ngx_http_lua_co_ctx_t *co_ctx) { } /* delete any pending but not handled events */ -#if (NGX_THREADS) - ngx_mutex_lock(ngx_posted_events_mutex); -#endif #if defined(nginx_version) && nginx_version >= 1007005 if (u->conn->read->posted) { ngx_delete_posted_event(u->conn->read); @@ -113,14 +110,6 @@ static void ngx_http_lua_fd_cleanup(ngx_http_lua_co_ctx_t *co_ctx) { ngx_delete_posted_event(u->conn->write); } #endif -#if (NGX_THREADS) - ngx_unlock(&u->conn->lock); - u->conn->read->locked = 0; - u->conn->write->locked = 0; - - ngx_mutex_unlock(ngx_posted_events_mutex); -#endif - /* not sure what this line does, the 0 means non-reusable */ ngx_reusable_connection(u->conn, 0); From 112fdd2a034e296212762e9431dc6593fa49f462 Mon Sep 17 00:00:00 2001 From: daurnimator Date: Tue, 3 Feb 2015 17:49:13 -0500 Subject: [PATCH 04/15] Don't use libc allocation functions, use ngx_alloc and ngx_free https://github.com/openresty/lua-nginx-module/pull/450#discussion_r24043683 --- src/ngx_http_lua_fd.c | 12 ++++++------ 1 file changed, 6 insertions(+), 6 deletions(-) diff --git a/src/ngx_http_lua_fd.c b/src/ngx_http_lua_fd.c index bae9bfc0ff..8b63bf22b2 100644 --- a/src/ngx_http_lua_fd.c +++ b/src/ngx_http_lua_fd.c @@ -11,13 +11,13 @@ #include "ngx_http_lua_fd.h" #include /* HUGE_VAL */ -#include /* calloc, free */ #include /* POLLIN, POLLOUT */ #include #include #include "ngx_core.h" +#include "ngx_alloc.h" /* ngx_alloc, ngx_free */ #include "ngx_http.h" /* ngx_http_request_t, ngx_http_run_posted_requests */ #include "ngx_http_lua_common.h" /* ngx_http_lua_module, ngx_http_lua_co_ctx_t */ @@ -119,7 +119,7 @@ static void ngx_http_lua_fd_cleanup(ngx_http_lua_co_ctx_t *co_ctx) { u->conn = NULL; } - free(u); + ngx_free(u); co_ctx->data = NULL; } @@ -152,12 +152,12 @@ static int ngx_http_lua_fd_wait(lua_State *L) { if ((co_ctx = ctx->cur_co_ctx) == NULL) { return luaL_error(L, "no co ctx found"); } - if ((u = calloc(1, sizeof(ngx_http_lua_udata_t))) == NULL) { + if ((u = ngx_alloc(sizeof(ngx_http_lua_udata_t), r->connection->log)) == NULL) { return luaL_error(L, "no mem"); } /* always create a connection object (even if fd is < 0) */ if ((u->conn = ngx_get_connection(fd, r->connection->log)) == NULL) { - free(u); + ngx_free(u); return luaL_error(L, "unable to get nginx connection"); } ngx_http_lua_cleanup_pending_operation(co_ctx); @@ -172,13 +172,13 @@ static int ngx_http_lua_fd_wait(lua_State *L) { if (fd >= 0) { if ((poll_mask & POLLIN) && ngx_handle_read_event(u->conn->read, NGX_LEVEL_EVENT) != NGX_OK) { ngx_free_connection(u->conn); - free(u); + ngx_free(u); return luaL_error(L, "unable to add to nginx main loop"); } if ((poll_mask & POLLOUT) && ngx_handle_write_event(u->conn->write, NGX_LEVEL_EVENT) != NGX_OK) { if (poll_mask & POLLIN) ngx_del_event(u->conn->read, NGX_READ_EVENT, 0); ngx_free_connection(u->conn); - free(u); + ngx_free(u); return luaL_error(L, "unable to add to nginx main loop"); } } From 14d25dd0b23cab5ed96b885f07d7664ae5d3cebb Mon Sep 17 00:00:00 2001 From: daurnimator Date: Tue, 3 Feb 2015 17:59:57 -0500 Subject: [PATCH 05/15] Don't use complex initialisers https://github.com/openresty/lua-nginx-module/pull/450#discussion_r24043476 --- src/ngx_http_lua_fd.c | 15 +++++++++------ 1 file changed, 9 insertions(+), 6 deletions(-) diff --git a/src/ngx_http_lua_fd.c b/src/ngx_http_lua_fd.c index 8b63bf22b2..ba86f99a98 100644 --- a/src/ngx_http_lua_fd.c +++ b/src/ngx_http_lua_fd.c @@ -58,14 +58,17 @@ static ngx_int_t ngx_http_lua_fd_resume_request(ngx_http_request_t *r) { } static void ngx_http_lua_fd_rev_handler(ngx_event_t *ev) { - ngx_connection_t *conn = ev->data; - ngx_http_lua_udata_t *u = conn->data; - ngx_http_request_t *r = u->request; - ngx_http_lua_co_ctx_t *co_ctx = u->co_ctx; - ngx_http_lua_ctx_t *ctx; + ngx_http_lua_udata_t *u; + ngx_http_request_t *r; + ngx_http_lua_co_ctx_t *co_ctx; + ngx_http_lua_ctx_t *ctx; + + u = ((ngx_connection_t*)(ev->data))->data; + r = u->request; + co_ctx = u->co_ctx; ngx_http_lua_cleanup_pending_operation(co_ctx); - ev = NULL, conn = NULL, u = NULL; /* now invalidated */ + ev = NULL, u = NULL; /* now invalidated */ if ((ctx = ngx_http_get_module_ctx(r, ngx_http_lua_module)) != NULL) { /* set current coroutine to the one that had the event */ From b0d214b09536e61bb17164122969a7efc899fa0a Mon Sep 17 00:00:00 2001 From: daurnimator Date: Tue, 3 Feb 2015 18:00:39 -0500 Subject: [PATCH 06/15] Style: align up the member variable names. https://github.com/openresty/lua-nginx-module/pull/450#discussion_r24043508 --- src/ngx_http_lua_fd.c | 14 +++++++------- 1 file changed, 7 insertions(+), 7 deletions(-) diff --git a/src/ngx_http_lua_fd.c b/src/ngx_http_lua_fd.c index ba86f99a98..3448baceb1 100644 --- a/src/ngx_http_lua_fd.c +++ b/src/ngx_http_lua_fd.c @@ -25,15 +25,15 @@ #include "ngx_http_lua_contentby.h" /* ngx_http_lua_content_wev_handler */ typedef struct { - ngx_http_request_t *request; + ngx_http_request_t *request; ngx_http_lua_co_ctx_t *co_ctx; - ngx_connection_t *conn; + ngx_connection_t *conn; } ngx_http_lua_udata_t; static ngx_int_t ngx_http_lua_fd_resume_request(ngx_http_request_t *r) { ngx_http_lua_ctx_t *ctx; - lua_State *vm; - ngx_int_t rc; + lua_State *vm; + ngx_int_t rc; if ((ctx = ngx_http_get_module_ctx(r, ngx_http_lua_module)) == NULL) { return NGX_ERROR; } @@ -127,10 +127,10 @@ static void ngx_http_lua_fd_cleanup(ngx_http_lua_co_ctx_t *co_ctx) { } static int ngx_http_lua_fd_wait(lua_State *L) { - ngx_http_request_t *r; - ngx_http_lua_ctx_t *ctx; + ngx_http_request_t *r; + ngx_http_lua_ctx_t *ctx; ngx_http_lua_co_ctx_t *co_ctx; - ngx_http_lua_udata_t *u; + ngx_http_lua_udata_t *u; ngx_socket_t fd = luaL_optint(L, 1, -1); /* -1 is invalid fd */ const char *events = luaL_optstring(L, 2, ""); From 6409ee6376eb8a5d88c5e240009483d58b032175 Mon Sep 17 00:00:00 2001 From: daurnimator Date: Tue, 3 Feb 2015 18:01:27 -0500 Subject: [PATCH 07/15] Style: blank lines https://github.com/openresty/lua-nginx-module/pull/450#discussion_r24043523 https://github.com/openresty/lua-nginx-module/pull/450#discussion_r24043535 --- src/ngx_http_lua_fd.c | 6 ++++++ src/ngx_http_lua_fd.h | 2 ++ 2 files changed, 8 insertions(+) diff --git a/src/ngx_http_lua_fd.c b/src/ngx_http_lua_fd.c index 3448baceb1..5f5b9df419 100644 --- a/src/ngx_http_lua_fd.c +++ b/src/ngx_http_lua_fd.c @@ -24,12 +24,14 @@ #include "ngx_http_lua_util.h" /* ngx_http_lua_get_lua_vm, ngx_http_lua_run_thread, ngx_http_lua_finalize_request */ #include "ngx_http_lua_contentby.h" /* ngx_http_lua_content_wev_handler */ + typedef struct { ngx_http_request_t *request; ngx_http_lua_co_ctx_t *co_ctx; ngx_connection_t *conn; } ngx_http_lua_udata_t; + static ngx_int_t ngx_http_lua_fd_resume_request(ngx_http_request_t *r) { ngx_http_lua_ctx_t *ctx; lua_State *vm; @@ -57,6 +59,7 @@ static ngx_int_t ngx_http_lua_fd_resume_request(ngx_http_request_t *r) { } } + static void ngx_http_lua_fd_rev_handler(ngx_event_t *ev) { ngx_http_lua_udata_t *u; ngx_http_request_t *r; @@ -81,6 +84,7 @@ static void ngx_http_lua_fd_rev_handler(ngx_event_t *ev) { ngx_http_run_posted_requests(r->connection); } + static void ngx_http_lua_fd_cleanup(ngx_http_lua_co_ctx_t *co_ctx) { ngx_http_lua_udata_t *u = co_ctx->data; if (u->conn != NULL) { @@ -126,6 +130,7 @@ static void ngx_http_lua_fd_cleanup(ngx_http_lua_co_ctx_t *co_ctx) { co_ctx->data = NULL; } + static int ngx_http_lua_fd_wait(lua_State *L) { ngx_http_request_t *r; ngx_http_lua_ctx_t *ctx; @@ -206,6 +211,7 @@ static int ngx_http_lua_fd_wait(lua_State *L) { return lua_yield(L, 0); } + void ngx_http_lua_inject_fd(lua_State *L) { lua_pushcfunction(L, ngx_http_lua_fd_wait); lua_setfield(L, -2, "wait"); diff --git a/src/ngx_http_lua_fd.h b/src/ngx_http_lua_fd.h index c5a7a1251c..66a9de77a4 100644 --- a/src/ngx_http_lua_fd.h +++ b/src/ngx_http_lua_fd.h @@ -7,6 +7,8 @@ #include + void ngx_http_lua_inject_fd(lua_State *L); + #endif /* _NGX_HTTP_LUA_FD_H_INCLUDED_ */ From e1c1f77b4b1649e5e2579e267a22bc3da3d1b05e Mon Sep 17 00:00:00 2001 From: daurnimator Date: Tue, 3 Feb 2015 18:07:58 -0500 Subject: [PATCH 08/15] follow the nginx coding style strictly put function name at column 1, and the { char on the next line (alone). https://github.com/openresty/lua-nginx-module/pull/450#discussion_r24043567 --- src/ngx_http_lua_fd.c | 16 ++++++++++++---- 1 file changed, 12 insertions(+), 4 deletions(-) diff --git a/src/ngx_http_lua_fd.c b/src/ngx_http_lua_fd.c index 5f5b9df419..fe4946ece1 100644 --- a/src/ngx_http_lua_fd.c +++ b/src/ngx_http_lua_fd.c @@ -32,7 +32,9 @@ typedef struct { } ngx_http_lua_udata_t; -static ngx_int_t ngx_http_lua_fd_resume_request(ngx_http_request_t *r) { +static ngx_int_t +ngx_http_lua_fd_resume_request(ngx_http_request_t *r) +{ ngx_http_lua_ctx_t *ctx; lua_State *vm; ngx_int_t rc; @@ -85,7 +87,9 @@ static void ngx_http_lua_fd_rev_handler(ngx_event_t *ev) { } -static void ngx_http_lua_fd_cleanup(ngx_http_lua_co_ctx_t *co_ctx) { +static void +ngx_http_lua_fd_cleanup(ngx_http_lua_co_ctx_t *co_ctx) +{ ngx_http_lua_udata_t *u = co_ctx->data; if (u->conn != NULL) { /* can't use ngx_close_connection here, @@ -131,7 +135,9 @@ static void ngx_http_lua_fd_cleanup(ngx_http_lua_co_ctx_t *co_ctx) { } -static int ngx_http_lua_fd_wait(lua_State *L) { +static int +ngx_http_lua_fd_wait(lua_State *L) +{ ngx_http_request_t *r; ngx_http_lua_ctx_t *ctx; ngx_http_lua_co_ctx_t *co_ctx; @@ -212,7 +218,9 @@ static int ngx_http_lua_fd_wait(lua_State *L) { } -void ngx_http_lua_inject_fd(lua_State *L) { +void +ngx_http_lua_inject_fd(lua_State *L) +{ lua_pushcfunction(L, ngx_http_lua_fd_wait); lua_setfield(L, -2, "wait"); } From c9d16b4879b21d49915f0ed8a30c7bcb60c6d319 Mon Sep 17 00:00:00 2001 From: daurnimator Date: Tue, 3 Feb 2015 18:32:54 -0500 Subject: [PATCH 09/15] Style: always use {} with if statements https://github.com/openresty/lua-nginx-module/pull/450#discussion_r24044149 --- src/ngx_http_lua_fd.c | 9 ++++++--- 1 file changed, 6 insertions(+), 3 deletions(-) diff --git a/src/ngx_http_lua_fd.c b/src/ngx_http_lua_fd.c index fe4946ece1..d5dc6db90b 100644 --- a/src/ngx_http_lua_fd.c +++ b/src/ngx_http_lua_fd.c @@ -148,10 +148,11 @@ ngx_http_lua_fd_wait(lua_State *L) int poll_mask = 0; double timeout = luaL_optnumber(L, 3, HUGE_VAL); /* default to infinite timeout */ while (*events) { - if (*events == 'r') + if (*events == 'r') { poll_mask |= POLLIN; - else if (*events == 'w') + } else if (*events == 'w') { poll_mask |= POLLOUT; + } events++; } if ((fd < 0 || !poll_mask) && timeout == HUGE_VAL) { @@ -190,7 +191,9 @@ ngx_http_lua_fd_wait(lua_State *L) return luaL_error(L, "unable to add to nginx main loop"); } if ((poll_mask & POLLOUT) && ngx_handle_write_event(u->conn->write, NGX_LEVEL_EVENT) != NGX_OK) { - if (poll_mask & POLLIN) ngx_del_event(u->conn->read, NGX_READ_EVENT, 0); + if (poll_mask & POLLIN) { + ngx_del_event(u->conn->read, NGX_READ_EVENT, 0); + } ngx_free_connection(u->conn); ngx_free(u); return luaL_error(L, "unable to add to nginx main loop"); From 0bc283703aadcc3c5d6b67ef43d9b829ce7dc9e9 Mon Sep 17 00:00:00 2001 From: daurnimator Date: Wed, 21 Oct 2015 10:11:25 -0400 Subject: [PATCH 10/15] Use luaL_optinteger instead of luaL_optint optint was removed in lua 5.3 --- src/ngx_http_lua_fd.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/ngx_http_lua_fd.c b/src/ngx_http_lua_fd.c index d5dc6db90b..b4fbe59fbc 100644 --- a/src/ngx_http_lua_fd.c +++ b/src/ngx_http_lua_fd.c @@ -143,7 +143,7 @@ ngx_http_lua_fd_wait(lua_State *L) ngx_http_lua_co_ctx_t *co_ctx; ngx_http_lua_udata_t *u; - ngx_socket_t fd = luaL_optint(L, 1, -1); /* -1 is invalid fd */ + ngx_socket_t fd = luaL_optinteger(L, 1, -1); /* -1 is invalid fd */ const char *events = luaL_optstring(L, 2, ""); int poll_mask = 0; double timeout = luaL_optnumber(L, 3, HUGE_VAL); /* default to infinite timeout */ From 8b29808a35d675fe01a3b4e17488363dc5f6c39e Mon Sep 17 00:00:00 2001 From: daurnimator Date: Wed, 21 Oct 2015 10:17:14 -0400 Subject: [PATCH 11/15] Remove comment now that I understand what it does See https://github.com/openresty/lua-nginx-module/pull/450/files#r24052274 --- src/ngx_http_lua_fd.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/ngx_http_lua_fd.c b/src/ngx_http_lua_fd.c index b4fbe59fbc..632d98e871 100644 --- a/src/ngx_http_lua_fd.c +++ b/src/ngx_http_lua_fd.c @@ -121,7 +121,7 @@ ngx_http_lua_fd_cleanup(ngx_http_lua_co_ctx_t *co_ctx) ngx_delete_posted_event(u->conn->write); } #endif - /* not sure what this line does, the 0 means non-reusable */ + /* mark as non-reusable */ ngx_reusable_connection(u->conn, 0); /* invalidate connection object */ From 8b39c3361183e9047975cafb2b2837d878d5adb0 Mon Sep 17 00:00:00 2001 From: daurnimator Date: Wed, 16 Dec 2015 17:16:04 +1100 Subject: [PATCH 12/15] Refactor to be connection module. In accordance with https://github.com/openresty/lua-nginx-module/pull/450#issuecomment-164914312 --- config | 4 +- src/ngx_http_lua_connection.c | 355 ++++++++++++++++++++++++++++++++++ src/ngx_http_lua_connection.h | 20 ++ src/ngx_http_lua_fd.c | 229 ---------------------- src/ngx_http_lua_fd.h | 14 -- src/ngx_http_lua_util.c | 4 +- 6 files changed, 379 insertions(+), 247 deletions(-) create mode 100644 src/ngx_http_lua_connection.c create mode 100644 src/ngx_http_lua_connection.h delete mode 100644 src/ngx_http_lua_fd.c delete mode 100644 src/ngx_http_lua_fd.h diff --git a/config b/config index 7f0df95949..f9ec3e6c73 100644 --- a/config +++ b/config @@ -351,7 +351,7 @@ NGX_ADDON_SRCS="$NGX_ADDON_SRCS \ $ngx_addon_dir/src/ngx_http_lua_config.c \ $ngx_addon_dir/src/ngx_http_lua_worker.c \ $ngx_addon_dir/src/ngx_http_lua_lex.c \ - $ngx_addon_dir/src/ngx_http_lua_fd.c \ + $ngx_addon_dir/src/ngx_http_lua_connection.c \ " NGX_ADDON_DEPS="$NGX_ADDON_DEPS \ @@ -406,7 +406,7 @@ NGX_ADDON_DEPS="$NGX_ADDON_DEPS \ $ngx_addon_dir/src/ngx_http_lua_config.h \ $ngx_addon_dir/src/ngx_http_lua_worker.h \ $ngx_addon_dir/src/ngx_http_lua_lex.h \ - $ngx_addon_dir/src/ngx_http_lua_fd.h \ + $ngx_addon_dir/src/ngx_http_lua_connection.h \ " CFLAGS="$CFLAGS -DNDK_SET_VAR" diff --git a/src/ngx_http_lua_connection.c b/src/ngx_http_lua_connection.c new file mode 100644 index 0000000000..9801f9ea7a --- /dev/null +++ b/src/ngx_http_lua_connection.c @@ -0,0 +1,355 @@ +/* + * Copyright (C) 2014-2015 Daurnimator + */ + +/* need to include DDEBUG so that ngx_http_lua_util.h works */ +#ifndef DDEBUG +#define DDEBUG 0 +#endif +#include "ddebug.h" + +#include "ngx_http_lua_connection.h" + +#include /* HUGE_VAL */ +#include /* POLLIN, POLLOUT */ + +#include +#include + +#include "ngx_core.h" +#include "ngx_alloc.h" /* ngx_alloc, ngx_free */ +#include "ngx_http.h" /* ngx_http_request_t, ngx_http_run_posted_requests */ +#include "ngx_http_lua_common.h" /* ngx_http_lua_module, ngx_http_lua_co_ctx_t */ + +#include "ngx_http_lua_util.h" /* ngx_http_lua_get_lua_vm, ngx_http_lua_run_thread, ngx_http_lua_finalize_request */ +#include "ngx_http_lua_contentby.h" /* ngx_http_lua_content_wev_handler */ + + +typedef struct { + ngx_http_request_t *request; + ngx_http_lua_co_ctx_t *co_ctx; + ngx_connection_t *conn; +} ngx_http_lua_udata_t; + + +static ngx_int_t +ngx_http_lua_fd_resume_request(ngx_http_request_t *r) +{ + ngx_http_lua_ctx_t *ctx; + lua_State *vm; + ngx_int_t rc; + if ((ctx = ngx_http_get_module_ctx(r, ngx_http_lua_module)) == NULL) { + return NGX_ERROR; + } + /* restore normal resume handler */ + ctx->resume_handler = ngx_http_lua_wev_handler; + /* resume lua thread */ + vm = ngx_http_lua_get_lua_vm(r, ctx); + lua_pushboolean(ctx->cur_co_ctx->co, 1); + /* resume coroutine */ + rc = ngx_http_lua_run_thread(vm, r, ctx, 1 /*nret*/); + switch (rc) { + case NGX_DONE: /* coroutine finished */ + ngx_http_lua_finalize_request(r, NGX_DONE); + /* fall-through */ + case NGX_AGAIN: /* coroutine yielded */ + return ngx_http_lua_run_posted_threads(r->connection, vm, r, ctx); + default: /* NGX_ERROR: coroutine failed */ + if (ctx->entered_content_phase) { + ngx_http_lua_finalize_request(r, rc); + return NGX_DONE; + } + return rc; + } +} + + +static void ngx_http_lua_fd_rev_handler(ngx_event_t *ev) { + ngx_connection_t *conn; + ngx_http_lua_udata_t *u; + ngx_http_request_t *r; + ngx_http_lua_co_ctx_t *co_ctx; + ngx_http_lua_ctx_t *ctx; + + conn = ev->data; + u = conn->data; + r = u->request; + co_ctx = u->co_ctx; + + ngx_http_lua_cleanup_pending_operation(co_ctx); + ev = NULL, u = NULL; /* now invalidated */ + + if ((ctx = ngx_http_get_module_ctx(r, ngx_http_lua_module)) != NULL) { + /* set current coroutine to the one that had the event */ + ctx->cur_co_ctx = co_ctx; + ctx->resume_handler = ngx_http_lua_fd_resume_request; + /* queue/fire off handler */ + r->write_event_handler(r); + } + + ngx_http_run_posted_requests(r->connection); +} + + +static void +ngx_http_lua_fd_cleanup(ngx_http_lua_co_ctx_t *co_ctx) +{ + ngx_http_lua_udata_t *u = co_ctx->data; + if (u->conn->data) { + /* remove from mainloop; do not pass CLOSE_SOCKET flag */ + ngx_del_conn(u->conn, 0); + u->conn->data = NULL; + } + ngx_free(u); + co_ctx->data = NULL; +} + + +int +ngx_http_lua_connection_init(ngx_connection_t **p, ngx_socket_t fd, const char **err) +{ + ngx_connection_t *conn; + if ((conn = ngx_get_connection(fd, ngx_cycle->log)) == NULL) { + *err = "unable to get nginx connection"; + return NGX_ERROR; + } + conn->data = NULL; + conn->read->handler = ngx_http_lua_fd_rev_handler; + conn->read->log = conn->log; + conn->write->handler = ngx_http_lua_fd_rev_handler; + conn->write->log = conn->log; + conn->number = ngx_atomic_fetch_add(ngx_connection_counter, 1); + *p = conn; + return NGX_OK; +} + + +void +ngx_http_lua_connection_release(ngx_connection_t *conn) +{ + /* can't use ngx_close_connection here, + as it closes the file descriptor unconditionally */ + + /* cancel timeout timer */ + if (conn->read->timer_set) { + ngx_del_timer(conn->read); + } + + if (conn->data) { + /* remove from mainloop; do not pass CLOSE_SOCKET flag */ + ngx_del_conn(conn, 0); + } + + /* delete any pending but not handled events */ +#if defined(nginx_version) && nginx_version >= 1007005 + if (conn->read->posted) { + ngx_delete_posted_event(conn->read); + } + if (conn->write->posted) { + ngx_delete_posted_event(conn->write); + } +#else + if (conn->read->prev) { + ngx_delete_posted_event(conn->read); + } + if (conn->write->prev) { + ngx_delete_posted_event(conn->write); + } +#endif + + /* mark as non-reusable */ + ngx_reusable_connection(conn, 0); + + /* invalidate connection object */ + conn->fd = -1; + conn->data = NULL; + ngx_free_connection(conn); +} + + +int +ngx_http_lua_connection_prep(ngx_http_request_t *r, ngx_connection_t *conn, + int poll_mask, ngx_msec_t wait_ms, const char **err) +{ + ngx_http_lua_ctx_t *ctx; + ngx_http_lua_co_ctx_t *co_ctx; + ngx_http_lua_udata_t *u; + if ((ctx = ngx_http_get_module_ctx(r, ngx_http_lua_module)) == NULL) { + *err ="no request ctx found"; + return NGX_ERROR; + } + if ((co_ctx = ctx->cur_co_ctx) == NULL) { + *err ="no co ctx found"; + return NGX_ERROR; + } + if ((u = ngx_alloc(sizeof(*u), r->connection->log)) == NULL) { + *err ="no memory"; + return NGX_ERROR; + } + if ((poll_mask & POLLIN) && ngx_handle_read_event(conn->read, NGX_LEVEL_EVENT) != NGX_OK) { + ngx_free(u); + *err ="unable to add to nginx main loop"; + return NGX_ERROR; + } + if ((poll_mask & POLLOUT) && ngx_handle_write_event(conn->write, NGX_LEVEL_EVENT) != NGX_OK) { + if (poll_mask & POLLIN) { + ngx_del_event(conn->read, NGX_READ_EVENT, 0); + } + ngx_free(u); + *err ="unable to add to nginx main loop"; + return NGX_ERROR; + } + /* only clean up pending after no failures are possible */ + ngx_http_lua_cleanup_pending_operation(co_ctx); + conn->data = u; + u->request = r; + u->co_ctx = co_ctx; + u->conn = conn; + co_ctx->cleanup = (ngx_http_cleanup_pt)&ngx_http_lua_fd_cleanup; + co_ctx->data = u; + + if (wait_ms != (ngx_msec_t)-1) { + ngx_add_timer(conn->read, wait_ms); + } + + /* make sure nginx knows what to do next */ + if (ctx->entered_content_phase) { + r->write_event_handler = ngx_http_lua_content_wev_handler; + } else { + r->write_event_handler = ngx_http_core_run_phases; + } + + return NGX_OK; +} + + +/* Lua C API bindings */ + +static int +ngx_http_lua_connection_new(lua_State *L) +{ + ngx_connection_t **pconn; + ngx_socket_t fd = luaL_checkinteger(L, 1); + const char *errmsg; + luaL_argcheck(L, fd >= 0, 1, "invalid file descriptor"); + pconn = lua_newuserdata(L, sizeof(*pconn)); + *pconn = NULL; + luaL_getmetatable(L, NGX_HTTP_LUA_CONNECTION_KEY); + lua_setmetatable(L, -2); + if (NGX_ERROR == ngx_http_lua_connection_init(pconn, fd, &errmsg)) { + return luaL_error(L, errmsg); + } + (*pconn)->data = NULL; + return 1; +} + + +static ngx_connection_t* +ngx_http_lua_connection_check(lua_State *L, int idx) +{ + ngx_connection_t **pconn = luaL_checkudata(L, idx, NGX_HTTP_LUA_CONNECTION_KEY); + luaL_argcheck(L, *pconn != NULL, idx, "ngx_connection_t has been freed"); + return *pconn; +} + + +static int +ngx_http_lua_connection_gc(lua_State *L) +{ + ngx_connection_t **pconn = luaL_checkudata(L, 1, NGX_HTTP_LUA_CONNECTION_KEY); + if (NULL != *pconn) { + ngx_http_lua_connection_release(*pconn); + *pconn = NULL; + } + return 0; +} + + +static int +ngx_http_lua_connection_tostring(lua_State *L) +{ + ngx_connection_t *conn = ngx_http_lua_connection_check(L, 1); + lua_pushfstring(L, "ngx_connection*: %p", conn); + return 1; +} + + +static int +ngx_http_lua_connection_wait(lua_State *L) +{ + ngx_connection_t *conn = ngx_http_lua_connection_check(L, 1); + int poll_mask; + double timeout; + const char *events, *err; + ngx_http_request_t *r; + + switch(lua_type(L, 2)) { + case LUA_TSTRING: + events = lua_tostring(L, 2); + poll_mask = 0; + while (*events) { + if (*events == 'r') { + poll_mask |= POLLIN; + } else if (*events == 'w') { + poll_mask |= POLLOUT; + } + events++; + } + break; + case LUA_TNUMBER: + poll_mask = lua_tointeger(L, 2); + if (!(poll_mask & ~(POLLIN|POLLOUT))) { + break; + } + /* fall through on invalid poll_mask */ + default: + return luaL_argerror(L, 2, "expected bitwise 'or' of POLLIN|POLLOUT or characters from set 'rw'"); + } + + timeout = luaL_optnumber(L, 3, HUGE_VAL); /* default to infinite timeout */ + if (!poll_mask && timeout == HUGE_VAL) { + return luaL_error(L, "must provide a valid events mask or finite timeout"); + } + if ((r = ngx_http_lua_get_req(L)) == NULL) { + return luaL_error(L, "no request found"); + } + + if (NGX_ERROR == ngx_http_lua_connection_prep(r, conn, poll_mask, + (timeout < 0)? 0 : (timeout == HUGE_VAL)? ((ngx_msec_t)-1) : timeout*1000, &err)) { + return luaL_error(L, err); + } + + return lua_yield(L, 0); +} + + +void +ngx_http_lua_inject_connection(lua_State *L) +{ + luaL_newmetatable(L, NGX_HTTP_LUA_CONNECTION_KEY); + lua_pushcfunction(L, ngx_http_lua_connection_gc); + lua_setfield(L, -2, "__gc"); + lua_pushcfunction(L, ngx_http_lua_connection_tostring); + lua_setfield(L, -2, "__tostring"); + lua_newtable(L); + lua_pushcfunction(L, ngx_http_lua_connection_wait); + lua_setfield(L, -2, "wait"); + lua_pushcfunction(L, ngx_http_lua_connection_gc); + lua_setfield(L, -2, "free"); + lua_setfield(L, -2, "__index"); + lua_pop(L, 1); + + lua_newtable(L); + lua_pushcfunction(L, ngx_http_lua_connection_new); + lua_setfield(L, -2, "new"); + lua_pushcfunction(L, ngx_http_lua_connection_wait); + lua_setfield(L, -2, "wait"); + lua_pushcfunction(L, ngx_http_lua_connection_gc); + lua_setfield(L, -2, "free"); + lua_pushinteger(L, POLLIN); + lua_setfield(L, -2, "POLLIN"); + lua_pushinteger(L, POLLOUT); + lua_setfield(L, -2, "POLLOUT"); + + lua_setfield(L, -2, "connection"); +} diff --git a/src/ngx_http_lua_connection.h b/src/ngx_http_lua_connection.h new file mode 100644 index 0000000000..170ffac6ac --- /dev/null +++ b/src/ngx_http_lua_connection.h @@ -0,0 +1,20 @@ +/* + * Copyright (C) 2014 Daurnimator + */ + +#ifndef _NGX_HTTP_LUA_CONNECTION_H_INCLUDED_ +#define _NGX_HTTP_LUA_CONNECTION_H_INCLUDED_ + +#include +#include "ngx_http_lua_common.h" + +#define NGX_HTTP_LUA_CONNECTION_KEY "ngx_connection_t*" + +int ngx_http_lua_connection_init(ngx_connection_t **p, ngx_socket_t fd, const char **err); +void ngx_http_lua_connection_release(ngx_connection_t *conn); +int ngx_http_lua_connection_prep(ngx_http_request_t *r, ngx_connection_t *conn, + int poll_mask, ngx_msec_t wait_ms, const char **err); +void ngx_http_lua_inject_connection(lua_State *L); + + +#endif /* _NGX_HTTP_LUA_CONNECTION_H_INCLUDED_ */ diff --git a/src/ngx_http_lua_fd.c b/src/ngx_http_lua_fd.c deleted file mode 100644 index 632d98e871..0000000000 --- a/src/ngx_http_lua_fd.c +++ /dev/null @@ -1,229 +0,0 @@ -/* - * Copyright (C) 2014-2015 Daurnimator - */ - -/* need to include DDEBUG so that ngx_http_lua_util.h works */ -#ifndef DDEBUG -#define DDEBUG 0 -#endif -#include "ddebug.h" - -#include "ngx_http_lua_fd.h" - -#include /* HUGE_VAL */ -#include /* POLLIN, POLLOUT */ - -#include -#include - -#include "ngx_core.h" -#include "ngx_alloc.h" /* ngx_alloc, ngx_free */ -#include "ngx_http.h" /* ngx_http_request_t, ngx_http_run_posted_requests */ -#include "ngx_http_lua_common.h" /* ngx_http_lua_module, ngx_http_lua_co_ctx_t */ - -#include "ngx_http_lua_util.h" /* ngx_http_lua_get_lua_vm, ngx_http_lua_run_thread, ngx_http_lua_finalize_request */ -#include "ngx_http_lua_contentby.h" /* ngx_http_lua_content_wev_handler */ - - -typedef struct { - ngx_http_request_t *request; - ngx_http_lua_co_ctx_t *co_ctx; - ngx_connection_t *conn; -} ngx_http_lua_udata_t; - - -static ngx_int_t -ngx_http_lua_fd_resume_request(ngx_http_request_t *r) -{ - ngx_http_lua_ctx_t *ctx; - lua_State *vm; - ngx_int_t rc; - if ((ctx = ngx_http_get_module_ctx(r, ngx_http_lua_module)) == NULL) { - return NGX_ERROR; - } - /* restore normal resume handler */ - ctx->resume_handler = ngx_http_lua_wev_handler; - /* resume lua thread */ - vm = ngx_http_lua_get_lua_vm(r, ctx); - rc = ngx_http_lua_run_thread(vm, r, ctx, 0 /*nret*/); - switch (rc) { - case NGX_DONE: /* coroutine finished */ - ngx_http_lua_finalize_request(r, NGX_DONE); - /* fall-through */ - case NGX_AGAIN: /* coroutine yielded */ - return ngx_http_lua_run_posted_threads(r->connection, vm, r, ctx); - default: /* NGX_ERROR: coroutine failed */ - if (ctx->entered_content_phase) { - ngx_http_lua_finalize_request(r, rc); - return NGX_DONE; - } - return rc; - } -} - - -static void ngx_http_lua_fd_rev_handler(ngx_event_t *ev) { - ngx_http_lua_udata_t *u; - ngx_http_request_t *r; - ngx_http_lua_co_ctx_t *co_ctx; - ngx_http_lua_ctx_t *ctx; - - u = ((ngx_connection_t*)(ev->data))->data; - r = u->request; - co_ctx = u->co_ctx; - - ngx_http_lua_cleanup_pending_operation(co_ctx); - ev = NULL, u = NULL; /* now invalidated */ - - if ((ctx = ngx_http_get_module_ctx(r, ngx_http_lua_module)) != NULL) { - /* set current coroutine to the one that had the event */ - ctx->cur_co_ctx = co_ctx; - ctx->resume_handler = ngx_http_lua_fd_resume_request; - /* queue/fire off handler */ - r->write_event_handler(r); - } - - ngx_http_run_posted_requests(r->connection); -} - - -static void -ngx_http_lua_fd_cleanup(ngx_http_lua_co_ctx_t *co_ctx) -{ - ngx_http_lua_udata_t *u = co_ctx->data; - if (u->conn != NULL) { - /* can't use ngx_close_connection here, - as it closes the file descriptor unconditionally */ - - /* cancel timeout timer */ - if (u->conn->read->timer_set) { - ngx_del_timer(u->conn->read); - } - - if (u->conn->fd != -1) { - /* remove from mainloop; do not pass CLOSE_SOCKET flag */ - ngx_del_conn(u->conn, 0); - } - - /* delete any pending but not handled events */ -#if defined(nginx_version) && nginx_version >= 1007005 - if (u->conn->read->posted) { - ngx_delete_posted_event(u->conn->read); - } - if (u->conn->write->posted) { - ngx_delete_posted_event(u->conn->write); - } -#else - if (u->conn->read->prev) { - ngx_delete_posted_event(u->conn->read); - } - if (u->conn->write->prev) { - ngx_delete_posted_event(u->conn->write); - } -#endif - /* mark as non-reusable */ - ngx_reusable_connection(u->conn, 0); - - /* invalidate connection object */ - u->conn->fd = -1; - ngx_free_connection(u->conn); - - u->conn = NULL; - } - ngx_free(u); - co_ctx->data = NULL; -} - - -static int -ngx_http_lua_fd_wait(lua_State *L) -{ - ngx_http_request_t *r; - ngx_http_lua_ctx_t *ctx; - ngx_http_lua_co_ctx_t *co_ctx; - ngx_http_lua_udata_t *u; - - ngx_socket_t fd = luaL_optinteger(L, 1, -1); /* -1 is invalid fd */ - const char *events = luaL_optstring(L, 2, ""); - int poll_mask = 0; - double timeout = luaL_optnumber(L, 3, HUGE_VAL); /* default to infinite timeout */ - while (*events) { - if (*events == 'r') { - poll_mask |= POLLIN; - } else if (*events == 'w') { - poll_mask |= POLLOUT; - } - events++; - } - if ((fd < 0 || !poll_mask) && timeout == HUGE_VAL) { - return luaL_error(L, "must provide a valid file descriptor or timeout"); - } - if ((r = ngx_http_lua_get_req(L)) == NULL) { - return luaL_error(L, "no request found"); - } - if ((ctx = ngx_http_get_module_ctx(r, ngx_http_lua_module)) == NULL) { - return luaL_error(L, "no request ctx found"); - } - if ((co_ctx = ctx->cur_co_ctx) == NULL) { - return luaL_error(L, "no co ctx found"); - } - if ((u = ngx_alloc(sizeof(ngx_http_lua_udata_t), r->connection->log)) == NULL) { - return luaL_error(L, "no mem"); - } - /* always create a connection object (even if fd is < 0) */ - if ((u->conn = ngx_get_connection(fd, r->connection->log)) == NULL) { - ngx_free(u); - return luaL_error(L, "unable to get nginx connection"); - } - ngx_http_lua_cleanup_pending_operation(co_ctx); - - u->conn->data = u; - u->conn->read->handler = ngx_http_lua_fd_rev_handler; - u->conn->read->log = u->conn->log; - u->conn->write->handler = ngx_http_lua_fd_rev_handler; - u->conn->write->log = u->conn->log; - u->conn->number = ngx_atomic_fetch_add(ngx_connection_counter, 1); - - if (fd >= 0) { - if ((poll_mask & POLLIN) && ngx_handle_read_event(u->conn->read, NGX_LEVEL_EVENT) != NGX_OK) { - ngx_free_connection(u->conn); - ngx_free(u); - return luaL_error(L, "unable to add to nginx main loop"); - } - if ((poll_mask & POLLOUT) && ngx_handle_write_event(u->conn->write, NGX_LEVEL_EVENT) != NGX_OK) { - if (poll_mask & POLLIN) { - ngx_del_event(u->conn->read, NGX_READ_EVENT, 0); - } - ngx_free_connection(u->conn); - ngx_free(u); - return luaL_error(L, "unable to add to nginx main loop"); - } - } - - u->request = r; - - u->co_ctx = co_ctx; - co_ctx->cleanup = (ngx_http_cleanup_pt)&ngx_http_lua_fd_cleanup; - co_ctx->data = u; - - if (timeout != HUGE_VAL) { /* no point adding an infinite timeout */ - ngx_add_timer(u->conn->read, (ngx_msec_t)(timeout * 1000)); - } - - /* make sure nginx knows what to do next */ - if (ctx->entered_content_phase) { - r->write_event_handler = ngx_http_lua_content_wev_handler; - } else { - r->write_event_handler = ngx_http_core_run_phases; - } - - return lua_yield(L, 0); -} - - -void -ngx_http_lua_inject_fd(lua_State *L) -{ - lua_pushcfunction(L, ngx_http_lua_fd_wait); - lua_setfield(L, -2, "wait"); -} diff --git a/src/ngx_http_lua_fd.h b/src/ngx_http_lua_fd.h deleted file mode 100644 index 66a9de77a4..0000000000 --- a/src/ngx_http_lua_fd.h +++ /dev/null @@ -1,14 +0,0 @@ -/* - * Copyright (C) 2014 Daurnimator - */ - -#ifndef _NGX_HTTP_LUA_FD_H_INCLUDED_ -#define _NGX_HTTP_LUA_FD_H_INCLUDED_ - -#include - - -void ngx_http_lua_inject_fd(lua_State *L); - - -#endif /* _NGX_HTTP_LUA_FD_H_INCLUDED_ */ diff --git a/src/ngx_http_lua_util.c b/src/ngx_http_lua_util.c index 53db488549..d4098435e1 100644 --- a/src/ngx_http_lua_util.c +++ b/src/ngx_http_lua_util.c @@ -49,7 +49,7 @@ #include "ngx_http_lua_config.h" #include "ngx_http_lua_worker.h" #include "ngx_http_lua_socket_tcp.h" -#include "ngx_http_lua_fd.h" +#include "ngx_http_lua_connection.h" #if 1 #undef ngx_http_lua_probe_info @@ -737,7 +737,7 @@ ngx_http_lua_inject_ngx_api(lua_State *L, ngx_http_lua_main_conf_t *lmcf, ngx_http_lua_inject_subrequest_api(L); ngx_http_lua_inject_sleep_api(L); ngx_http_lua_inject_phase_api(L); - ngx_http_lua_inject_fd(L); + ngx_http_lua_inject_connection(L); #if (NGX_PCRE) ngx_http_lua_inject_regex_api(L); From 16a4dc7a304d89162bdee51ecb02f76d786625c9 Mon Sep 17 00:00:00 2001 From: daurnimator Date: Wed, 16 Dec 2015 23:48:08 +1100 Subject: [PATCH 13/15] As far as I can tell; you don't need to run ngx_http_run_posted_requests at the end of a resume handler --- src/ngx_http_lua_connection.c | 2 -- 1 file changed, 2 deletions(-) diff --git a/src/ngx_http_lua_connection.c b/src/ngx_http_lua_connection.c index 9801f9ea7a..d2ae10140e 100644 --- a/src/ngx_http_lua_connection.c +++ b/src/ngx_http_lua_connection.c @@ -86,8 +86,6 @@ static void ngx_http_lua_fd_rev_handler(ngx_event_t *ev) { /* queue/fire off handler */ r->write_event_handler(r); } - - ngx_http_run_posted_requests(r->connection); } From f07a2d0999f47b5f2acae03f5b63210a457e530a Mon Sep 17 00:00:00 2001 From: daurnimator Date: Thu, 17 Dec 2015 00:26:01 +1100 Subject: [PATCH 14/15] Use ngx_add_event instead of ngx_handle_*_event Now the LEVEL flag actually works --- src/ngx_http_lua_connection.c | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/ngx_http_lua_connection.c b/src/ngx_http_lua_connection.c index d2ae10140e..0255d9e140 100644 --- a/src/ngx_http_lua_connection.c +++ b/src/ngx_http_lua_connection.c @@ -184,12 +184,12 @@ ngx_http_lua_connection_prep(ngx_http_request_t *r, ngx_connection_t *conn, *err ="no memory"; return NGX_ERROR; } - if ((poll_mask & POLLIN) && ngx_handle_read_event(conn->read, NGX_LEVEL_EVENT) != NGX_OK) { + if ((poll_mask & POLLIN) && ngx_add_event(conn->read, NGX_READ_EVENT, NGX_LEVEL_EVENT) != NGX_OK) { ngx_free(u); *err ="unable to add to nginx main loop"; return NGX_ERROR; } - if ((poll_mask & POLLOUT) && ngx_handle_write_event(conn->write, NGX_LEVEL_EVENT) != NGX_OK) { + if ((poll_mask & POLLOUT) && ngx_add_event(conn->write, NGX_WRITE_EVENT, NGX_LEVEL_EVENT) != NGX_OK) { if (poll_mask & POLLIN) { ngx_del_event(conn->read, NGX_READ_EVENT, 0); } From c426237414f3321e0469c9648c2222f67269b279 Mon Sep 17 00:00:00 2001 From: daurnimator Date: Thu, 17 Dec 2015 01:23:11 +1100 Subject: [PATCH 15/15] cleanup old events before adding new ones --- src/ngx_http_lua_connection.c | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/ngx_http_lua_connection.c b/src/ngx_http_lua_connection.c index 0255d9e140..f177358662 100644 --- a/src/ngx_http_lua_connection.c +++ b/src/ngx_http_lua_connection.c @@ -184,6 +184,8 @@ ngx_http_lua_connection_prep(ngx_http_request_t *r, ngx_connection_t *conn, *err ="no memory"; return NGX_ERROR; } + /* cleanup old events before adding new ones */ + ngx_http_lua_cleanup_pending_operation(co_ctx); if ((poll_mask & POLLIN) && ngx_add_event(conn->read, NGX_READ_EVENT, NGX_LEVEL_EVENT) != NGX_OK) { ngx_free(u); *err ="unable to add to nginx main loop"; @@ -197,8 +199,6 @@ ngx_http_lua_connection_prep(ngx_http_request_t *r, ngx_connection_t *conn, *err ="unable to add to nginx main loop"; return NGX_ERROR; } - /* only clean up pending after no failures are possible */ - ngx_http_lua_cleanup_pending_operation(co_ctx); conn->data = u; u->request = r; u->co_ctx = co_ctx;