-
Notifications
You must be signed in to change notification settings - Fork 2k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
feature: implemented a configure_by_lua phase #1259
Open
thibaultcha
wants to merge
2
commits into
openresty:master
Choose a base branch
from
thibaultcha:feat/configure-by-lua
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from 1 commit
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,287 @@ | ||
|
||
/* | ||
* Copyright (C) Yichun Zhang (agentzh) | ||
* | ||
* Author: Thibault Charbonnier (thibaultcha) | ||
* I hereby assign copyright in this code to the lua-nginx-module project, | ||
* to be licensed under the same terms as the rest of the code. | ||
*/ | ||
|
||
|
||
#ifndef DDEBUG | ||
#define DDEBUG 0 | ||
#endif | ||
|
||
|
||
#include "ddebug.h" | ||
#include "ngx_http_lua_configureby.h" | ||
#include "ngx_http_lua_directive.h" | ||
#include "ngx_http_lua_util.h" | ||
#include "ngx_http_lua_shdict.h" | ||
#include "ngx_http_lua_api.h" | ||
|
||
|
||
static ngx_conf_t *cfp; | ||
|
||
|
||
char * | ||
ngx_http_lua_configure_by_lua_block(ngx_conf_t *cf, ngx_command_t *cmd, | ||
void *conf) | ||
{ | ||
char *rv; | ||
ngx_conf_t save; | ||
|
||
save = *cf; | ||
cf->handler = ngx_http_lua_configure_by_lua; | ||
cf->handler_conf = conf; | ||
|
||
rv = ngx_http_lua_conf_lua_block_parse(cf, cmd); | ||
|
||
*cf = save; | ||
|
||
return rv; | ||
} | ||
|
||
|
||
char * | ||
ngx_http_lua_configure_by_lua(ngx_conf_t *cf, ngx_command_t *cmd, | ||
void *conf) | ||
{ | ||
u_char *name; | ||
ngx_str_t *value; | ||
ngx_http_lua_main_conf_t *lmcf = conf; | ||
|
||
if (cmd->post == NULL) { | ||
return NGX_CONF_ERROR; | ||
} | ||
|
||
if (lmcf->configure_handler) { | ||
return "is duplicate"; | ||
} | ||
|
||
value = cf->args->elts; | ||
|
||
if (value[1].len == 0) { | ||
ngx_conf_log_error(NGX_LOG_ERR, cf, 0, | ||
"invalid location config: no runnable Lua code"); | ||
return NGX_CONF_ERROR; | ||
} | ||
|
||
lmcf->configure_handler = (ngx_http_lua_configure_handler_pt) cmd->post; | ||
|
||
if (cmd->post == ngx_http_lua_configure_handler_file) { | ||
name = ngx_http_lua_rebase_path(cf->pool, value[1].data, | ||
value[1].len); | ||
if (name == NULL) { | ||
return NGX_CONF_ERROR; | ||
} | ||
|
||
lmcf->configure_src.data = name; | ||
lmcf->configure_src.len = ngx_strlen(name); | ||
|
||
} else { | ||
lmcf->configure_src = value[1]; | ||
} | ||
|
||
return NGX_CONF_OK; | ||
} | ||
|
||
|
||
ngx_int_t | ||
ngx_http_lua_configure_handler_inline(ngx_conf_t *cf, | ||
ngx_http_lua_main_conf_t *lmcf) | ||
{ | ||
int status; | ||
lua_State *L = lmcf->lua; | ||
|
||
cfp = cf; | ||
|
||
status = luaL_loadbuffer(L, (char *) lmcf->configure_src.data, | ||
lmcf->configure_src.len, "=configure_by_lua") | ||
|| ngx_http_lua_do_call(cf->log, L); | ||
|
||
cfp = NULL; | ||
|
||
return ngx_http_lua_report(cf->log, L, status, "configure_by_lua"); | ||
} | ||
|
||
|
||
ngx_int_t | ||
ngx_http_lua_configure_handler_file(ngx_conf_t *cf, | ||
ngx_http_lua_main_conf_t *lmcf) | ||
{ | ||
int status; | ||
lua_State *L = lmcf->lua; | ||
|
||
cfp = cf; | ||
|
||
status = luaL_loadfile(L, (char *) lmcf->configure_src.data) | ||
|| ngx_http_lua_do_call(cf->log, L); | ||
|
||
cfp = NULL; | ||
|
||
return ngx_http_lua_report(cf->log, L, status, "configure_by_lua_file"); | ||
} | ||
|
||
|
||
ngx_uint_t | ||
ngx_http_lua_is_configure_phase() | ||
{ | ||
return cfp != NULL; | ||
} | ||
|
||
|
||
#ifndef NGX_LUA_NO_FFI_API | ||
unsigned int | ||
ngx_http_lua_ffi_is_configure_phase() | ||
{ | ||
return ngx_http_lua_is_configure_phase(); | ||
} | ||
|
||
|
||
int | ||
ngx_http_lua_ffi_configure_shared_dict(ngx_str_t *name, ngx_str_t *size, | ||
u_char *errstr, size_t *err_len) | ||
{ | ||
ssize_t ssize; | ||
ngx_shm_zone_t **zp; | ||
ngx_shm_zone_t *zone; | ||
ngx_http_lua_shdict_ctx_t *ctx; | ||
ngx_http_lua_main_conf_t *lmcf; | ||
lua_State *L; | ||
ngx_conf_t *cf = cfp; | ||
|
||
lmcf = ngx_http_conf_get_module_main_conf(cf, ngx_http_lua_module); | ||
|
||
ssize = ngx_parse_size(size); | ||
if (ssize <= NGX_HTTP_LUA_SHDICT_MINSIZE) { | ||
*err_len = ngx_snprintf(errstr, *err_len, | ||
"invalid lua shared dict size \"%s\"", | ||
size->data) | ||
- errstr; | ||
return NGX_ERROR; | ||
} | ||
|
||
ctx = ngx_pcalloc(cf->cycle->pool, sizeof(ngx_http_lua_shdict_ctx_t)); | ||
if (ctx == NULL) { | ||
*err_len = ngx_snprintf(errstr, *err_len, "no memory") | ||
- errstr; | ||
return NGX_ERROR; | ||
} | ||
|
||
ctx->name = *name; | ||
ctx->main_conf = lmcf; | ||
ctx->log = &cf->cycle->new_log; | ||
|
||
zone = ngx_http_lua_shared_memory_add(cf, name, (size_t) ssize, | ||
&ngx_http_lua_module); | ||
if (zone == NULL) { | ||
*err_len = ngx_snprintf(errstr, *err_len, "no memory") | ||
- errstr; | ||
return NGX_ERROR; | ||
} | ||
|
||
if (zone->data) { | ||
return NGX_DECLINED; | ||
} | ||
|
||
zone->init = ngx_http_lua_shdict_init_zone; | ||
zone->data = ctx; | ||
|
||
if (lmcf->shdict_zones == NULL) { | ||
lmcf->shdict_zones = ngx_palloc(cf->pool, sizeof(ngx_array_t)); | ||
if (lmcf->shdict_zones == NULL) { | ||
*err_len = ngx_snprintf(errstr, *err_len, "no memory") | ||
- errstr; | ||
return NGX_ERROR; | ||
} | ||
|
||
if (ngx_array_init(lmcf->shdict_zones, cf->pool, 2, | ||
sizeof(ngx_shm_zone_t *)) | ||
!= NGX_OK) | ||
{ | ||
*err_len = ngx_snprintf(errstr, *err_len, "no memory") | ||
- errstr; | ||
return NGX_ERROR; | ||
} | ||
} | ||
|
||
zp = ngx_array_push(lmcf->shdict_zones); | ||
if (zp == NULL) { | ||
*err_len = ngx_snprintf(errstr, *err_len, "no memory") | ||
- errstr; | ||
return NGX_ERROR; | ||
} | ||
|
||
*zp = zone; | ||
|
||
L = lmcf->lua; | ||
|
||
lua_getglobal(L, "ngx"); | ||
lua_getfield(L, -1, "shared"); | ||
if (!lua_getmetatable(L, -1)) { | ||
ngx_http_lua_create_shdict_mt(L); /* ngx.shared shmt */ | ||
} | ||
|
||
ngx_http_lua_attach_shdict(L, name, zone); | ||
lua_pop(L, 2); /* pop: ngx.shared + shmt */ | ||
|
||
return NGX_OK; | ||
} | ||
|
||
|
||
void | ||
ngx_http_lua_ffi_configure_max_pending_timers(int n_timers) | ||
{ | ||
|
||
ngx_http_lua_main_conf_t *lmcf; | ||
ngx_conf_t *cf = cfp; | ||
|
||
lmcf = ngx_http_conf_get_module_main_conf(cf, ngx_http_lua_module); | ||
|
||
lmcf->max_pending_timers = (ngx_int_t) n_timers; | ||
} | ||
|
||
|
||
void | ||
ngx_http_lua_ffi_configure_max_running_timers(int n_timers) | ||
{ | ||
|
||
ngx_http_lua_main_conf_t *lmcf; | ||
ngx_conf_t *cf = cfp; | ||
|
||
lmcf = ngx_http_conf_get_module_main_conf(cf, ngx_http_lua_module); | ||
|
||
lmcf->max_running_timers = (ngx_int_t) n_timers; | ||
} | ||
|
||
|
||
int | ||
ngx_http_lua_ffi_configure_env(u_char *value, size_t name_len, size_t len) | ||
{ | ||
ngx_core_conf_t *ccf; | ||
ngx_str_t *var; | ||
ngx_conf_t *cf = cfp; | ||
|
||
ccf = (ngx_core_conf_t *) ngx_get_conf(cf->cycle->conf_ctx, | ||
ngx_core_module); | ||
|
||
var = ngx_array_push(&ccf->env); | ||
if (var == NULL) { | ||
return NGX_ERROR; | ||
} | ||
|
||
var->data = ngx_pnalloc(cf->pool, len); | ||
if (var->data == NULL) { | ||
return NGX_ERROR; | ||
} | ||
|
||
(void) ngx_copy(var->data, value, len); | ||
var->len = name_len; | ||
|
||
return NGX_OK; | ||
} | ||
#endif | ||
|
||
|
||
/* vi:set ft=c ts=4 sw=4 et fdm=marker: */ |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,35 @@ | ||
|
||
/* | ||
* Copyright (C) Yichun Zhang (agentzh) | ||
* | ||
* Author: Thibault Charbonnier (thibaultcha) | ||
* I hereby assign copyright in this code to the lua-nginx-module project, | ||
* to be licensed under the same terms as the rest of the code. | ||
*/ | ||
|
||
|
||
#ifndef _NGX_HTTP_LUA_CONFIGUREBY_H_INCLUDED_ | ||
#define _NGX_HTTP_LUA_CONFIGUREBY_H_INCLUDED_ | ||
|
||
|
||
#include "ngx_http_lua_common.h" | ||
|
||
|
||
char *ngx_http_lua_configure_by_lua_block(ngx_conf_t *cf, ngx_command_t *cmd, | ||
void *conf); | ||
|
||
char *ngx_http_lua_configure_by_lua(ngx_conf_t *cf, ngx_command_t *cmd, | ||
void *conf); | ||
|
||
ngx_int_t ngx_http_lua_configure_handler_inline(ngx_conf_t *cf, | ||
ngx_http_lua_main_conf_t *lmcf); | ||
|
||
ngx_int_t ngx_http_lua_configure_handler_file(ngx_conf_t *cf, | ||
ngx_http_lua_main_conf_t *lmcf); | ||
|
||
ngx_uint_t ngx_http_lua_is_configure_phase(); | ||
|
||
|
||
#endif /* _NGX_HTTP_LUA_CONFIGUREBY_H_INCLUDED_ */ | ||
|
||
/* vi:set ft=c ts=4 sw=4 et fdm=marker: */ |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I don't like all the code duplications in this C source file. It makes the code base harder to maintain and sync new changes.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I can definitely improve it :) Which area? The context runner and/or the shm configure API?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@thibaultcha Almost all the code in this C source file is duplicated C code.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@agentzh I just added a new commit which introduces
ngx_http_lua_shared_dict_add
, which factorizes all of the shared code for shm create forlua_shared_dict
andngx_configure.shared_dict()
.As stated before, the code related to parsing and running the
_by_*_block
and_by_*_file
directives of this module are already always duplicated - this feels outside the scope of this PR, but if you want me to address this, please advise.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@thibaultcha
What exactly are you referring to?
The thing is if the config phase handlers can call these functions, then we should make it that way. I don't want to maintain 2 duplicated copies of configurations. Also, how do you handle config directive overriding and inheritance here? Seems like you already bypass the nginx config system entirely?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
There is no inheritance involved here since we only support configuration of
http
(or above) level directives, and this is deliberate.Ultimately, I do wish to support lower levels of configuration as explained in the PR (
server
,location
...), but without it there are no need for supporting inheritance yet.I was reffering to the only part of the code that I see is duplicated from other parts of ngx_lua, and that is the
ngx_http_lua_configure_by_lua
and other handlers that run the Lua code for this phase.If not, then I am not sure what you are referring to when you are saying things are duplicated?