Skip to content
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: added the ngx.configure module and APIs #176

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion .travis.yml
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,7 @@ install:
- git clone https://github.com/openresty/openresty.git ../openresty
- git clone https://github.com/openresty/openresty-devel-utils.git
- git clone https://github.com/simpl/ngx_devel_kit.git ../ndk-nginx-module
- git clone https://github.com/openresty/lua-nginx-module.git ../lua-nginx-module
- git clone -b feat/configure-by-lua https://github.com/thibaultcha/lua-nginx-module.git ../lua-nginx-module
- git clone https://github.com/openresty/no-pool-nginx.git ../no-pool-nginx
- git clone https://github.com/openresty/echo-nginx-module.git ../echo-nginx-module
- git clone https://github.com/openresty/lua-resty-lrucache.git
Expand Down
159 changes: 159 additions & 0 deletions lib/ngx/configure.lua
Original file line number Diff line number Diff line change
@@ -0,0 +1,159 @@
-- Copyright (C) Yichun Zhang (agentzh)
--
-- Author: Thibault Charbonnier (thibaultcha)


local base = require "resty.core.base"
base.allows_subsystem("http")


local ffi = require "ffi"
local C = ffi.C
--local FFI_OK = base.FFI_OK
local FFI_ERROR = base.FFI_ERROR
local FFI_DECLINED = base.FFI_DECLINED
local ffi_str = ffi.string
local get_string_buf = base.get_string_buf
local get_size_ptr = base.get_size_ptr
local find = string.find
local type = type


local ERR_BUF_SIZE = 128


ffi.cdef [[
unsigned int ngx_http_lua_ffi_is_configure_phase();

int ngx_http_lua_ffi_configure_shared_dict(ngx_str_t *name,
ngx_str_t *size, unsigned char *errstr, size_t *errlen);

void ngx_http_lua_ffi_configure_max_pending_timers(int n_timers);

void ngx_http_lua_ffi_configure_max_running_timers(int n_timers);

int ngx_http_lua_ffi_configure_env(const unsigned char *value,
size_t name_len, size_t len);
]]


local _M = { version = base.version }


function _M.is_configure_phase()
return C.ngx_http_lua_ffi_is_configure_phase() == 1
end


do
local name_str_t = ffi.new("ngx_str_t[1]")
local size_str_t = ffi.new("ngx_str_t[1]")


function _M.shared_dict(name, size)
if not _M.is_configure_phase() then
error("API disabled in the current context", 2)
end

if type(name) ~= "string" then
error("name must be a string", 2)
end

if type(size) ~= "string" then
error("size must be a string", 2)
end

local name_len = #name
if name_len == 0 then
error("invalid lua shared dict name", 2)
end

local size_len = #size
if size_len == 0 then
error("invalid lua shared dict size", 2)
end

local name_t = name_str_t[0]
local size_t = size_str_t[0]

name_t.data = name
name_t.len = name_len

size_t.data = size
size_t.len = size_len

local err = get_string_buf(ERR_BUF_SIZE)
local errlen = get_size_ptr()
errlen[0] = ERR_BUF_SIZE

local rc = C.ngx_http_lua_ffi_configure_shared_dict(name_str_t,
size_str_t, err,
errlen)
if rc == FFI_DECLINED then
error(ffi_str(err, errlen[0]), 2)
end

if rc == FFI_ERROR then
error("no memory")
end

-- NGINX_OK/FFI_OK
end
end


function _M.max_pending_timers(n_timers)
if not _M.is_configure_phase() then
error("API disabled in the current context", 2)
end

if type(n_timers) ~= "number" then
error("n_timers must be a number", 2)
end

if n_timers < 0 then
error("n_timers must be positive", 2)
end

C.ngx_http_lua_ffi_configure_max_pending_timers(n_timers)
end


function _M.max_running_timers(n_timers)
if not _M.is_configure_phase() then
error("API disabled in the current context", 2)
end

if type(n_timers) ~= "number" then
error("n_timers must be a number", 2)
end

if n_timers < 0 then
error("n_timers must be positive", 2)
end

C.ngx_http_lua_ffi_configure_max_running_timers(n_timers)
end


function _M.env(value)
if not _M.is_configure_phase() then
error("API disabled in the current context", 2)
end

if type(value) ~= "string" then
error("value must be a string", 2)
end

local len = #value
local idx = find(value, "=")

local rc = C.ngx_http_lua_ffi_configure_env(value, idx and idx - 1 or len,
len)
if rc == FFI_ERROR then
error("no memory")
end
end


return _M
8 changes: 7 additions & 1 deletion lib/resty/core/phase.lua
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
local ffi = require 'ffi'
local base = require "resty.core.base"
local ngx_configure = require "ngx.configure"

local C = ffi.C
local FFI_ERROR = base.FFI_ERROR
Expand Down Expand Up @@ -33,8 +34,13 @@ local context_names = {
function ngx.get_phase()
local r = getfenv(0).__ngx_req

-- if we have no request object, assume we are called from the "init" phase
-- if we have no request object, assume we are called from the "init"
-- or "configure" phase
if not r then
if ngx_configure.is_configure_phase() then
return "configure"
end

return "init"
end

Expand Down
5 changes: 5 additions & 0 deletions lib/resty/core/shdict.lua
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@

local ffi = require 'ffi'
local base = require "resty.core.base"
local ngx_configure = require "ngx.configure"

local ffi_new = ffi.new
local ffi_str = ffi.string
Expand Down Expand Up @@ -71,6 +72,10 @@ local errmsg = base.get_errmsg_ptr()


local function check_zone(zone)
if ngx_configure.is_configure_phase() then
error("API disabled in the context of configure_by_lua", 3)
end

if not zone or type(zone) ~= "table" then
return error("bad \"zone\" argument")
end
Expand Down
Loading