Skip to content

Commit 5d06341

Browse files
committed
Merge branch 'master' of github.com:openresty/lua-nginx-module
2 parents 4720940 + 3085f32 commit 5d06341

9 files changed

+128
-73
lines changed

src/ngx_http_lua_initworkerby.c

Lines changed: 1 addition & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -207,7 +207,7 @@ ngx_http_lua_init_worker(ngx_cycle_t *cycle)
207207
ngx_destroy_pool(conf.temp_pool);
208208
conf.temp_pool = NULL;
209209

210-
c = ngx_http_lua_create_fake_connection();
210+
c = ngx_http_lua_create_fake_connection(NULL);
211211
if (c == NULL) {
212212
goto failed;
213213
}
@@ -261,7 +261,6 @@ ngx_http_lua_init_worker(ngx_cycle_t *cycle)
261261

262262
(void) lmcf->init_worker_handler(cycle->log, lmcf, lmcf->lua);
263263

264-
ngx_destroy_pool(r->pool);
265264
ngx_destroy_pool(c->pool);
266265
return NGX_OK;
267266

@@ -271,10 +270,6 @@ ngx_http_lua_init_worker(ngx_cycle_t *cycle)
271270
ngx_destroy_pool(conf.temp_pool);
272271
}
273272

274-
if (r && r->pool) {
275-
ngx_destroy_pool(r->pool);
276-
}
277-
278273
if (c) {
279274
ngx_http_lua_close_fake_connection(c);
280275
}

src/ngx_http_lua_sleep.c

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -102,8 +102,10 @@ ngx_http_lua_sleep_handler(ngx_event_t *ev)
102102
return;
103103
}
104104

105-
log_ctx = c->log->data;
106-
log_ctx->current_request = r;
105+
if (c->fd != -1) { /* not a fake connection */
106+
log_ctx = c->log->data;
107+
log_ctx->current_request = r;
108+
}
107109

108110
coctx->cleanup = NULL;
109111

src/ngx_http_lua_socket_tcp.c

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2687,8 +2687,10 @@ ngx_http_lua_socket_tcp_handler(ngx_event_t *ev)
26872687
r = u->request;
26882688
c = r->connection;
26892689

2690-
ctx = c->log->data;
2691-
ctx->current_request = r;
2690+
if (c->fd != -1) { /* not a fake connection */
2691+
ctx = c->log->data;
2692+
ctx->current_request = r;
2693+
}
26922694

26932695
ngx_log_debug3(NGX_LOG_DEBUG_HTTP, c->log, 0,
26942696
"lua tcp socket handler for \"%V?%V\", wev %d", &r->uri,

src/ngx_http_lua_socket_udp.c

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1299,8 +1299,10 @@ ngx_http_lua_socket_udp_handler(ngx_event_t *ev)
12991299
r = u->request;
13001300
c = r->connection;
13011301

1302-
ctx = c->log->data;
1303-
ctx->current_request = r;
1302+
if (c->fd != -1) { /* not a fake connection */
1303+
ctx = c->log->data;
1304+
ctx->current_request = r;
1305+
}
13041306

13051307
ngx_log_debug3(NGX_LOG_DEBUG_HTTP, c->log, 0,
13061308
"lua udp socket handler for \"%V?%V\", wev %d", &r->uri,

src/ngx_http_lua_timer.c

Lines changed: 94 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -26,12 +26,22 @@ typedef struct {
2626
void **srv_conf;
2727
void **loc_conf;
2828

29+
ngx_pool_t *pool;
30+
31+
ngx_listening_t *listening;
32+
ngx_str_t client_addr_text;
33+
2934
ngx_http_lua_main_conf_t *lmcf;
3035
ngx_http_lua_vm_state_t *vm_state;
3136

3237
} ngx_http_lua_timer_ctx_t;
3338

3439

40+
typedef struct {
41+
ngx_connection_t *connection;
42+
} ngx_http_lua_timer_log_ctx_t;
43+
44+
3545
static int ngx_http_lua_ngx_timer_at(lua_State *L);
3646
static void ngx_http_lua_timer_handler(ngx_event_t *ev);
3747
static u_char * ngx_http_lua_log_timer_error(ngx_log_t *log, u_char *buf,
@@ -67,7 +77,7 @@ ngx_http_lua_ngx_timer_at(lua_State *L)
6777
ngx_http_connection_t *hc;
6878
#endif
6979

70-
ngx_http_lua_timer_ctx_t *tctx;
80+
ngx_http_lua_timer_ctx_t *tctx = NULL;
7181
ngx_http_lua_main_conf_t *lmcf;
7282
#if 0
7383
ngx_http_core_main_conf_t *cmcf;
@@ -201,10 +211,7 @@ ngx_http_lua_ngx_timer_at(lua_State *L)
201211
p = ngx_alloc(sizeof(ngx_event_t) + sizeof(ngx_http_lua_timer_ctx_t),
202212
r->connection->log);
203213
if (p == NULL) {
204-
lua_pushlightuserdata(L, &ngx_http_lua_coroutines_key);
205-
lua_rawget(L, LUA_REGISTRYINDEX);
206-
luaL_unref(L, -1, co_ref);
207-
return luaL_error(L, "no memory");
214+
goto nomem;
208215
}
209216

210217
ev = (ngx_event_t *) p;
@@ -223,6 +230,34 @@ ngx_http_lua_ngx_timer_at(lua_State *L)
223230
tctx->loc_conf = r->loc_conf;
224231
tctx->lmcf = lmcf;
225232

233+
tctx->pool = ngx_create_pool(128, ngx_cycle->log);
234+
if (tctx->pool == NULL) {
235+
goto nomem;
236+
}
237+
238+
if (r->connection) {
239+
tctx->listening = r->connection->listening;
240+
241+
} else {
242+
tctx->listening = NULL;
243+
}
244+
245+
if (r->connection->addr_text.len) {
246+
tctx->client_addr_text.data = ngx_palloc(tctx->pool,
247+
r->connection->addr_text.len);
248+
if (tctx->client_addr_text.data == NULL) {
249+
goto nomem;
250+
}
251+
252+
ngx_memcpy(tctx->client_addr_text.data, r->connection->addr_text.data,
253+
r->connection->addr_text.len);
254+
tctx->client_addr_text.len = r->connection->addr_text.len;
255+
256+
} else {
257+
tctx->client_addr_text.len = 0;
258+
tctx->client_addr_text.data = NULL;
259+
}
260+
226261
if (ctx && ctx->vm_state) {
227262
tctx->vm_state = ctx->vm_state;
228263
tctx->vm_state->count++;
@@ -241,6 +276,18 @@ ngx_http_lua_ngx_timer_at(lua_State *L)
241276

242277
lua_pushinteger(L, 1);
243278
return 1;
279+
280+
nomem:
281+
282+
if (tctx && tctx->pool) {
283+
ngx_destroy_pool(tctx->pool);
284+
}
285+
286+
lua_pushlightuserdata(L, &ngx_http_lua_coroutines_key);
287+
lua_rawget(L, LUA_REGISTRYINDEX);
288+
luaL_unref(L, -1, co_ref);
289+
290+
return luaL_error(L, "no memory");
244291
}
245292

246293

@@ -259,6 +306,7 @@ ngx_http_lua_timer_handler(ngx_event_t *ev)
259306
ngx_http_lua_timer_ctx_t tctx;
260307
ngx_http_lua_main_conf_t *lmcf;
261308
ngx_http_core_loc_conf_t *clcf;
309+
ngx_http_lua_timer_log_ctx_t *logctx;
262310

263311
ngx_log_debug0(NGX_LOG_DEBUG_HTTP, ngx_cycle->log, 0,
264312
"lua ngx.timer expired");
@@ -278,12 +326,23 @@ ngx_http_lua_timer_handler(ngx_event_t *ev)
278326
goto failed;
279327
}
280328

281-
c = ngx_http_lua_create_fake_connection();
329+
c = ngx_http_lua_create_fake_connection(tctx.pool);
282330
if (c == NULL) {
283331
goto failed;
284332
}
285333

334+
logctx = ngx_palloc(c->pool, sizeof(ngx_http_lua_timer_log_ctx_t));
335+
if (logctx == NULL) {
336+
goto failed;
337+
}
338+
339+
logctx->connection = c;
340+
286341
c->log->handler = ngx_http_lua_log_timer_error;
342+
c->log->data = logctx;
343+
344+
c->listening = tctx.listening;
345+
c->addr_text = tctx.client_addr_text;
287346

288347
r = ngx_http_lua_create_fake_request(c);
289348
if (r == NULL) {
@@ -402,12 +461,11 @@ ngx_http_lua_timer_handler(ngx_event_t *ev)
402461
ngx_http_lua_cleanup_vm(tctx.vm_state);
403462
}
404463

405-
if (r && r->pool) {
406-
ngx_destroy_pool(r->pool);
407-
}
408-
409464
if (c) {
410465
ngx_http_lua_close_fake_connection(c);
466+
467+
} else if (tctx.pool) {
468+
ngx_destroy_pool(tctx.pool);
411469
}
412470
}
413471

@@ -416,14 +474,39 @@ static u_char *
416474
ngx_http_lua_log_timer_error(ngx_log_t *log, u_char *buf, size_t len)
417475
{
418476
u_char *p;
477+
ngx_connection_t *c;
478+
479+
ngx_http_lua_timer_log_ctx_t *ctx;
419480

420481
if (log->action) {
421482
p = ngx_snprintf(buf, len, " while %s", log->action);
422483
len -= p - buf;
423484
buf = p;
424485
}
425486

426-
return ngx_snprintf(buf, len, ", context: ngx.timer");
487+
ctx = log->data;
488+
489+
dd("ctx = %p", ctx);
490+
491+
p = ngx_snprintf(buf, len, ", context: ngx.timer");
492+
len -= p - buf;
493+
buf = p;
494+
495+
c = ctx->connection;
496+
497+
if (c->addr_text.len) {
498+
p = ngx_snprintf(buf, len, ", client: %V", &c->addr_text);
499+
len -= p - buf;
500+
buf = p;
501+
}
502+
503+
if (c && c->listening && c->listening->addr_text.len) {
504+
p = ngx_snprintf(buf, len, ", server: %V", &c->listening->addr_text);
505+
/* len -= p - buf; */
506+
buf = p;
507+
}
508+
509+
return buf;
427510
}
428511

429512

src/ngx_http_lua_util.c

Lines changed: 12 additions & 43 deletions
Original file line numberDiff line numberDiff line change
@@ -3584,7 +3584,6 @@ ngx_http_lua_free_fake_request(ngx_http_request_t *r)
35843584
{
35853585
ngx_log_t *log;
35863586
ngx_http_cleanup_t *cln;
3587-
ngx_http_log_ctx_t *ctx;
35883587

35893588
log = r->connection->log;
35903589

@@ -3603,15 +3602,9 @@ ngx_http_lua_free_fake_request(ngx_http_request_t *r)
36033602
}
36043603
}
36053604

