Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

implement haproxy socket type from configuration #125

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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")
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@leafo using your create_luasocket interface now instead, just using the raw core.tcp wasn't playing well with the expected socket API.

when "cqueues"
require("pgmoon.cqueues").CqueuesSocket!
else
Expand Down