Skip to content

Commit

Permalink
updated to meta-lua-nginx-module #e4a48d1.
Browse files Browse the repository at this point in the history
  • Loading branch information
dndx committed Dec 20, 2017
1 parent 4557314 commit 9cbd9df
Show file tree
Hide file tree
Showing 23 changed files with 333 additions and 109 deletions.
2 changes: 1 addition & 1 deletion src/api/ngx_stream_lua_api.h
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@
/* Public API for other Nginx modules */


#define ngx_stream_lua_version 4
#define ngx_stream_lua_version 10012


typedef struct {
Expand Down
1 change: 1 addition & 0 deletions src/ngx_stream_lua_common.h
Original file line number Diff line number Diff line change
Expand Up @@ -163,6 +163,7 @@ typedef struct {

struct ngx_stream_lua_main_conf_s {
lua_State *lua;
ngx_pool_cleanup_t *vm_cleanup;

ngx_str_t lua_path;
ngx_str_t lua_cpath;
Expand Down
26 changes: 23 additions & 3 deletions src/ngx_stream_lua_initworkerby.c
Original file line number Diff line number Diff line change
Expand Up @@ -53,13 +53,33 @@ ngx_stream_lua_init_worker(ngx_cycle_t *cycle)

lmcf = ngx_stream_cycle_get_module_main_conf(cycle, ngx_stream_lua_module);

if (lmcf == NULL
|| lmcf->init_worker_handler == NULL
|| lmcf->lua == NULL)
if (lmcf == NULL || lmcf->lua == NULL) {
return NGX_OK;
}

/* lmcf != NULL && lmcf->lua != NULL */

/* disable init_worker_by_lua* and destroy lua VM in cache processes */
if (ngx_process == NGX_PROCESS_HELPER
#if defined(HAVE_PRIVILEGED_PROCESS_PATCH) && !NGX_WIN32
&& !ngx_is_privileged_agent
#endif
)
{
ngx_log_debug2(NGX_LOG_DEBUG_STREAM, ngx_cycle->log, 0,
"lua close the global Lua VM %p in the "
"cache helper process %P", lmcf->lua, ngx_pid);

lmcf->vm_cleanup->handler(lmcf->vm_cleanup->data);
lmcf->vm_cleanup->handler = NULL;

return NGX_OK;
}

if (lmcf->init_worker_handler == NULL) {
return NGX_OK;
}

conf_ctx = (ngx_stream_conf_ctx_t *)
cycle->conf_ctx[ngx_stream_module.index];
stream_ctx.main_conf = conf_ctx->main_conf;
Expand Down
2 changes: 2 additions & 0 deletions src/ngx_stream_lua_regex.c
Original file line number Diff line number Diff line change
Expand Up @@ -2199,6 +2199,8 @@ ngx_stream_lua_ffi_compile_regex(const unsigned char *pat, size_t pat_len,
goto error;
}

pool->log = (ngx_log_t *) &ngx_cycle->new_log;

re = ngx_palloc(pool, sizeof(ngx_stream_lua_regex_t));
if (re == NULL) {
ngx_destroy_pool(pool);
Expand Down
27 changes: 27 additions & 0 deletions src/ngx_stream_lua_shdict.c
Original file line number Diff line number Diff line change
Expand Up @@ -2983,6 +2983,33 @@ ngx_stream_lua_ffi_shdict_set_expire(ngx_shm_zone_t *zone, u_char *key,

return NGX_OK;
}


size_t
ngx_stream_lua_ffi_shdict_capacity(ngx_shm_zone_t *zone)
{
return zone->shm.size;
}


# if nginx_version >= 1011007
size_t
ngx_stream_lua_ffi_shdict_free_space(ngx_shm_zone_t *zone)
{
size_t bytes;
ngx_stream_lua_shdict_ctx_t *ctx;

ctx = zone->data;

ngx_shmtx_lock(&ctx->shpool->mutex);
bytes = ctx->shpool->pfree * ngx_pagesize;
ngx_shmtx_unlock(&ctx->shpool->mutex);

return bytes;
}
# endif /* nginx_version >= 1011007 */


#endif /* NGX_LUA_NO_FFI_API */


Expand Down
109 changes: 80 additions & 29 deletions src/ngx_stream_lua_socket_tcp.c
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,8 @@ static int ngx_stream_lua_socket_tcp_settimeouts(lua_State *L);
static void ngx_stream_lua_socket_tcp_handler(ngx_event_t *ev);
static ngx_int_t ngx_stream_lua_socket_tcp_get_peer(ngx_peer_connection_t *pc,
void *data);
static void ngx_stream_lua_socket_init_peer_connection_addr_text(
ngx_peer_connection_t *pc);
static void ngx_stream_lua_socket_read_handler(ngx_stream_lua_request_t *r,
ngx_stream_lua_socket_tcp_upstream_t *u);
static void ngx_stream_lua_socket_send_handler(ngx_stream_lua_request_t *r,
Expand Down Expand Up @@ -105,8 +107,6 @@ static ngx_int_t ngx_stream_lua_get_keepalive_peer(ngx_stream_lua_request_t *r,
static void ngx_stream_lua_socket_keepalive_dummy_handler(ngx_event_t *ev);
static ngx_int_t ngx_stream_lua_socket_keepalive_close_handler(ngx_event_t *ev);
static void ngx_stream_lua_socket_keepalive_rev_handler(ngx_event_t *ev);
static void ngx_stream_lua_socket_free_pool(ngx_log_t *log,
ngx_stream_lua_socket_pool_t *spool);
static int ngx_stream_lua_socket_tcp_upstream_destroy(lua_State *L);
static int ngx_stream_lua_socket_downstream_destroy(lua_State *L);
static ngx_int_t ngx_stream_lua_socket_push_input_data(ngx_stream_lua_request_t *r,
Expand All @@ -126,7 +126,11 @@ static ngx_int_t ngx_stream_lua_socket_tcp_resume_helper(ngx_stream_lua_request_
int socket_op);
static void ngx_stream_lua_tcp_resolve_cleanup(void *data);
static void ngx_stream_lua_coctx_cleanup(void *data);
static void ngx_stream_lua_socket_free_pool(ngx_log_t *log,
ngx_stream_lua_socket_pool_t *spool);
static int ngx_stream_lua_socket_shutdown_pool(lua_State *L);
static void ngx_stream_lua_socket_shutdown_pool_helper(
ngx_stream_lua_socket_pool_t *spool);
static void
ngx_stream_lua_socket_empty_resolve_handler(ngx_resolver_ctx_t *ctx);
static int ngx_stream_lua_socket_prepare_error_retvals(ngx_stream_lua_request_t *r,
Expand Down Expand Up @@ -471,6 +475,7 @@ ngx_stream_lua_socket_tcp_connect(lua_State *L)
switch (lua_type(L, -1)) {
case LUA_TNUMBER:
lua_tostring(L, -1);
/* FALLTHROUGH */

case LUA_TSTRING:
custom_pool = 1;
Expand Down Expand Up @@ -505,7 +510,7 @@ ngx_stream_lua_socket_tcp_connect(lua_State *L)
if (n == 3) {
port = luaL_checkinteger(L, 3);

if (port < 0 || port > 65536) {
if (port < 0 || port > 65535) {
lua_pushnil(L);
lua_pushfstring(L, "bad port number: %d", port);
return 2;
Expand Down Expand Up @@ -952,7 +957,7 @@ ngx_stream_lua_socket_resolve_handler(ngx_resolver_ctx_t *ctx)
#else
/* for nginx older than 1.5.8 */

len = NGX_INET_ADDRSTRLEN + sizeof(":65536") - 1;
len = NGX_INET_ADDRSTRLEN + sizeof(":65535") - 1;

p = ngx_pnalloc(r->pool, len + sizeof(struct sockaddr_in));
if (p == NULL) {
Expand Down Expand Up @@ -1018,6 +1023,50 @@ ngx_stream_lua_socket_resolve_handler(ngx_resolver_ctx_t *ctx)
}


static void
ngx_stream_lua_socket_init_peer_connection_addr_text(ngx_peer_connection_t *pc)
{
ngx_connection_t *c;
size_t addr_text_max_len;

c = pc->connection;

switch (pc->sockaddr->sa_family) {

#if (NGX_HAVE_INET6)
case AF_INET6:
addr_text_max_len = NGX_INET6_ADDRSTRLEN;
break;
#endif

#if (NGX_HAVE_UNIX_DOMAIN)
case AF_UNIX:
addr_text_max_len = NGX_UNIX_ADDRSTRLEN;
break;
#endif

case AF_INET:
addr_text_max_len = NGX_INET_ADDRSTRLEN;
break;

default:
addr_text_max_len = NGX_SOCKADDR_STRLEN;
break;
}

c->addr_text.data = ngx_pnalloc(c->pool, addr_text_max_len);
if (c->addr_text.data == NULL) {
ngx_log_error(NGX_LOG_ERR, pc->log, 0,
"init peer connection addr_text failed: no memory");
return;
}

c->addr_text.len = ngx_sock_ntop(pc->sockaddr, pc->socklen,
c->addr_text.data,
addr_text_max_len, 0);
}


static int
ngx_stream_lua_socket_resolve_retval_handler(ngx_stream_lua_request_t *r,
ngx_stream_lua_socket_tcp_upstream_t *u, lua_State *L)
Expand Down Expand Up @@ -3311,8 +3360,12 @@ ngx_stream_lua_socket_connected_handler(ngx_stream_lua_request_t *r,
llcf = ngx_stream_lua_get_module_loc_conf(r, ngx_stream_lua_module);

if (llcf->log_socket_errors) {
ngx_stream_lua_socket_init_peer_connection_addr_text(&u->peer);

ngx_log_error(NGX_LOG_ERR, r->connection->log, 0,
"stream lua tcp socket connect timed out");
"stream lua tcp socket connect timed out,"
" when connecting to %V:%ud",
&c->addr_text, ngx_inet_get_port(u->peer.sockaddr));
}

ngx_stream_lua_socket_handle_conn_error(r, u,
Expand Down Expand Up @@ -4934,19 +4987,13 @@ ngx_stream_lua_socket_free_pool(ngx_log_t *log, ngx_stream_lua_socket_pool_t *sp
}


static int
ngx_stream_lua_socket_shutdown_pool(lua_State *L)
static void
ngx_stream_lua_socket_shutdown_pool_helper(ngx_stream_lua_socket_pool_t *spool)
{
ngx_queue_t *q;
ngx_connection_t *c;
ngx_stream_lua_socket_pool_t *spool;
ngx_queue_t *q;
ngx_connection_t *c;
ngx_stream_lua_socket_pool_item_t *item;

spool = lua_touserdata(L, 1);
if (spool == NULL) {
return 0;
}

while (!ngx_queue_empty(&spool->cache)) {
q = ngx_queue_head(&spool->cache);

Expand All @@ -4960,6 +5007,19 @@ ngx_stream_lua_socket_shutdown_pool(lua_State *L)
}

spool->active_connections = 0;
}


static int
ngx_stream_lua_socket_shutdown_pool(lua_State *L)
{
ngx_stream_lua_socket_pool_t *spool;

spool = lua_touserdata(L, 1);

if (spool != NULL) {
ngx_stream_lua_socket_shutdown_pool_helper(spool);
}

return 0;
}
Expand Down Expand Up @@ -5376,10 +5436,7 @@ ngx_stream_lua_ssl_free_session(lua_State *L)
void
ngx_stream_lua_cleanup_conn_pools(lua_State *L)
{
ngx_queue_t *q;
ngx_connection_t *c;
ngx_stream_lua_socket_pool_t *spool;
ngx_stream_lua_socket_pool_item_t *item;

lua_pushlightuserdata(L, &ngx_stream_lua_socket_pool_key);
lua_rawget(L, LUA_REGISTRYINDEX); /* table */
Expand All @@ -5389,18 +5446,12 @@ ngx_stream_lua_cleanup_conn_pools(lua_State *L)
/* tb key val */
spool = lua_touserdata(L, -1);

if (!ngx_queue_empty(&spool->cache)) {
q = ngx_queue_head(&spool->cache);
item = ngx_queue_data(q, ngx_stream_lua_socket_pool_item_t, queue);
c = item->connection;

ngx_stream_lua_socket_tcp_close_connection(c);

ngx_queue_remove(q);
if (spool != NULL) {
ngx_log_debug2(NGX_LOG_DEBUG_STREAM, ngx_cycle->log, 0,
"stream lua tcp socket keepalive: free connection pool %p "
"for \"%s\"", spool, spool->key);

ngx_log_debug1(NGX_LOG_DEBUG_STREAM, ngx_cycle->log, 0,
"stream lua tcp socket keepalive: free connection pool "
"for \"%s\"", spool->key);
ngx_stream_lua_socket_shutdown_pool_helper(spool);
}

lua_pop(L, 1);
Expand Down
4 changes: 2 additions & 2 deletions src/ngx_stream_lua_socket_udp.c
Original file line number Diff line number Diff line change
Expand Up @@ -225,7 +225,7 @@ ngx_stream_lua_socket_udp_setpeername(lua_State *L)
if (n == 3) {
port = luaL_checkinteger(L, 3);

if (port < 0 || port > 65536) {
if (port < 0 || port > 65535) {
lua_pushnil(L);
lua_pushfstring(L, "bad port number: %d", port);
return 2;
Expand Down Expand Up @@ -596,7 +596,7 @@ ngx_stream_lua_socket_resolve_handler(ngx_resolver_ctx_t *ctx)
#else
/* for nginx older than 1.5.8 */

len = NGX_INET_ADDRSTRLEN + sizeof(":65536") - 1;
len = NGX_INET_ADDRSTRLEN + sizeof(":65535") - 1;

p = ngx_pnalloc(r->pool, len + sizeof(struct sockaddr_in));
if (p == NULL) {
Expand Down
6 changes: 6 additions & 0 deletions src/ngx_stream_lua_util.c
Original file line number Diff line number Diff line change
Expand Up @@ -3242,6 +3242,12 @@ ngx_stream_lua_init_vm(lua_State *parent_vm, ngx_cycle_t *cycle,

cln->data = state;

if (lmcf->vm_cleanup == NULL) {
/* this assignment will happen only once,
* and also only for the main Lua VM */
lmcf->vm_cleanup = cln;
}

if (pcln) {
*pcln = cln;
}
Expand Down
30 changes: 30 additions & 0 deletions src/ngx_stream_lua_util.h
Original file line number Diff line number Diff line change
Expand Up @@ -446,6 +446,36 @@ ngx_stream_lua_get_flush_chain(ngx_stream_lua_request_t *r, ngx_stream_lua_ctx_t
}


#if (nginx_version < 1011002)
static ngx_inline in_port_t
ngx_inet_get_port(struct sockaddr *sa)
{
struct sockaddr_in *sin;
#if (NGX_HAVE_INET6)
struct sockaddr_in6 *sin6;
#endif

switch (sa->sa_family) {

#if (NGX_HAVE_INET6)
case AF_INET6:
sin6 = (struct sockaddr_in6 *) sa;
return ntohs(sin6->sin6_port);
#endif

#if (NGX_HAVE_UNIX_DOMAIN)
case AF_UNIX:
return 0;
#endif

default: /* AF_INET */
sin = (struct sockaddr_in *) sa;
return ntohs(sin->sin_port);
}
}
#endif


extern ngx_uint_t ngx_stream_lua_location_hash;
extern ngx_uint_t ngx_stream_lua_content_length_hash;

Expand Down
Loading

0 comments on commit 9cbd9df

Please sign in to comment.