leafo / pgmoon

A pure Lua Postgres driver for use in OpenResty & more
MIT License
397 stars 93 forks source link

implement haproxy socket type from configuration #125

Open mecampbellsoup opened 2 years ago

mecampbellsoup commented 2 years ago

Resolves https://github.com/leafo/pgmoon/issues/114.

When the Lua HAProxy core object is detected, the socket class will automatically use its (non-blocking) socket type. The pattern is similar to existing auto-detection of whether pgmoon is running inside an NGINX context or not and choosing that socket type if so.

http://www.arpalert.org/src/haproxy-lua-api/1.7/index.html#core.get_info

cesarmesones commented 2 years ago

we are also using haproxy in lua and would love to be able to use this api!

pyreta commented 2 years ago

we are also using haproxy in lua and would love to be able to use this api!

plus one!

JsmithCoreWeave commented 2 years ago

we are also using haproxy in lua and would love to be able to use this api!

Would be helpful for us as well.

leafo commented 2 years ago

Just adding comment in case it's helpful to anyone: if it's API compatible with LuaSocket then you can run package.loaded.socket = core before creating your connection object.

Be warned, I have no idea how this will play with ssl. (This change in this patch too is unknown)

mecampbellsoup commented 1 year ago

Just adding comment in case it's helpful to anyone: if it's API compatible with LuaSocket then you can run package.loaded.socket = core before creating your connection object.

I tried using pgmoon:master without our HAProxy-specific changes and putting package.loaded.socket = core in our connect DB function as follows:

function connect_db(pg_settings)
    -- Returns a pgmoon postgres object or nil on failure.

    package.loaded.socket = core

    local pg = pgmoon.new(pg_settings)
    -- Set the timeout value (in milliseconds) for all socket operations
    -- (connect, write, receive)
    pg:settimeout(5000)
    pg.convert_null = true -- https://github.com/leafo/pgmoon#converting-nulls
    local success, err = pg:connect()

However this does not seem to work.

mecampbellsoup commented 1 year ago

if it's API compatible with LuaSocket then you can run package.loaded.socket = core before creating your connection object.

Surprising it didn't work since the HAProxy Lua API docs specify the same constraint:

This class must be compatible with the Lua Socket class. Only the ‘client’ functions are available.

https://www.arpalert.org/src/haproxy-lua-api/2.6/index.html#Socket