From 047e617abb282a7e8000e5da554011231e5d5537 Mon Sep 17 00:00:00 2001 From: Matt Campbell Date: Sun, 9 Jan 2022 12:59:07 -0500 Subject: [PATCH 1/2] implement haproxy socket type from configuration --- README.md | 4 ++-- pgmoon-dev-1.rockspec | 3 ++- pgmoon/socket.lua | 13 ++++++++++--- pgmoon/socket.moon | 12 +++++++++--- 4 files changed, 23 insertions(+), 9 deletions(-) diff --git a/README.md b/README.md index d1d6e4f..6d20a1c 100644 --- a/README.md +++ b/README.md @@ -110,7 +110,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: @@ -122,7 +122,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, `"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) diff --git a/pgmoon-dev-1.rockspec b/pgmoon-dev-1.rockspec index f672764..0dd3407 100644 --- a/pgmoon-dev-1.rockspec +++ b/pgmoon-dev-1.rockspec @@ -2,7 +2,8 @@ package = "pgmoon" version = "dev-1" source = { - url = "git://github.com/leafo/pgmoon.git" + url = "git://github.com/mecampbellsoup/pgmoon.git", + tag = "luasocket-to-haproxy" } description = { diff --git a/pgmoon/socket.lua b/pgmoon/socket.lua index 795b417..6c38976 100644 --- a/pgmoon/socket.lua +++ b/pgmoon/socket.lua @@ -25,9 +25,14 @@ do settimeout = true } luasocket = { - tcp = function(...) - local socket = require("socket") - local sock = socket.tcp(...) + tcp = function(socket_type, ...) + local sock + if socket_type == "haproxy" then + sock = core.tcp(...) + else + local socket = require("socket") + sock = socket.tcp(...) + end local proxy = setmetatable({ sock = sock, send = function(self, ...) @@ -98,6 +103,8 @@ return { socket = ngx.socket.tcp() elseif "luasocket" == _exp_0 then socket = luasocket.tcp() + elseif "haproxy" == _exp_0 then + socket = luasocket.tcp(socket_type) elseif "cqueues" == _exp_0 then socket = require("pgmoon.cqueues").CqueuesSocket() else diff --git a/pgmoon/socket.moon b/pgmoon/socket.moon index 2b9ca44..ac804d0 100644 --- a/pgmoon/socket.moon +++ b/pgmoon/socket.moon @@ -23,9 +23,13 @@ luasocket = do } { - tcp: (...) -> - socket = require "socket" - sock = socket.tcp ... + tcp: (socket_type, ...) -> + local sock + if socket_type == "haproxy" + sock = core.tcp ... + else + socket = require "socket" + sock = socket.tcp ... proxy = setmetatable { :sock send: (...) => @sock\send flatten ... @@ -80,6 +84,8 @@ luasocket = do ngx.socket.tcp! when "luasocket" luasocket.tcp! + when "haproxy" + luasocket.tcp(socket_type) when "cqueues" require("pgmoon.cqueues").CqueuesSocket! else From 4f01149ed268cd2cef9a8afc953337fe3bec071d Mon Sep 17 00:00:00 2001 From: Matt Campbell Date: Thu, 31 Mar 2022 10:44:50 -0400 Subject: [PATCH 2/2] fix: Update source url protocol in rockspec --- pgmoon-dev-1.rockspec | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/pgmoon-dev-1.rockspec b/pgmoon-dev-1.rockspec index 0dd3407..14d6745 100644 --- a/pgmoon-dev-1.rockspec +++ b/pgmoon-dev-1.rockspec @@ -2,7 +2,7 @@ package = "pgmoon" version = "dev-1" source = { - url = "git://github.com/mecampbellsoup/pgmoon.git", + url = "git+https://github.com/mecampbellsoup/pgmoon.git", tag = "luasocket-to-haproxy" }