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

Calling connect() after keepalive() #44

Closed
gnois opened this issue Dec 13, 2016 · 16 comments
Closed

Calling connect() after keepalive() #44

gnois opened this issue Dec 13, 2016 · 16 comments

Comments

@gnois
Copy link

gnois commented Dec 13, 2016

This used to work in 1.6 but no longer in 1.7 or 1.8, with error
.../pgmoon/init.lua:194: attempt to index field 'sock' (a nil value)

File app/db.lua

local pg = require('pgmoon')
return pg.new({...})

File app/handler.lua

local db = require('app.db')
function ware1(db)
	db:connect()
	-- do work
	db:keepalive()
end

function ware2(db)
	db:connect()
	-- do work
	db:keepalive()
end

function handler(db)
	ware1(db)
	-- do some work
	ware2(db)
	-- do more work
end

I found that the self.sock handle is set to nil after keepalive().
Can we call connect() after keepalive() if they are within the same request?

Thanks!

@ttfkam
Copy link
Contributor

ttfkam commented Dec 20, 2016

I'm seeing this as well when running the build tests.

@ttfkam
Copy link
Contributor

ttfkam commented Dec 20, 2016

Here's the culprit
c841be9#diff-ecfebe05627fcb262bfc1b60e5ea0306

I'm assuming it was an oversight since I don't see any extra logic at work.

@thibaultcha
Copy link
Contributor

thibaultcha commented Dec 20, 2016

It is an OpenResty paradigm to create one TCP socket in such a new() method, and not in connect() as pgmoon was previously doing. See https://github.com/openresty/lua-resty-redis for such an example, among others.

Maybe pgmoon shouldn't free the socket in :setkeepalive()? There could be some side-effects that I am not aware of.

@ttfkam
Copy link
Contributor

ttfkam commented Dec 20, 2016

Side effects worse than a nil reference by default? 😏

@thibaultcha
Copy link
Contributor

If you feel like this behavior is more desirable than the current one, feel free to propose a PR to this project.

@ttfkam
Copy link
Contributor

ttfkam commented Dec 20, 2016

Looking at lua-resty-redis as a reference,
https://github.com/openresty/lua-resty-redis/blob/403d2982ec1dacaa758301c788754a82c82b00d8/lib/resty/redis.lua#L99

it doesn't appear that the socket should be set to nil. I'll provide a PR soon.

@gnois
Copy link
Author

gnois commented Dec 20, 2016

I found the last paragraph of https://github.com/openresty/lua-resty-redis#set_keepalive gives a pointer that connect is allowed:

Any subsequent operations other than connect() on the current object will return the closed error.

Thanks @ttfkam and @thibaultcha

@ttfkam
Copy link
Contributor

ttfkam commented Dec 20, 2016

On the bright side, it turns out the failing tests were due to my build environment. The rockspec file really should have more dependencies defined.

On the downside, there are no tests for keepalive I can see. This probably makes sense since the connection behavior is different for OpenResty environments than for straight Lua usage.

I use pg_bouncer for my connection pooling, so I don't have a setup handy for it. @gnois, if you checkout my keepalive branch, I'd love to know that it works for you. The only two files you'd need to swap out are pgmoon/init.moon and pgmoon/init.lua

@gnois
Copy link
Author

gnois commented Dec 20, 2016

@ttfkam Works well enough for me. 👍

@thibaultcha
Copy link
Contributor

The rockspec file really should have more dependencies defined.

The rockspec is for production installations only, since the format does not support development-only dependencies yet. Development dependencies are to be manually installed; library maintainers have different ways of dealing with them and/or simply document them in the README. Feel free to send a PR improving the process for this library if you feel a need for it.

@ttfkam
Copy link
Contributor

ttfkam commented Dec 20, 2016

Still getting used to Lua. Thank you for the explanation.

@ttfkam
Copy link
Contributor

ttfkam commented Dec 20, 2016

Associating the pull request with this issue. #46

@yzk0281
Copy link

yzk0281 commented Mar 14, 2017

@ttfkam
How did this problem solved in the end ?
I came with the same problem.

@ttfkam
Copy link
Contributor

ttfkam commented Mar 14, 2017

I put up a pull request months ago with what I saw as the best solution at the time, but it's open source; you can't force anyone to actually accept the patch if they don't want to. Until @leafo accepts the patch or fixes on his own as he sees fit, you'll have to simply stop using pgmoon's implementation of keepalive().

As a workaround, you can use pgbouncer to manage connection pooling rather than relying on the database driver to do so.

@yzk0281
Copy link

yzk0281 commented Mar 15, 2017

@ttfkam
Hi Sir, is Your PR change the keepalive function to not set sock = nil?

@ttfkam
Copy link
Contributor

ttfkam commented Mar 15, 2017

Yes, #46

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

4 participants