-
Notifications
You must be signed in to change notification settings - Fork 449
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
Bad request in function 'send' #44
Comments
@freman ngx_lua's cosocket objects can only be used by the request creating them while you're sharing the cosocket object in your module table across all the requests, thus leading to the "bad request" exception. See https://github.com/openresty/lua-resty-redis#limitations The correct way is to create separate cosocket objects (hence separate resty.redis instances) for each request handler. |
I thought that's what I was doing by initing the module for every request. |
@freman Lua modules are cached and shared globally unless you turn off |
@freman The correct way is to make your module initiate new objects for individual requests in an OO-style way, just like |
Here I was hoping to reduce the code duplication, now I'm on my way down the rabbit hole :D |
I think I'm still messing something up @agentzh - still newish at lua, this would be my first object orientated script (and previously my first module) - I pretty much resorted to copying what you had done. local _M = {}
local mt = { __index = _M }
local REDIS_TIMEOUT = 250
local REDIS_HOST = '127.0.0.1'
local REDIS_PORT = 6379
local REDIS_POOLSIZE = 200
local REDIS_IDLETIME = 60
local MAP_EXPIRES = 3660
function _M.new(self)
local redis = require "resty.redis"
red = redis:new()
err = false
red:set_timeout(REDIS_TIMEOUT)
local ok, err = red:connect(REDIS_HOST, REDIS_PORT)
if not ok then
ngx.log(ngx.ERR, "Absolute failure of redis: " .. err)
return nil
end
return setmetatable({ red = red, err = err }, mt)
end
function _M.fetch(self)
local res, err = self.red:get(ngx.var.uid_got)
if not res then
ngx.log(ngx.ERR, "Failed to get value for key (" .. ngx.var.uid_got .. "): " .. err)
self.err = true
return
end
if type(res) == "string" then
ngx.var.httphost = res
local res, err = self.red:expire(ngx.var.uid_got, MAP_EXPIRES)
if not res then
ngx.log(ngx.ERR, "Failed to reset expiry for (" .. ngx.var.uid_got ..") to " .. MAP_EXPIRES .. ": " .. err)
self.err = true
return
end
end
return true
end
function _M.store(self)
local res, err = self.red:setex(ngx.var.uid_got, MAP_EXPIRES, ngx.var.httphost)
if not res then
ngx.log(ngx.ERR, "Failed to store value (" .. ngx.var.httphost .. ") for key (" .. ngx.var.uid_got .. "): " .. err)
self.err = true
return
end
-- if ngx.var.args then
-- ngx.log(ngx.ERR, "Not redirecting...")
-- return true
-- end
return "/" .. ngx.var.redirecturl
end
function _M.clear(self)
local res, err = self.red:del(ngx.var.uid_got)
if not res then
ngx.log(ngx.ERR, "Failed to clear value for key (" .. ngx.var.uid_got .. "): " .. err)
self.err = true
return
end
end
function _M.pool(self)
if self.err then
ngx.log(ngx.ERR, "Not sending closed connection back to pool")
else
self.red:set_keepalive(REDIS_IDLETIME, REDIS_POOLSIZE)
end
end
return _M It's still spitting a few bad request errors out of redis.lua:292
And select_host.lua if ngx.var.uid_got then
local hostmap = require "hostmap"
if hostmap then
local map = hostmap:new()
if map:fetch() then
map:pool()
end
end
end |
@freman I've noticed one obvious mistake that you do not declare the local variables It is recommended to always use the |
@freman BTW, it's also better to move the line |
Thank you very much for your help and advice @agentzh, I believe it's working now without fault |
@freman Good to know :) I'm closing this. |
Hi @agentzh I seem to have this same bad request error when trying to get or set, and I can't figure out what to improve. I was also trying to use pcall to be able to serve an alternative if there is a bad request. I don't see where I would not have local. I thought it is. What am I not getting?
|
@morksinaanab As I've said earlier, this is a clear sign of misusing the cosocket API. Please read my previous comments in this ticket for more details. BTW, please do not paste lengthy examples since I will not have the time to read it. Always try to provide a minimal and standalone example that is easy to digest for others if you want to provide any. Thanks for your cooperation! |
i have jump into this bad hole,and thanks veriy much. |
Greetings, I'm absolutely positive this is my fault but I can't figure out what I'm doing wrong.
Seems to be randomly spitting back a "bad request"
I went uber paranoid with error handling and made it not call keepalive unless there were no errors...
hostmap.lua
select_host.lua
Exception - the "Why so bad request?" lines are me debugging to make sure that values are actually being passed.
The text was updated successfully, but these errors were encountered: