Skip to content

Commit af16d1f

Browse files
committed
feature: added the Lua global __ngx_cycle which is a lightuserdata holding the current ngx_cycle_t pointer.
1 parent f24a96b commit af16d1f

File tree

2 files changed

+57
-4
lines changed

2 files changed

+57
-4
lines changed

src/ngx_http_lua_util.c

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -81,7 +81,7 @@ ngx_uint_t ngx_http_lua_content_length_hash = 0;
8181
static ngx_int_t ngx_http_lua_send_http10_headers(ngx_http_request_t *r,
8282
ngx_http_lua_ctx_t *ctx);
8383
static void ngx_http_lua_init_registry(lua_State *L, ngx_log_t *log);
84-
static void ngx_http_lua_init_globals(lua_State *L,
84+
static void ngx_http_lua_init_globals(lua_State *L, ngx_cycle_t *cycle,
8585
ngx_http_lua_main_conf_t *lmcf, ngx_log_t *log);
8686
static void ngx_http_lua_set_path(ngx_cycle_t *cycle, lua_State *L, int tab_idx,
8787
const char *fieldname, const char *path, const char *default_path,
@@ -294,7 +294,7 @@ ngx_http_lua_new_state(lua_State *parent_vm, ngx_cycle_t *cycle,
294294
lua_pop(L, 1); /* remove the "package" table */
295295

296296
ngx_http_lua_init_registry(L, log);
297-
ngx_http_lua_init_globals(L, lmcf, log);
297+
ngx_http_lua_init_globals(L, cycle, lmcf, log);
298298

299299
return L;
300300
}
@@ -683,12 +683,15 @@ ngx_http_lua_init_registry(lua_State *L, ngx_log_t *log)
683683

684684

685685
static void
686-
ngx_http_lua_init_globals(lua_State *L, ngx_http_lua_main_conf_t *lmcf,
687-
ngx_log_t *log)
686+
ngx_http_lua_init_globals(lua_State *L, ngx_cycle_t *cycle,
687+
ngx_http_lua_main_conf_t *lmcf, ngx_log_t *log)
688688
{
689689
ngx_log_debug0(NGX_LOG_DEBUG_HTTP, log, 0,
690690
"lua initializing lua globals");
691691

692+
lua_pushlightuserdata(L, cycle);
693+
lua_setglobal(L, "__ngx_cycle");
694+
692695
#if defined(NDK) && NDK
693696
ngx_http_lua_inject_ndk_api(L);
694697
#endif /* defined(NDK) && NDK */

t/130-internal-api.t

Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
1+
# vim:set ft= ts=4 sw=4 et fdm=marker:
2+
use lib 'lib';
3+
use Test::Nginx::Socket::Lua;
4+
5+
#worker_connections(1014);
6+
#master_process_enabled(1);
7+
#log_level('warn');
8+
9+
repeat_each(2);
10+
11+
plan tests => repeat_each() * 3;
12+
13+
#no_diff();
14+
no_long_string();
15+
#master_on();
16+
#workers(2);
17+
18+
run_tests();
19+
20+
__DATA__
21+
22+
=== TEST 1: __ngx_req and __ngx_cycle
23+
--- http_config
24+
init_by_lua '
25+
my_cycle = __ngx_cycle
26+
';
27+
28+
--- config
29+
location = /t {
30+
content_by_lua '
31+
local ffi = require "ffi"
32+
local function tonum(ud)
33+
return tonumber(ffi.cast("uintptr_t", ud))
34+
end
35+
ngx.say(string.format("init: cycle=%#x", tonum(my_cycle)))
36+
ngx.say(string.format("content cycle=%#x", tonum(__ngx_cycle)))
37+
ngx.say(string.format("content req=%#x", tonum(__ngx_req)))
38+
';
39+
}
40+
--- request
41+
GET /t
42+
43+
--- response_body_like chop
44+
^init: cycle=(0x[a-f0-9]{4,})
45+
content cycle=\1
46+
content req=0x[a-f0-9]{4,}
47+
$
48+
--- no_error_log
49+
[error]
50+

0 commit comments

Comments
 (0)