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

bad request on repeated requests #128

Open
r614 opened this issue Jul 6, 2022 · 3 comments
Open

bad request on repeated requests #128

r614 opened this issue Jul 6, 2022 · 3 comments

Comments

@r614
Copy link

r614 commented Jul 6, 2022

hi! i am running into the following error when my openresty/nginx server gets repeated requests to an endpoint.
after a certain point, all requests start failing with:

2022/07/05 22:32:21 [error] 7#7: *24332 lua entry thread aborted: runtime error: /usr/local/openresty/luajit/share/lua/5.1/pgmoon/init.lua:251: bad request
stack traceback:
coroutine 0:
	[C]: in function 'connect'
	/usr/local/openresty/luajit/share/lua/5.1/pgmoon/init.lua:251: in function 'connect'
	/usr/local/openresty/nginx//rewrite.lua:66: in function 'connect'
	/usr/local/openresty/nginx//rewrite.lua:92: in function 'pod_data_by_id'
	/usr/local/openresty/nginx//rewrite.lua:132: in function 'go'
	rewrite_by_lua(nginx.conf:45):2: in main chunk, client: <ip>, server: , request: "GET /?auth-token=<some_token>HTTP/1.1", host: <some host>

it's failing when calling the connect function on a request:

local _pg_cfg = pgmoon.new({
  host = os.getenv("pg_host"),
  port = os.getenv("pg_port"),
  user = os.getenv("pg_user"),
  database = os.getenv("pg_db"),
  password = os.getenv("pg_pw")
})


local connect = function()
  local success, err = _pg_cfg:connect()
  if err ~= nil then
    ngx.log(ngx.ERR, err)
    ngx.exit(ngx.HTTP_INTERNAL_SERVER_ERROR)
  end
  if not success then
    ngx.log(ngx.ERR, "Could not acquire a Postgres connection")
    ngx.exit(ngx.HTTP_INTERNAL_SERVER_ERROR)
  end
  return _pg_cfg
end

@r614 r614 changed the title 'error: bad request' on repeated requests bad request on repeated requests Jul 6, 2022
@leafo
Copy link
Owner

leafo commented Sep 30, 2022

I'm not entirely sure since there are many factors about your setup that could influence what's going on, but based on the error message it sounds like you're trying to issue commands to the server on a connection socket that is in a different state and expecting something else. Eg. send sending connect protocol messages through a socket that has already established a connection to the database

@r614
Copy link
Author

r614 commented Sep 30, 2022

thanks for the response! i managed to fix it by moving the _pg_cfg block inside the connect function, something like this:

local connect = function()
 local _pg_cfg = pgmoon.new({
   host = os.getenv("pg_host"),
   port = os.getenv("pg_port"),
   user = os.getenv("pg_user"),
   database = os.getenv("pg_db"),
   password = os.getenv("pg_pw")
 })
 local success, err = _pg_cfg:connect()
 if err ~= nil then
   ngx.log(ngx.ERR, err)
   ngx.exit(ngx.HTTP_INTERNAL_SERVER_ERROR)
 end
 if not success then
   ngx.log(ngx.ERR, "Could not acquire a Postgres connection")
   ngx.exit(ngx.HTTP_INTERNAL_SERVER_ERROR)
 end
 return _pg_cfg
end

to be honest, i am not sure why this fixed the issue. my guess was similar that there's a state mismatch, but beyond that, i have no clue since this was also my first time messing around with nginx + lua lol.

@leafo
Copy link
Owner

leafo commented Oct 9, 2022

The object returned by pgmoon.new is a stateful object that holds the connection socket. Based on what you said it sounds like you were attempting to re-use it for a new connection by calling pg:connect without closing the existing connection that would have been opened on a previous call to your connect function.

Your solution of creating a new instance on every new connection is perfectly fine and my recommended approach. The previous connection will automatically be closed via garbage collection if you don't explicitly close it. I recommend this approach since if you are/ever use threading via coroutines you want each thread to have its own connection since query functions are not atomic (eg. commonly lua async network libraries yield on any network activity, and the connection may be in the middle of sending a receiving a query when a new thread is yielded to)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants