@@ -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+
3545static int ngx_http_lua_ngx_timer_at (lua_State * L );
3646static void ngx_http_lua_timer_handler (ngx_event_t * ev );
3747static 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 *
416474ngx_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
0 commit comments