I have ab test requirement
If when I have 10 different versions of the web to buy the page style
I need to set a different weight ratio for each page.
I want to use the lua-resty-balancer module to configure the access ratio for different pages.
Each page has a different id, and each id has a different weight. When nginx receives a user request, it automatically assigns a page id proportionally.
The following code is executed by me in the init_by_lua phase
local _M = {}
_M.VERSION = '0.10'
local mt = { __index = _M }
local resty_chash = require "resty.chash"
local resty_roundrobin = require "resty.roundrobin"
---page ID = weigth %
local page_list = {
["1985"] = 5,
["1986"] = 3,
["1987"] = 2,
}
@leafo
-- XX: we can do the following steps to keep consistency with nginx chash
local str_null = string.char(0)
local pages, nodes = {}, {}
for cid, weight in pairs(page_list) do
-- XX: we can just use serv as id when we doesn't need keep consistency with nginx chash
local id = string.gsub(cid, ":", str_null)
pages[id] = cid
nodes[id] = weight
end
local rr_up = resty_roundrobin:new(page_list)
package.loaded.my_rr_up = rr_up
local chash_up = resty_chash:new(nodes)
package.loaded.my_chash_up = chash_up
package.loaded.my_cids = pages
II want to get the my_rr_up module in the controller layer
local chash_up = package.loaded.my_chash_up
When running the request, the following error is reported.
2019/08/20 10:11:09 [error] 78194#78194: *2 lua entry thread aborted: runtime error: /usr/local/share/lua/5.1/lapis/init.lua:17: attempt to index local 'app_cls' (a userdata value)
stack traceback:
coroutine 0:
/usr/local/share/lua/5.1/lapis/init.lua: in function 'serve'
/usr/local/share/lua/5.1/lapis/init.lua
local application = require("lapis.application")
local Application
Application = application.Application
local app_cache = { }
setmetatable(app_cache, {
__mode = "k"
})
local dispatcher
local serve
serve = function(app_cls)
local app = app_cache[app_cls]
if not (app) then
local name = app_cls
if type(name) == "string" then
app_cls = require(name)
end
if app_cls.__base then
app = app_cls()
else
app_cls:build_router()
app = app_cls
end
app_cache[name] = app
end
if not (dispatcher) then
dispatcher = require("lapis.nginx")
end
return dispatcher.dispatch(app)
end
return {
serve = serve,
application = application,
Application = Application,
app_cache = app_cache
}