Skip to content

Commit

Permalink
implement haproxy socket type from configuration
Browse files Browse the repository at this point in the history
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
  • Loading branch information
mecampbellsoup committed Jun 8, 2022
1 parent 4bc922e commit fcbfdc8
Show file tree
Hide file tree
Showing 4 changed files with 11 additions and 3 deletions.
4 changes: 2 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -127,7 +127,7 @@ Functions in table returned by `require("pgmoon")`:

Creates a new `Postgres` object from a configuration object. All fields are
optional unless otherwise stated. The newly created object will not
automatically connect, you must call `conect` after creating the object.
automatically connect, you must call `connect` after creating the object.

Available options:

Expand All @@ -139,7 +139,7 @@ Available options:
* `"ssl"`: enable ssl (default: `false`)
* `"ssl_verify"`: verify server certificate (default: `nil`)
* `"ssl_required"`: abort the connection if the server does not support SSL connections (default: `nil`)
* `"socket_type"`: the type of socket to use, one of: `"nginx"`, `"luasocket"`, `cqueues` (default: `"nginx"` if in nginx, `"luasocket"` otherwise)
* `"socket_type"`: the type of socket to use, one of: `"nginx"`, `"haproxy"`, `"luasocket"`, `"cqueues"` (default: `"nginx"` if in nginx, `"haproxy"` if in haproxy, `"luasocket"` otherwise)
* `"application_name"`: set the name of the connection as displayed in `pg_stat_activity`. (default: `"pgmoon"`)
* `"pool"`: (OpenResty only) name of pool to use when using OpenResty cosocket (default: `"#{host}:#{port}:#{database}"`)
* `"pool_size"`: (OpenResty only) Passed directly to OpenResty cosocket connect function, [see docs](https://github.com/openresty/lua-nginx-module#tcpsockconnect)
Expand Down
2 changes: 1 addition & 1 deletion lint_config.lua
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@

return {
whitelist_globals = {
["."] = {"ngx"}
["."] = {"ngx", "core"}
}
}
4 changes: 4 additions & 0 deletions pgmoon/socket.lua
Original file line number Diff line number Diff line change
Expand Up @@ -89,6 +89,8 @@ return {
if socket_type == nil then
if ngx and ngx.get_phase() ~= "init" then
socket_type = "nginx"
elseif core and core.get_info() then
socket_type = "haproxy"
else
socket_type = "luasocket"
end
Expand All @@ -99,6 +101,8 @@ return {
socket = ngx.socket.tcp()
elseif "luasocket" == _exp_0 then
socket = create_luasocket()
elseif "haproxy" == _exp_0 then
socket = core.tcp(...)
elseif "cqueues" == _exp_0 then
socket = require("pgmoon.cqueues").CqueuesSocket()
else
Expand Down
4 changes: 4 additions & 0 deletions pgmoon/socket.moon
Original file line number Diff line number Diff line change
Expand Up @@ -85,6 +85,8 @@ create_luasocket = do
-- luasocket
socket_type = if ngx and ngx.get_phase! != "init"
"nginx"
elseif core and core.get_info!
"haproxy"
else
"luasocket"

Expand All @@ -93,6 +95,8 @@ create_luasocket = do
ngx.socket.tcp!
when "luasocket"
create_luasocket!
when "haproxy"
core.tcp ...
when "cqueues"
require("pgmoon.cqueues").CqueuesSocket!
else
Expand Down

0 comments on commit fcbfdc8

Please sign in to comment.