[mod_magnet] defer req_env init unless needed

defer req_env initialization unless req_env might be needed by script
master
Glenn Strauss 10 months ago
parent a162fc70e9
commit d0273c8af5
  1. 16
      src/mod_magnet.c
  2. 11
      src/mod_magnet_cache.c
  3. 1
      src/mod_magnet_cache.h

@ -2289,14 +2289,12 @@ static int push_traceback(lua_State *L, int narg) {
}
static handler_t magnet_attract(request_st * const r, plugin_data * const p, script * const sc) {
/*(always check at least mtime and size to trigger script reload)*/
int etag_flags = r->conf.etag_flags | ETAG_USE_MTIME | ETAG_USE_SIZE;
lua_State * const L = script_cache_check_script(sc, etag_flags);
lua_State * const L = sc->L;
int lua_return_value;
const int func_ndx = 1;
const int lighty_table_ndx = 2;
if (NULL == L) {
if (lua_gettop(L) == 0) {
log_perror(r->conf.errh, __FILE__, __LINE__,
"loading script %s failed", sc->name.ptr);
@ -2469,11 +2467,19 @@ static handler_t magnet_attract_array(request_st * const r, plugin_data * const
}
if (NULL == scripts) return HANDLER_GO_ON; /* no scripts set */
r->con->srv->request_env(r);
/*(always check at least mtime and size to trigger script reload)*/
const int etag_flags = r->conf.etag_flags | ETAG_USE_MTIME | ETAG_USE_SIZE;
int req_env_inited = 0;
/* execute scripts sequentially while HANDLER_GO_ON */
handler_t rc = HANDLER_GO_ON;
do {
script_cache_check_script(*scripts, etag_flags);
if ((*scripts)->req_env_init && !req_env_inited) {
/*(request env init is deferred until needed)*/
req_env_inited = 1;
r->con->srv->request_env(r);
}
rc = magnet_attract(r, p, *scripts);
} while (rc == HANDLER_GO_ON && *++scripts);

@ -5,7 +5,8 @@
#include <errno.h>
#include <stdlib.h>
#include <unistd.h> /* read() */
#include <string.h> /* strstr() */
#include <unistd.h> /* lseek() read() */
#include <lualib.h>
#include <lauxlib.h>
@ -54,7 +55,7 @@ static lua_State *script_cache_load_script(script * const sc, int etag_flags)
stat_cache_entry * const sce = stat_cache_get_entry_open(&sc->name, 1);
buffer_clear(&sc->etag);
if (NULL == sce || sce->fd < 0) {
if (NULL == sce || sce->fd < 0 || -1 == lseek(sce->fd, 0, SEEK_SET)) {
/*(sce->fd < 0 might indicate empty file, which is not a valid script)*/
if (NULL != sce) errno = EBADF;
return NULL;
@ -64,7 +65,7 @@ static lua_State *script_cache_load_script(script * const sc, int etag_flags)
buffer_copy_buffer(&sc->etag, etag);
const off_t sz = sce->st.st_size;
char * const buf = malloc(sz);
char * const buf = malloc(sz+1);
force_assert(buf);
ssize_t rd = 0;
@ -78,6 +79,10 @@ static lua_State *script_cache_load_script(script * const sc, int etag_flags)
return NULL;
}
/*(coarse heuristic to detect if script needs req_env initialized)*/
buf[sz] = '\0'; /* for strstr() */
sc->req_env_init = (NULL != strstr(buf, "req_env"));
int rc = luaL_loadbuffer(sc->L, buf, (size_t)sz, sc->name.ptr);
free(buf);

@ -12,6 +12,7 @@ typedef struct {
buffer etag;
lua_State *L;
int req_env_init;
} script;
typedef struct {

Loading…
Cancel
Save