Skip to content

Commit

Permalink
allow initializing with socket type
Browse files Browse the repository at this point in the history
  • Loading branch information
leafo committed Oct 21, 2016
1 parent 80a1d0a commit 6bd4dc8
Show file tree
Hide file tree
Showing 6 changed files with 41 additions and 22 deletions.
5 changes: 0 additions & 5 deletions pgmoon/cqueues.lua
Expand Up @@ -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
}
5 changes: 1 addition & 4 deletions pgmoon/cqueues.moon
Expand Up @@ -45,9 +45,6 @@ class CqueuesSocket
-- openresty pooling interface, disable pooling
getreusedtimes: => 0

new = ->
CqueuesSocket!, "cqueues"

{ :new , :CqueuesSocket }
{ :CqueuesSocket }


2 changes: 1 addition & 1 deletion pgmoon/init.lua
Expand Up @@ -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
Expand Down
2 changes: 1 addition & 1 deletion pgmoon/init.moon
Expand Up @@ -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
Expand Down
22 changes: 18 additions & 4 deletions pgmoon/socket.lua
Expand Up @@ -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
}
27 changes: 20 additions & 7 deletions pgmoon/socket.moon
Expand Up @@ -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
}

0 comments on commit 6bd4dc8

Please sign in to comment.