diff --git a/pgmoon/cqueues.lua b/pgmoon/cqueues.lua index ee6796a..9e5b238 100644 --- a/pgmoon/cqueues.lua +++ b/pgmoon/cqueues.lua @@ -63,11 +63,6 @@ do _base_0.__class = _class_0 CqueuesSocket = _class_0 end -local new -new = function() - return CqueuesSocket(), "cqueues" -end return { - new = new, CqueuesSocket = CqueuesSocket } diff --git a/pgmoon/cqueues.moon b/pgmoon/cqueues.moon index cc19b9a..47237dc 100644 --- a/pgmoon/cqueues.moon +++ b/pgmoon/cqueues.moon @@ -45,9 +45,6 @@ class CqueuesSocket -- openresty pooling interface, disable pooling getreusedtimes: => 0 -new = -> - CqueuesSocket!, "cqueues" - -{ :new , :CqueuesSocket } +{ :CqueuesSocket } diff --git a/pgmoon/init.lua b/pgmoon/init.lua index 29d2437..a53a63e 100644 --- a/pgmoon/init.lua +++ b/pgmoon/init.lua @@ -664,7 +664,7 @@ do _base_0.__index = _base_0 _class_0 = setmetatable({ __init = function(self, opts) - self.sock, self.sock_type = socket.new() + self.sock, self.sock_type = socket.new(opts and opts.socket_type) if opts then self.user = opts.user self.host = opts.host diff --git a/pgmoon/init.moon b/pgmoon/init.moon index d335f83..c8ff8eb 100644 --- a/pgmoon/init.moon +++ b/pgmoon/init.moon @@ -151,7 +151,7 @@ class Postgres @set_type_oid tonumber(res.oid), "hstore" new: (opts) => - @sock, @sock_type = socket.new! + @sock, @sock_type = socket.new opts and opts.socket_type if opts @user = opts.user diff --git a/pgmoon/socket.lua b/pgmoon/socket.lua index d3f4b17..5b41300 100644 --- a/pgmoon/socket.lua +++ b/pgmoon/socket.lua @@ -79,11 +79,25 @@ do } end return { - new = function() - if ngx and ngx.get_phase() ~= "init" then - return ngx.socket.tcp(), "nginx" + new = function(socket_type) + if socket_type == nil then + if ngx and ngx.get_phase() ~= "init" then + socket_type = "nginx" + else + socket_type = "luasocket" + end + end + local socket + local _exp_0 = socket_type + if "nginx" == _exp_0 then + socket = ngx.socket.tcp() + elseif "luasocket" == _exp_0 then + socket = luasocket.tcp() + elseif "cqueues" == _exp_0 then + socket = require("pgmoon.cqueues").CqueuesSocket() else - return luasocket.tcp(), "luasocket" + socket = error("unknown socket type: " .. tostring(socket_type)) end + return socket, socket_type end } diff --git a/pgmoon/socket.moon b/pgmoon/socket.moon index 8103e0f..ed927e9 100644 --- a/pgmoon/socket.moon +++ b/pgmoon/socket.moon @@ -65,12 +65,25 @@ luasocket = do } { - new: -> - -- Fallback to LuaSocket is only required when pgmoon - -- runs in plain Lua, or in the init_by_lua context. - if ngx and ngx.get_phase! != "init" - ngx.socket.tcp!, "nginx" - else - luasocket.tcp!, "luasocket" + new: (socket_type) -> + if socket_type == nil + -- choose the default socket, try to use nginx, otherwise default to + -- luasocket + socket_type = if ngx and ngx.get_phase! != "init" + "nginx" + else + "luasocket" + + socket = switch socket_type + when "nginx" + ngx.socket.tcp! + when "luasocket" + luasocket.tcp! + when "cqueues" + require("pgmoon.cqueues").CqueuesSocket! + else + error "unknown socket type: #{socket_type}" + + socket, socket_type }