forked from daurnimator/mongol
-
Notifications
You must be signed in to change notification settings - Fork 66
Expand file tree
/
Copy pathinit.lua
More file actions
executable file
·142 lines (115 loc) · 3.33 KB
/
init.lua
File metadata and controls
executable file
·142 lines (115 loc) · 3.33 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
module("resty.mongol", package.seeall)
local mod_name = (...)
local assert , pcall = assert , pcall
local ipairs , pairs = ipairs , pairs
local setmetatable = setmetatable
local socket = ngx.socket.tcp
local connmethods = { }
local connmt = { __index = connmethods }
local dbmt = require ( mod_name .. ".dbmt" )
local bson = require ( mod_name .. ".bson" )
null = bson.bson_null
function connmethods:ismaster()
local db = self:new_db_handle("admin")
local r, err = db:cmd({ismaster = true})
if not r then
return nil, err
end
return r.ismaster, r.hosts
end
local function parse_host ( str )
local host , port = str:match ( "([^:]+):?(%d*)" )
port = port or 27017
return host , port
end
function connmethods:getprimary ( searched )
searched = searched or { [ self.host .. ":" .. self.port ] = true }
local db = self:new_db_handle("admin")
local r, err = db:cmd({ ismaster = true })
if not r then
return nil, "query admin failed: "..err
elseif r.ismaster then return self
else
for i , v in ipairs ( r.hosts ) do
if not searched[v] then
searched[v] = true
local host, port = parse_host(v)
local conn = new()
local ok, err = conn:connect(host, port)
if not ok then
return nil, "connect failed: "..err..v
end
local found = conn:getprimary(searched)
if found then
return found
end
end
end
end
return nil , "No master server found"
end
function connmethods:databases()
local db = self:new_db_handle("admin")
local r = assert ( db:cmd({ listDatabases = true } ))
return r.databases
end
function connmethods:shutdown()
local db = self:new_db_handle("admin")
db:cmd({ shutdown = true } )
end
function connmethods:new_db_handle ( db )
if not db then
return nil
end
return setmetatable ( {
conn = self ;
db = db ;
} , dbmt )
end
function connmethods:set_timeout(timeout)
local sock = self.sock
if not sock then
return nil, "not initialized"
end
return sock:settimeout(timeout)
end
function connmethods:set_keepalive(...)
local sock = self.sock
if not sock then
return nil, "not initialized"
end
return sock:setkeepalive(...)
end
function connmethods:get_reused_times()
local sock = self.sock
if not sock then
return nil, "not initialized"
end
return sock:getreusedtimes()
end
function connmethods:connect(host, port)
self.host = host or self.host
self.port = port or self.port
local sock = self.sock
return sock:connect(self.host, self.port)
end
function connmethods:close()
local sock = self.sock
if not sock then
return nil, "not initialized"
end
return sock:close()
end
connmt.__call = connmethods.new_db_handle
function new(self)
return setmetatable ( {
sock = socket();
host = "localhost";
port = 27017;
} , connmt )
end
-- to prevent use of casual module global variables
getmetatable(resty.mongol).__newindex = function (table, key, val)
error('attempt to write to undeclared variable "' .. key .. '": '
.. debug.traceback())
end