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 Jul 6, 2023
1 parent 7b7ef2a commit bb54268
Show file tree
Hide file tree
Showing 4 changed files with 26 additions and 9 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"}
}
}
15 changes: 12 additions & 3 deletions pgmoon/socket.lua
Original file line number Diff line number Diff line change
Expand Up @@ -72,10 +72,15 @@ do
return true
end
}
create_luasocket = function(...)
local socket = require("socket")
create_luasocket = function(socket_type, ...)
local socket
if socket_type == "haproxy" then
socket = core.tcp(...)
else
socket = require("socket").tcp(...)
end
local proxy = {
sock = socket.tcp(...)
sock = socket
}
for k, v in pairs(method_overrides) do
proxy[k] = v
Expand All @@ -89,6 +94,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 +106,8 @@ return {
socket = ngx.socket.tcp()
elseif "luasocket" == _exp_0 then
socket = create_luasocket()
elseif "haproxy" == _exp_0 then
socket = create_luasocket("haproxy")
elseif "cqueues" == _exp_0 then
socket = require("pgmoon.cqueues").CqueuesSocket()
else
Expand Down
14 changes: 11 additions & 3 deletions pgmoon/socket.moon
Original file line number Diff line number Diff line change
Expand Up @@ -64,10 +64,14 @@ create_luasocket = do
true
}

(...) ->
socket = require("socket")
(socket_type, ...) ->
socket = if socket_type == "haproxy"
core.tcp ...
else
require("socket").tcp ...

proxy = {
sock: socket.tcp ...
sock: socket
}
for k,v in pairs method_overrides
proxy[k] = v
Expand All @@ -85,6 +89,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 +99,8 @@ create_luasocket = do
ngx.socket.tcp!
when "luasocket"
create_luasocket!
when "haproxy"
create_luasocket("haproxy")
when "cqueues"
require("pgmoon.cqueues").CqueuesSocket!
else
Expand Down

0 comments on commit bb54268

Please sign in to comment.