Skip to content

Commit 246ec8a

Browse files
committed
refactor: simplified Lua chunk and file cache key generation.
1 parent f45908c commit 246ec8a

7 files changed

+211
-279
lines changed

src/ngx_http_lua_balancer.c

Lines changed: 14 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -125,7 +125,7 @@ char *
125125
ngx_http_lua_balancer_by_lua(ngx_conf_t *cf, ngx_command_t *cmd,
126126
void *conf)
127127
{
128-
u_char *p;
128+
u_char *cache_key = NULL;
129129
u_char *name;
130130
ngx_str_t *value;
131131
ngx_http_lua_srv_conf_t *lscf = conf;
@@ -149,47 +149,35 @@ ngx_http_lua_balancer_by_lua(ngx_conf_t *cf, ngx_command_t *cmd,
149149

150150
if (cmd->post == ngx_http_lua_balancer_handler_file) {
151151
/* Lua code in an external file */
152-
153152
name = ngx_http_lua_rebase_path(cf->pool, value[1].data,
154153
value[1].len);
155154
if (name == NULL) {
156155
return NGX_CONF_ERROR;
157156
}
158157

159-
lscf->balancer.src.data = name;
160-
lscf->balancer.src.len = ngx_strlen(name);
161-
162-
p = ngx_palloc(cf->pool, NGX_HTTP_LUA_FILE_KEY_LEN + 1);
163-
if (p == NULL) {
158+
cache_key = ngx_http_lua_gen_file_cache_key(cf, value[1].data,
159+
value[1].len);
160+
if (cache_key == NULL) {
164161
return NGX_CONF_ERROR;
165162
}
166163

167-
lscf->balancer.src_key = p;
168-
169-
p = ngx_copy(p, NGX_HTTP_LUA_FILE_TAG, NGX_HTTP_LUA_FILE_TAG_LEN);
170-
p = ngx_http_lua_digest_hex(p, value[1].data, value[1].len);
171-
*p = '\0';
164+
lscf->balancer.src.data = name;
165+
lscf->balancer.src.len = ngx_strlen(name);
172166

173167
} else {
174-
/* inlined Lua code */
175-
176-
lscf->balancer.src = value[1];
177-
178-
p = ngx_palloc(cf->pool,
179-
sizeof("balancer_by_lua_") +
180-
NGX_HTTP_LUA_INLINE_KEY_LEN);
181-
if (p == NULL) {
168+
cache_key = ngx_http_lua_gen_chunk_cache_key(cf, "balancer_by_lua",
169+
value[1].data,
170+
value[1].len);
171+
if (cache_key == NULL) {
182172
return NGX_CONF_ERROR;
183173
}
184174

185-
lscf->balancer.src_key = p;
186-
187-
p = ngx_copy(p, "balancer_by_lua_", sizeof("balancer_by_lua_") - 1);
188-
p = ngx_copy(p, NGX_HTTP_LUA_INLINE_TAG, NGX_HTTP_LUA_INLINE_TAG_LEN);
189-
p = ngx_http_lua_digest_hex(p, value[1].data, value[1].len);
190-
*p = '\0';
175+
/* Don't eval nginx variables for inline lua code */
176+
lscf->balancer.src = value[1];
191177
}
192178

179+
lscf->balancer.src_key = cache_key;
180+
193181
uscf = ngx_http_conf_get_module_srv_conf(cf, ngx_http_upstream_module);
194182

195183
if (uscf->peer.init_upstream) {

src/ngx_http_lua_cache.c

Lines changed: 66 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,10 @@
1919
#include "ngx_http_lua_util.h"
2020

2121

22+
static u_char *ngx_http_lua_gen_file_cache_key_helper(u_char *out,
23+
const u_char *src, size_t src_len);
24+
25+
2226
/**
2327
* Find code chunk associated with the given key in code cache,
2428
* and push it to the top of Lua stack if found.
@@ -260,7 +264,6 @@ ngx_http_lua_cache_loadfile(ngx_log_t *log, lua_State *L,
260264
{
261265
int n;
262266
ngx_int_t rc, errcode = NGX_ERROR;
263-
u_char *p;
264267
u_char buf[NGX_HTTP_LUA_FILE_KEY_LEN + 1];
265268
const char *err = NULL;
266269

@@ -270,11 +273,8 @@ ngx_http_lua_cache_loadfile(ngx_log_t *log, lua_State *L,
270273
if (cache_key == NULL) {
271274
dd("CACHE file key not pre-calculated...calculating");
272275

273-
p = ngx_copy(buf, NGX_HTTP_LUA_FILE_TAG, NGX_HTTP_LUA_FILE_TAG_LEN);
274-
p = ngx_http_lua_digest_hex(p, script, ngx_strlen(script));
275-
*p = '\0';
276-
277-
cache_key = buf;
276+
cache_key = ngx_http_lua_gen_file_cache_key_helper(buf, script,
277+
ngx_strlen(script));
278278
*cache_ref = LUA_NOREF;
279279

280280
} else {
@@ -341,4 +341,64 @@ ngx_http_lua_cache_loadfile(ngx_log_t *log, lua_State *L,
341341
return errcode;
342342
}
343343

344+
345+
u_char *
346+
ngx_http_lua_gen_chunk_cache_key(ngx_conf_t *cf, const char *tag,
347+
const u_char *src, size_t src_len)
348+
{
349+
u_char *p, *out;
350+
size_t tag_len;
351+
352+
tag_len = ngx_strlen(tag);
353+
354+
out = ngx_palloc(cf->pool, tag_len + NGX_HTTP_LUA_INLINE_KEY_LEN + 2);
355+
if (out == NULL) {
356+
return NULL;
357+
}
358+
359+
p = ngx_copy(out, tag, tag_len);
360+
p = ngx_copy(p, "_", 1);
361+
p = ngx_copy(p, NGX_HTTP_LUA_INLINE_TAG, NGX_HTTP_LUA_INLINE_TAG_LEN);
362+
p = ngx_http_lua_digest_hex(p, src, src_len);
363+
*p = '\0';
364+
365+
return out;
366+
}
367+
368+
369+
static u_char *
370+
ngx_http_lua_gen_file_cache_key_helper(u_char *out, const u_char *src,
371+
size_t src_len)
372+
{
373+
u_char *p;
374+
375+
ngx_http_lua_assert(out != NULL);
376+
377+
if (out == NULL) {
378+
return NULL;
379+
}
380+
381+
p = ngx_copy(out, NGX_HTTP_LUA_FILE_TAG, NGX_HTTP_LUA_FILE_TAG_LEN);
382+
p = ngx_http_lua_digest_hex(p, src, src_len);
383+
*p = '\0';
384+
385+
return out;
386+
}
387+
388+
389+
u_char *
390+
ngx_http_lua_gen_file_cache_key(ngx_conf_t *cf, const u_char *src,
391+
size_t src_len)
392+
{
393+
u_char *out;
394+
395+
out = ngx_palloc(cf->pool, NGX_HTTP_LUA_FILE_TAG_LEN + 1);
396+
if (out == NULL) {
397+
return NULL;
398+
}
399+
400+
return ngx_http_lua_gen_file_cache_key_helper(out, src, src_len);
401+
}
402+
403+
344404
/* vi:set ft=c ts=4 sw=4 et fdm=marker: */

src/ngx_http_lua_cache.h

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,10 @@ ngx_int_t ngx_http_lua_cache_loadbuffer(ngx_log_t *log, lua_State *L,
1717
const char *name);
1818
ngx_int_t ngx_http_lua_cache_loadfile(ngx_log_t *log, lua_State *L,
1919
const u_char *script, int *cache_ref, const u_char *cache_key);
20+
u_char *ngx_http_lua_gen_chunk_cache_key(ngx_conf_t *cf, const char *tag,
21+
const u_char *src, size_t src_len);
22+
u_char *ngx_http_lua_gen_file_cache_key(ngx_conf_t *cf, const u_char *src,
23+
size_t src_len);
2024

2125

2226
#endif /* _NGX_HTTP_LUA_CACHE_H_INCLUDED_ */

0 commit comments

Comments
 (0)