3606-
/* the various request strings were allocated from r->pool */
3607-
ctx = log->data;
3608-
ctx->request = NULL;
3609-
36103605
r->request_line.len = 0;
36113606

36123607
r->connection->destroyed = 1;
3613-
3614-
ngx_destroy_pool(r->pool);
36153608
}
36163609

36173610

@@ -3755,12 +3748,11 @@ ngx_http_lua_cleanup_vm(void *data)
37553748

37563749

37573750
ngx_connection_t *
3758-
ngx_http_lua_create_fake_connection(void)
3751+
ngx_http_lua_create_fake_connection(ngx_pool_t *pool)
37593752
{
37603753
ngx_log_t *log;
37613754
ngx_connection_t *c;
37623755
ngx_connection_t *saved_c = NULL;
3763-
ngx_http_log_ctx_t *logctx;
37643756

37653757
/* (we temporarily use a valid fd (0) to make ngx_get_connection happy) */
37663758
if (ngx_cycle->files) {
@@ -3779,32 +3771,25 @@ ngx_http_lua_create_fake_connection(void)
37793771

37803772
c->fd = (ngx_socket_t) -1;
37813773

3782-
c->pool = ngx_create_pool(NGX_CYCLE_POOL_SIZE, c->log);
3783-
if (c->pool == NULL) {
3784-
goto failed;
3774+
if (pool) {
3775+
c->pool = pool;
3776+
3777+
} else {
3778+
c->pool = ngx_create_pool(128, c->log);
3779+
if (c->pool == NULL) {
3780+
goto failed;
3781+
}
37853782
}
37863783

37873784
log = ngx_pcalloc(c->pool, sizeof(ngx_log_t));
37883785
if (log == NULL) {
37893786
goto failed;
37903787
}
37913788

3792-
logctx = ngx_palloc(c->pool, sizeof(ngx_http_log_ctx_t));
3793-
if (logctx == NULL) {
3794-
goto failed;
3795-
}
3796-
3797-
dd("c pool allocated: %d", (int) (sizeof(ngx_log_t)
3798-
+ sizeof(ngx_http_log_ctx_t) + sizeof(ngx_http_request_t)));
3799-
3800-
logctx->connection = c;
3801-
logctx->request = NULL;
3802-
logctx->current_request = NULL;
3803-
38043789
c->log = log;
38053790
c->log->connection = c->number;
3806-
c->log->data = logctx;
38073791
c->log->action = NULL;
3792+
c->log->data = NULL;
38083793

38093794
c->log_error = NGX_ERROR_INFO;
38103795

@@ -3832,7 +3817,6 @@ ngx_http_lua_create_fake_connection(void)
38323817
ngx_http_request_t *
38333818
ngx_http_lua_create_fake_request(ngx_connection_t *c)
38343819
{
3835-
ngx_http_log_ctx_t *logctx;
38363820
ngx_http_request_t *r;
38373821

38383822
r = ngx_pcalloc(c->pool, sizeof(ngx_http_request_t));
@@ -3842,14 +3826,7 @@ ngx_http_lua_create_fake_request(ngx_connection_t *c)
38423826

38433827
c->requests++;
38443828

3845-
logctx = c->log->data;
3846-
logctx->request = r;
3847-
logctx->current_request = r;
3848-
3849-
r->pool = ngx_create_pool(NGX_CYCLE_POOL_SIZE, c->log);
3850-
if (r->pool == NULL) {
3851-
return NULL;
3852-
}
3829+
r->pool = c->pool;
38533830

38543831
dd("r pool allocated: %d", (int) (sizeof(ngx_http_lua_ctx_t)
38553832
+ sizeof(void *) * ngx_http_max_module + sizeof(ngx_http_cleanup_t)));
@@ -3880,7 +3857,7 @@ ngx_http_lua_create_fake_request(ngx_connection_t *c)
38803857

38813858
r->ctx = ngx_pcalloc(r->pool, sizeof(void *) * ngx_http_max_module);
38823859
if (r->ctx == NULL) {
3883-
goto failed;
3860+
return NULL;
38843861
}
38853862

38863863
#if 0
@@ -3915,14 +3892,6 @@ ngx_http_lua_create_fake_request(ngx_connection_t *c)
39153892
r->discard_body = 1;
39163893

39173894
return r;
3918-
3919-
failed:
3920-
3921-
if (r->pool) {
3922-
ngx_destroy_pool(r->pool);
3923-
}
3924-
3925-
return NULL;
39263895
}
39273896

39283897

src/ngx_http_lua_util.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -220,7 +220,7 @@ void ngx_http_lua_release_ngx_ctx_table(ngx_log_t *log, lua_State *L,
220220

221221
void ngx_http_lua_cleanup_vm(void *data);
222222

223-
ngx_connection_t * ngx_http_lua_create_fake_connection(void);
223+
ngx_connection_t * ngx_http_lua_create_fake_connection(ngx_pool_t *pool);
224224

225225
ngx_http_request_t * ngx_http_lua_create_fake_request(ngx_connection_t *c);
226226

0 commit comments

Comments
 (0)