-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathpool.lua
More file actions
275 lines (201 loc) · 5.39 KB
/
pool.lua
File metadata and controls
275 lines (201 loc) · 5.39 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
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
-- 另一个版本
local redis = require "resty.redis"
local assert = assert
local print = print
local rawget = rawget
local setmetatable = setmetatable
local tonumber = tonumber
local str_byte = string.byte
local str_gmatch = string.gmatch
local str_sub = string.sub
local str_upper = string.upper
local function parse_request(sock)
local line, err = sock:receive()
if not line then
return nil, err
end
local prefix = str_byte(line)
if prefix == 42 then -- char '*'
local result = {}
local num = tonumber(str_sub(line, 2))
if num <= 0 then
return nil, "Wrong protocol format"
end
for i = 1, num do
local res, err = parse_request(sock)
if res == nil then
return nil, err
end
result[i] = res
end
return result
end
if prefix == 36 then -- char '$'
local size = tonumber(str_sub(line, 2))
if size < 0 then
return nil, "Wrong protocol format"
end
local result, err = sock:receive(size)
if not result then
return nil, err
end
local crlf, err = sock:receive(2)
if not crlf then
return nil, err
end
return result
end
-- inline
local result = {}
for res in str_gmatch(line, "%S+") do
result[#result + 1] = res
end
return result
end
local function fetch_response(sock)
local line, err = sock:receive()
if not line then
return nil, err
end
local result = {line, "\r\n"}
local prefix = str_byte(line)
if prefix == 42 then -- char '*'
local num = tonumber(str_sub(line, 2))
if num <= 0 then
return result
end
for i = 1, num do
local res, err = fetch_response(sock)
if res == nil then
return nil, err
end
for x = 1, #res do
result[#result + 1] = res[x]
end
end
elseif prefix == 36 then -- char '$'
local size = tonumber(str_sub(line, 2))
if size < 0 then
return result
end
local res, err = sock:receive(size)
if not res then
return nil, err
end
local crlf, err = sock:receive(2)
if not crlf then
return nil, err
end
result[#result + 1] = res
result[#result + 1] = crlf
end
return result
end
local function build_data(value)
local result = {"*", #value, "\r\n"}
for i = 1, #value do
local v = value[i]
result[#result + 1] = "$"
result[#result + 1] = #v
result[#result + 1] = "\r\n"
result[#result + 1] = v
result[#result + 1] = "\r\n"
end
return result
end
local function status_reply(message)
return "+" .. message .. "\r\n"
end
local function command_args(request)
local command = request[1]
command = str_upper(command)
local args = {}
for i = 2, #request do
args[#args + 1] = request[i]
end
return command, args
end
local function exit(err)
ngx.log(ngx.NOTICE, err)
return ngx.exit(ngx.ERROR)
end
local _M = {}
_M._VERSION = "1.0"
function _M.new(self, config)
local t = {
_ip = config.ip or "127.0.0.1",
_port = config.port or 6379,
_timeout = config.timeout or 100000,
_size = config.size or 10,
_auth = config.auth,
_db = config.db,
}
return setmetatable(t, { __index = _M })
end
function _M.run(self)
local ip = self._ip
local port = self._port
local timeout = self._timeout
local size = self._size
local auth = self._auth
local db = self._db
local red = redis:new()
local ok, err = red:connect(ip, port)
if not ok then
return exit(err)
end
if auth then
local times = assert(red:get_reused_times())
if times == 0 then
local ok, err = red:auth(auth)
if not ok then
return exit(err)
end
end
end
if db then
local ok, err = red:select(db)
if not ok then
return exit(err)
end
end
local database = 0
local transactional = false
local upstream_sock = rawget(red, "_sock")
local downstream_sock = assert(ngx.req.socket(true))
while true do
local request, err = parse_request(downstream_sock)
if not request then
if err == "client aborted" then
break
end
return exit(err)
end
local command, args = command_args(request)
if command == "QUIT" then
downstream_sock:send(status_reply("OK"))
break
end
upstream_sock:send(build_data(request))
local response, err = fetch_response(upstream_sock)
if not response then
return exit(err)
end
if command == "SELECT" then
database = tonumber(args[1])
elseif command == "MULTI" then
transactional = true
elseif command == "EXEC" or command == "DISCARD" then
transactional = false
end
downstream_sock:send(response)
end
if database ~= 0 then
red:select(0)
end
if transactional then
red:discard()
end
red:set_keepalive(timeout, size)
end
return _M