Skip to content

Commit fcbfdc8

Browse files
implement haproxy socket type from configuration
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
1 parent 4bc922e commit fcbfdc8

File tree

4 files changed

+11
-3
lines changed

4 files changed

+11
-3
lines changed

README.md

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -127,7 +127,7 @@ Functions in table returned by `require("pgmoon")`:
127127

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

132132
Available options:
133133

@@ -139,7 +139,7 @@ Available options:
139139
* `"ssl"`: enable ssl (default: `false`)
140140
* `"ssl_verify"`: verify server certificate (default: `nil`)
141141
* `"ssl_required"`: abort the connection if the server does not support SSL connections (default: `nil`)
142-
* `"socket_type"`: the type of socket to use, one of: `"nginx"`, `"luasocket"`, `cqueues` (default: `"nginx"` if in nginx, `"luasocket"` otherwise)
142+
* `"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)
143143
* `"application_name"`: set the name of the connection as displayed in `pg_stat_activity`. (default: `"pgmoon"`)
144144
* `"pool"`: (OpenResty only) name of pool to use when using OpenResty cosocket (default: `"#{host}:#{port}:#{database}"`)
145145
* `"pool_size"`: (OpenResty only) Passed directly to OpenResty cosocket connect function, [see docs](https://github.com/openresty/lua-nginx-module#tcpsockconnect)

lint_config.lua

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11

22
return {
33
whitelist_globals = {
4-
["."] = {"ngx"}
4+
["."] = {"ngx", "core"}
55
}
66
}

pgmoon/socket.lua

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -89,6 +89,8 @@ return {
8989
if socket_type == nil then
9090
if ngx and ngx.get_phase() ~= "init" then
9191
socket_type = "nginx"
92+
elseif core and core.get_info() then
93+
socket_type = "haproxy"
9294
else
9395
socket_type = "luasocket"
9496
end
@@ -99,6 +101,8 @@ return {
99101
socket = ngx.socket.tcp()
100102
elseif "luasocket" == _exp_0 then
101103
socket = create_luasocket()
104+
elseif "haproxy" == _exp_0 then
105+
socket = core.tcp(...)
102106
elseif "cqueues" == _exp_0 then
103107
socket = require("pgmoon.cqueues").CqueuesSocket()
104108
else

pgmoon/socket.moon

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -85,6 +85,8 @@ create_luasocket = do
8585
-- luasocket
8686
socket_type = if ngx and ngx.get_phase! != "init"
8787
"nginx"
88+
elseif core and core.get_info!
89+
"haproxy"
8890
else
8991
"luasocket"
9092

@@ -93,6 +95,8 @@ create_luasocket = do
9395
ngx.socket.tcp!
9496
when "luasocket"
9597
create_luasocket!
98+
when "haproxy"
99+
core.tcp ...
96100
when "cqueues"
97101
require("pgmoon.cqueues").CqueuesSocket!
98102
else

0 commit comments

Comments
 (0)