Skip to content

Commit 3c5d979

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 3c5d979

File tree

4 files changed

+26
-9
lines changed

4 files changed

+26
-9
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: 12 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -72,10 +72,15 @@ do
7272
return true
7373
end
7474
}
75-
create_luasocket = function(...)
76-
local socket = require("socket")
75+
create_luasocket = function(socket_type, ...)
76+
local socket
77+
if socket_type == "haproxy" then
78+
socket = core.tcp(...)
79+
else
80+
socket = require("socket").tcp(...)
81+
end
7782
local proxy = {
78-
sock = socket.tcp(...)
83+
sock = socket
7984
}
8085
for k, v in pairs(method_overrides) do
8186
proxy[k] = v
@@ -89,6 +94,8 @@ return {
8994
if socket_type == nil then
9095
if ngx and ngx.get_phase() ~= "init" then
9196
socket_type = "nginx"
97+
elseif core and core.get_info() then
98+
socket_type = "haproxy"
9299
else
93100
socket_type = "luasocket"
94101
end
@@ -99,6 +106,8 @@ return {
99106
socket = ngx.socket.tcp()
100107
elseif "luasocket" == _exp_0 then
101108
socket = create_luasocket()
109+
elseif "haproxy" == _exp_0 then
110+
socket = create_luasocket("haproxy")
102111
elseif "cqueues" == _exp_0 then
103112
socket = require("pgmoon.cqueues").CqueuesSocket()
104113
else

pgmoon/socket.moon

Lines changed: 11 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -64,10 +64,14 @@ create_luasocket = do
6464
true
6565
}
6666

67-
(...) ->
68-
socket = require("socket")
67+
(socket_type, ...) ->
68+
socket = if socket_type == "haproxy"
69+
core.tcp ...
70+
else
71+
require("socket").tcp ...
72+
6973
proxy = {
70-
sock: socket.tcp ...
74+
sock: socket
7175
}
7276
for k,v in pairs method_overrides
7377
proxy[k] = v
@@ -85,6 +89,8 @@ create_luasocket = do
8589
-- luasocket
8690
socket_type = if ngx and ngx.get_phase! != "init"
8791
"nginx"
92+
elseif core and core.get_info!
93+
"haproxy"
8894
else
8995
"luasocket"
9096

@@ -93,6 +99,8 @@ create_luasocket = do
9399
ngx.socket.tcp!
94100
when "luasocket"
95101
create_luasocket!
102+
when "haproxy"
103+
create_luasocket("haproxy")
96104
when "cqueues"
97105
require("pgmoon.cqueues").CqueuesSocket!
98106
else

0 commit comments

Comments
 (0)