Skip to content

Commit 1d9a771

Browse files
ghedoagentzh
authored andcommitted
refactor: lua code cache: make load functions take a log directly.
This patch makes the functions ngx_http_lua_cache_loadbuffer() and ngx_http_lua_cache_loadfile() take an ngx_log_t* argument directly instead of an ngx_http_request_t* so that they can be used in contexts where an HTTP request is not available.
1 parent 2c40455 commit 1d9a771

9 files changed

+34
-24
lines changed

src/ngx_http_lua_accessby.c

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -176,7 +176,8 @@ ngx_http_lua_access_handler_inline(ngx_http_request_t *r)
176176
L = ngx_http_lua_get_lua_vm(r, NULL);
177177

178178
/* load Lua inline script (w/ cache) sp = 1 */
179-
rc = ngx_http_lua_cache_loadbuffer(r, L, llcf->access_src.value.data,
179+
rc = ngx_http_lua_cache_loadbuffer(r->connection->log, L,
180+
llcf->access_src.value.data,
180181
llcf->access_src.value.len,
181182
llcf->access_src_key,
182183
(const char *) llcf->access_chunkname);
@@ -215,7 +216,8 @@ ngx_http_lua_access_handler_file(ngx_http_request_t *r)
215216
L = ngx_http_lua_get_lua_vm(r, NULL);
216217

217218
/* load Lua script file (w/ cache) sp = 1 */
218-
rc = ngx_http_lua_cache_loadfile(r, L, script_path, llcf->access_src_key);
219+
rc = ngx_http_lua_cache_loadfile(r->connection->log, L, script_path,
220+
llcf->access_src_key);
219221
if (rc != NGX_OK) {
220222
if (rc < NGX_HTTP_SPECIAL_RESPONSE) {
221223
return NGX_HTTP_INTERNAL_SERVER_ERROR;

src/ngx_http_lua_bodyfilterby.c

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -160,7 +160,8 @@ ngx_http_lua_body_filter_inline(ngx_http_request_t *r, ngx_chain_t *in)
160160
L = ngx_http_lua_get_lua_vm(r, NULL);
161161

162162
/* load Lua inline script (w/ cache) sp = 1 */
163-
rc = ngx_http_lua_cache_loadbuffer(r, L, llcf->body_filter_src.value.data,
163+
rc = ngx_http_lua_cache_loadbuffer(r->connection->log, L,
164+
llcf->body_filter_src.value.data,
164165
llcf->body_filter_src.value.len,
165166
llcf->body_filter_src_key,
166167
"=body_filter_by_lua");
@@ -208,7 +209,7 @@ ngx_http_lua_body_filter_file(ngx_http_request_t *r, ngx_chain_t *in)
208209
L = ngx_http_lua_get_lua_vm(r, NULL);
209210

210211
/* load Lua script file (w/ cache) sp = 1 */
211-
rc = ngx_http_lua_cache_loadfile(r, L, script_path,
212+
rc = ngx_http_lua_cache_loadfile(r->connection->log, L, script_path,
212213
llcf->body_filter_src_key);
213214
if (rc != NGX_OK) {
214215
return NGX_ERROR;

src/ngx_http_lua_cache.c

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,7 @@
3232
*
3333
* */
3434
static ngx_int_t
35-
ngx_http_lua_cache_load_code(ngx_http_request_t *r, lua_State *L,
35+
ngx_http_lua_cache_load_code(ngx_log_t *log, lua_State *L,
3636
const char *key)
3737
{
3838
int rc;
@@ -68,7 +68,7 @@ ngx_http_lua_cache_load_code(ngx_http_request_t *r, lua_State *L,
6868
err = (u_char *) "unknown error";
6969
}
7070

71-
ngx_log_error(NGX_LOG_ERR, r->connection->log, 0,
71+
ngx_log_error(NGX_LOG_ERR, log, 0,
7272
"lua: failed to run factory at key \"%s\": %s",
7373
key, err);
7474
lua_pop(L, 2);
@@ -133,7 +133,7 @@ ngx_http_lua_cache_store_code(lua_State *L, const char *key)
133133

134134

135135
ngx_int_t
136-
ngx_http_lua_cache_loadbuffer(ngx_http_request_t *r, lua_State *L,
136+
ngx_http_lua_cache_loadbuffer(ngx_log_t *log, lua_State *L,
137137
const u_char *src, size_t src_len, const u_char *cache_key,
138138
const char *name)
139139
{
@@ -145,7 +145,7 @@ ngx_http_lua_cache_loadbuffer(ngx_http_request_t *r, lua_State *L,
145145

146146
dd("XXX cache key: [%s]", cache_key);
147147

148-
rc = ngx_http_lua_cache_load_code(r, L, (char *) cache_key);
148+
rc = ngx_http_lua_cache_load_code(log, L, (char *) cache_key);
149149
if (rc == NGX_OK) {
150150
/* code chunk loaded from cache, sp++ */
151151
dd("Code cache hit! cache key='%s', stack top=%d, script='%.*s'",
@@ -194,15 +194,15 @@ ngx_http_lua_cache_loadbuffer(ngx_http_request_t *r, lua_State *L,
194194

195195
error:
196196

197-
ngx_log_error(NGX_LOG_ERR, r->connection->log, 0,
197+
ngx_log_error(NGX_LOG_ERR, log, 0,
198198
"failed to load inlined Lua code: %s", err);
199199
lua_settop(L, n);
200200
return NGX_ERROR;
201201
}
202202

203203

204204
ngx_int_t
205-
ngx_http_lua_cache_loadfile(ngx_http_request_t *r, lua_State *L,
205+
ngx_http_lua_cache_loadfile(ngx_log_t *log, lua_State *L,
206206
const u_char *script, const u_char *cache_key)
207207
{
208208
int n;
@@ -229,7 +229,7 @@ ngx_http_lua_cache_loadfile(ngx_http_request_t *r, lua_State *L,
229229

230230
dd("XXX cache key for file: [%s]", cache_key);
231231

232-
rc = ngx_http_lua_cache_load_code(r, L, (char *) cache_key);
232+
rc = ngx_http_lua_cache_load_code(log, L, (char *) cache_key);
233233
if (rc == NGX_OK) {
234234
/* code chunk loaded from cache, sp++ */
235235
dd("Code cache hit! cache key='%s', stack top=%d, file path='%s'",
@@ -286,7 +286,7 @@ ngx_http_lua_cache_loadfile(ngx_http_request_t *r, lua_State *L,
286286

287287
error:
288288

289-
ngx_log_error(NGX_LOG_ERR, r->connection->log, 0,
289+
ngx_log_error(NGX_LOG_ERR, log, 0,
290290
"failed to load external Lua file \"%s\": %s", script, err);
291291

292292
lua_settop(L, n);

src/ngx_http_lua_cache.h

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -12,10 +12,10 @@
1212
#include "ngx_http_lua_common.h"
1313

1414

15-
ngx_int_t ngx_http_lua_cache_loadbuffer(ngx_http_request_t *r, lua_State *L,
15+
ngx_int_t ngx_http_lua_cache_loadbuffer(ngx_log_t *log, lua_State *L,
1616
const u_char *src, size_t src_len, const u_char *cache_key,
1717
const char *name);
18-
ngx_int_t ngx_http_lua_cache_loadfile(ngx_http_request_t *r, lua_State *L,
18+
ngx_int_t ngx_http_lua_cache_loadfile(ngx_log_t *log, lua_State *L,
1919
const u_char *script, const u_char *cache_key);
2020

2121

src/ngx_http_lua_contentby.c

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -251,7 +251,7 @@ ngx_http_lua_content_handler_file(ngx_http_request_t *r)
251251
L = ngx_http_lua_get_lua_vm(r, NULL);
252252

253253
/* load Lua script file (w/ cache) sp = 1 */
254-
rc = ngx_http_lua_cache_loadfile(r, L, script_path,
254+
rc = ngx_http_lua_cache_loadfile(r->connection->log, L, script_path,
255255
llcf->content_src_key);
256256
if (rc != NGX_OK) {
257257
if (rc < NGX_HTTP_SPECIAL_RESPONSE) {
@@ -280,7 +280,8 @@ ngx_http_lua_content_handler_inline(ngx_http_request_t *r)
280280
L = ngx_http_lua_get_lua_vm(r, NULL);
281281

282282
/* load Lua inline script (w/ cache) sp = 1 */
283-
rc = ngx_http_lua_cache_loadbuffer(r, L, llcf->content_src.value.data,
283+
rc = ngx_http_lua_cache_loadbuffer(r->connection->log, L,
284+
llcf->content_src.value.data,
284285
llcf->content_src.value.len,
285286
llcf->content_src_key,
286287
(const char *)

src/ngx_http_lua_directive.c

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -354,7 +354,8 @@ ngx_http_lua_filter_set_by_lua_inline(ngx_http_request_t *r, ngx_str_t *val,
354354
L = ngx_http_lua_get_lua_vm(r, NULL);
355355

356356
/* load Lua inline script (w/ cache) sp = 1 */
357-
rc = ngx_http_lua_cache_loadbuffer(r, L, filter_data->script.data,
357+
rc = ngx_http_lua_cache_loadbuffer(r->connection->log, L,
358+
filter_data->script.data,
358359
filter_data->script.len,
359360
filter_data->key, "=set_by_lua");
360361
if (rc != NGX_OK) {
@@ -407,7 +408,8 @@ ngx_http_lua_filter_set_by_lua_file(ngx_http_request_t *r, ngx_str_t *val,
407408
L = ngx_http_lua_get_lua_vm(r, NULL);
408409

409410
/* load Lua script file (w/ cache) sp = 1 */
410-
rc = ngx_http_lua_cache_loadfile(r, L, script_path, filter_data->key);
411+
rc = ngx_http_lua_cache_loadfile(r->connection->log, L, script_path,
412+
filter_data->key);
411413
if (rc != NGX_OK) {
412414
return NGX_ERROR;
413415
}

src/ngx_http_lua_headerfilterby.c

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -168,7 +168,7 @@ ngx_http_lua_header_filter_inline(ngx_http_request_t *r)
168168
L = ngx_http_lua_get_lua_vm(r, NULL);
169169

170170
/* load Lua inline script (w/ cache) sp = 1 */
171-
rc = ngx_http_lua_cache_loadbuffer(r, L,
171+
rc = ngx_http_lua_cache_loadbuffer(r->connection->log, L,
172172
llcf->header_filter_src.value.data,
173173
llcf->header_filter_src.value.len,
174174
llcf->header_filter_src_key,
@@ -211,7 +211,7 @@ ngx_http_lua_header_filter_file(ngx_http_request_t *r)
211211
L = ngx_http_lua_get_lua_vm(r, NULL);
212212

213213
/* load Lua script file (w/ cache) sp = 1 */
214-
rc = ngx_http_lua_cache_loadfile(r, L, script_path,
214+
rc = ngx_http_lua_cache_loadfile(r->connection->log, L, script_path,
215215
llcf->header_filter_src_key);
216216
if (rc != NGX_OK) {
217217
return NGX_ERROR;

src/ngx_http_lua_logby.c

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -113,7 +113,8 @@ ngx_http_lua_log_handler_inline(ngx_http_request_t *r)
113113
L = ngx_http_lua_get_lua_vm(r, NULL);
114114

115115
/* load Lua inline script (w/ cache) sp = 1 */
116-
rc = ngx_http_lua_cache_loadbuffer(r, L, llcf->log_src.value.data,
116+
rc = ngx_http_lua_cache_loadbuffer(r->connection->log, L,
117+
llcf->log_src.value.data,
117118
llcf->log_src.value.len,
118119
llcf->log_src_key,
119120
(const char *) llcf->log_chunkname);
@@ -150,7 +151,8 @@ ngx_http_lua_log_handler_file(ngx_http_request_t *r)
150151
L = ngx_http_lua_get_lua_vm(r, NULL);
151152

152153
/* load Lua script file (w/ cache) sp = 1 */
153-
rc = ngx_http_lua_cache_loadfile(r, L, script_path, llcf->log_src_key);
154+
rc = ngx_http_lua_cache_loadfile(r->connection->log, L, script_path,
155+
llcf->log_src_key);
154156
if (rc != NGX_OK) {
155157
return NGX_ERROR;
156158
}

src/ngx_http_lua_rewriteby.c

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -176,7 +176,8 @@ ngx_http_lua_rewrite_handler_inline(ngx_http_request_t *r)
176176
L = ngx_http_lua_get_lua_vm(r, NULL);
177177

178178
/* load Lua inline script (w/ cache) sp = 1 */
179-
rc = ngx_http_lua_cache_loadbuffer(r, L, llcf->rewrite_src.value.data,
179+
rc = ngx_http_lua_cache_loadbuffer(r->connection->log, L,
180+
llcf->rewrite_src.value.data,
180181
llcf->rewrite_src.value.len,
181182
llcf->rewrite_src_key,
182183
(const char *)
@@ -214,7 +215,8 @@ ngx_http_lua_rewrite_handler_file(ngx_http_request_t *r)
214215
L = ngx_http_lua_get_lua_vm(r, NULL);
215216

216217
/* load Lua script file (w/ cache) sp = 1 */
217-
rc = ngx_http_lua_cache_loadfile(r, L, script_path, llcf->rewrite_src_key);
218+
rc = ngx_http_lua_cache_loadfile(r->connection->log, L, script_path,
219+
llcf->rewrite_src_key);
218220
if (rc != NGX_OK) {
219221
if (rc < NGX_HTTP_SPECIAL_RESPONSE) {
220222
return NGX_HTTP_INTERNAL_SERVER_ERROR;

0 commit comments

Comments
 (0)