-
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
ngx.ctx inheirt #1057
Comments
@tokers The problem is that the nginx core clears all modules' |
@agentzh Thanks for your reply! Is there any other compromise way which is better than the |
@tokers I think maybe we can abuse a special custom nginx variable created by this module under the hood (on the C land) and use it to hold the pointer to |
@agentzh I will try! |
@agentzh I hacked the limits of ngx_ctx_bypassing.lua local debug = require "debug"
local ffi = require "ffi"
local base = require "resty.core.base"
local C = ffi.C
local registry = debug.getregistry()
local tostring = tostring
local tonumber = tonumber
local _M = {}
ffi.cdef[[
int ngx_http_lua_ffi_set_ctx_ref(ngx_http_request_t *r, int ref);
]]
function _M.stash_ngx_ctx()
local ctxs = registry.ngx_lua_ctx_tables
local ctx_ref = base.ref_in_table(ctxs, ngx.ctx)
local r = getfenv(0).__ngx_req
if not r then
return error("no request found")
end
if C.ngx_http_lua_ffi_set_ctx_ref(r, ctx_ref) ~= base.FFI_OK then
ngx.log(ngx.DEBUG, "stash the old ngx.ctx failed")
end
ngx.var.ctx_ref = tostring(ctx_ref)
end
function _M.apply_ngx_ctx()
local ctx_ref = tonumber(ngx.var.ctx_ref)
if not ctx_ref then
return
end
local ctxs = registry.ngx_lua_ctx_tables
local origin_ngx_ctx = ctxs[ctx_ref]
ngx.ctx = origin_ngx_ctx
local FREE_LIST_REF = 0
ctxs[ctx_ref] = ctxs[FREE_LIST_REF]
ctxs[FREE_LIST_REF] = ctx_ref
ngx.var.ctx_ref = ""
end
return _M location conf location /t1 {
content_by_lua_block {
local bypass_ngx_ctx = require "ngx_ctx_bypassing"
ngx.ctx = {
Date = "Wed May 3 15:18:04 CST 2017",
Site = "unknown"
}
bypass_ngx_ctx.stash_ngx_ctx()
ngx.exec("/t2")
}
}
location /t2 {
content_by_lua_block {
local bypass_ngx_ctx = require "ngx_ctx_bypassing"
ngx.ctx = {
Date = "Wed May 3 15:18:04 CST 2017",
Site = "unknown"
}
bypass_ngx_ctx.apply_ngx_ctx()
ngx.say("Date: " .. ngx.ctx["Date"] .. " Site: " .. ngx.ctx["Site"])
}
} My |
Would be highly interested in this option as well. |
@tokers Yeah, your workaround looks good to me. I didn't realize it can be done all in Lua. This is clever. |
@tokers I think the definition of |
@bjoe2k4 Yes, i miss the definition of |
@tokers Yeah, we should not override the existing ctx table ref. We just need to anchor the |
Maybe we could introduce |
@spacewander Frankly I don't really want to expose such implementation details to the user API level. It makes future implementation changes much harder if not impossible. |
need to share context between internal redirect (openresty/lua-nginx-module#1057) but then the policies have total control over generating the content so they can use cosockets / files / upstream / generate content please note this is higly experimental and should not be merged to master yet
need to share context between internal redirect (openresty/lua-nginx-module#1057) but then the policies have total control over generating the content so they can use cosockets / files / upstream / generate content please note this is higly experimental and should not be merged to master yet
need to share context between internal redirect (openresty/lua-nginx-module#1057) but then the policies have total control over generating the content so they can use cosockets / files / upstream / generate content please note this is higly experimental and should not be merged to master yet
this is needed to share ngx.ctx to subrequests or internal redirects quite similar to https://github.com/tokers/lua-resty-ctxdump implementation based on suggestions from openresty/lua-nginx-module#1057
this is needed to share ngx.ctx to subrequests or internal redirects quite similar to https://github.com/tokers/lua-resty-ctxdump implementation based on suggestions from openresty/lua-nginx-module#1057
this is needed to share ngx.ctx to subrequests or internal redirects quite similar to https://github.com/tokers/lua-resty-ctxdump implementation based on suggestions from openresty/lua-nginx-module#1057
this is needed to share ngx.ctx to subrequests or internal redirects quite similar to https://github.com/tokers/lua-resty-ctxdump implementation based on suggestions from openresty/lua-nginx-module#1057
We use the magic
ngx.ctx
for storing something, but when internel redirect is happening,ngx.ctx
will be dereferenced and the gc will recycle it, so most of the time we have to usengx.var.VARIABLE
, but the cost is expensive especially when the QPS is high. My problem is, why not add a flag to control whether inherits the originngx.ctx
whenngx.exec
is invoked. 😃The text was updated successfully, but these errors were encountered: