-
Notifications
You must be signed in to change notification settings - Fork 6
/
dshm.lua
361 lines (298 loc) · 8.11 KB
/
dshm.lua
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
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
local escape_uri = ngx.escape_uri
local unescape_uri = ngx.unescape_uri
local match = string.match
local tcp = ngx.socket.tcp
local strlen = string.len
local setmetatable = setmetatable
local tonumber = tonumber
local math = math
local tostring = tostring
local table = table
local ngx = ngx
local _M = {
_VERSION = '0.99',
PROTOCOL_ERROR = "protocol_error"
}
local mt = { __index = _M }
local function escape(key)
local i = 1
local result = {}
for m in (tostring(key) .. ":"):gmatch("([^:]*):") do
result[i] = escape_uri(m)
i = i + 1
end
ngx.log(ngx.DEBUG, "escape result : ", table.concat(result, ":"))
return table.concat(result, ":")
end
function _M.new(self, opts)
local sock, err = tcp()
if not sock then
return nil, err
end
local escape_key = escape
local unescape_key = unescape_uri
if opts then
local key_transform = opts.key_transform
if key_transform then
escape_key = key_transform[1]
unescape_key = key_transform[2]
if not escape_key or not unescape_key then
return nil, "expecting key_transform = { escape, unescape } table"
end
end
end
return setmetatable({
sock = sock,
escape_key = escape_key,
unescape_key = unescape_key,
}, mt)
end
local function read_response_line(self, data)
local sock = self.sock
if not sock then
return nil, "not initialized"
end
if not data then
local line, err = sock:receive()
if not line then
if err == "timeout" then
sock:close()
end
return nil, err
end
ngx.log(ngx.DEBUG, "RECEIVE : ", line)
local len = match(line, '^LEN (%d+)$')
if len then
ngx.log(ngx.DEBUG, "LEN : ", len)
return "LEN", tonumber(len)
end
local done = line == "DONE"
if done then
ngx.log(ngx.DEBUG, "DONE")
return "DONE", nil
end
local error = match(line, '^ERROR (%.+)$')
if error then
ngx.log(ngx.DEBUG, "ERROR", error)
return "ERROR", error
end
ngx.log(ngx.DEBUG, "PROTOCOL ERROR")
return nil, _M.PROTOCOL_ERROR
else
local data, err = sock:receive(data)
if not data then
if err == "timeout" then
sock:close()
end
return nil, err
end
ngx.log(ngx.DEBUG, "RECEIVE : ", data)
-- Discard trailing \r\n
local trail, err = sock:receive()
if not trail then
if err == "timeout" then
sock:close()
end
return nil, err
end
ngx.log(ngx.DEBUG, "TRAIL RECIEVED")
ngx.log(ngx.DEBUG, "DATA : ", data)
return "DATA", data
end
end
function _M.get(self, key)
ngx.log(ngx.DEBUG, "Get : ", key)
local sock = self.sock
if not sock then
return nil, "not initialized"
end
local bytes, err = sock:send("get " .. self.escape_key(key) .. "\r\n" )
if not bytes then
return nil, err
end
local resp, data = read_response_line(self)
if resp == "LEN" then
resp, data = read_response_line(self, data)
if resp == "DATA" then
local resp, _ = read_response_line(self)
if resp == "DONE" then
return data, nil
else
return nil, _M.PROTOCOL_ERROR
end
else
return nil, _M.PROTOCOL_ERROR
end
elseif resp == "ERROR" then
if data == "not_found" then
return nil, "not found"
else
return nil, data
end
else
return nil, _M.PROTOCOL_ERROR
end
end
function _M.delete(self, key)
ngx.log(ngx.DEBUG, "Delete : ", key)
local sock = self.sock
if not sock then
return nil, "not initialized"
end
local bytes, err = sock:send("delete " .. self.escape_key(key) .. "\r\n" )
if not bytes then
return nil, err
end
local resp, data = read_response_line(self)
if resp == "DONE" then
return 1, nil
elseif resp == "ERROR" then
return nil, data
else
return nil, _M.PROTOCOL_ERROR
end
end
function _M.incr(self, key, value, init)
ngx.log(ngx.DEBUG, "Incr : ", key, ", Value : ", value, ", Init : ", init)
local sock = self.sock
if not sock then
return nil, "not initialized"
end
local l_init = 0
if init then
l_init = init
end
local bytes, err = sock:send("incr " .. self.escape_key(key) .. " " .. value .. " " .. l_init .. "\r\n")
if not bytes then
return nil, err
end
local resp, data = read_response_line(self)
if resp == "LEN" then
resp, data = read_response_line(self, data)
if resp == "DATA" then
local resp, _ = read_response_line(self)
if resp == "DONE" then
return data, nil
else
return nil, _M.PROTOCOL_ERROR
end
else
return nil, _M.PROTOCOL_ERROR
end
elseif resp == "ERROR" then
return nil, data
else
return nil, _M.PROTOCOL_ERROR
end
end
function _M.set(self, key, value, exptime)
if not exptime or exptime == 0 then
exptime = 0
else
exptime = math.floor(exptime + 0.5)
end
ngx.log(ngx.DEBUG, "Key : ", key, ", Value : ", value, ", Exp : ", exptime)
local sock = self.sock
if not sock then
return nil, "not initialized"
end
local req = "set " .. self.escape_key(key) .. " "
.. exptime .. " " .. strlen(value) .. "\r\n" .. value
.. "\r\n"
local bytes, err = sock:send(req)
if not bytes then
return nil, err
end
local resp, data = read_response_line(self)
if resp == "LEN" then
resp, data = read_response_line(self, data)
if resp == "DATA" then
local resp, _ = read_response_line(self)
if resp == "DONE" then
return data, nil
else
return nil, _M.PROTOCOL_ERROR
end
else
return nil, _M.PROTOCOL_ERROR
end
elseif resp == "ERROR" then
return nil, data
else
return nil, _M.PROTOCOL_ERROR
end
end
function _M.touch(self, key, exptime)
if not exptime or exptime == 0 then
exptime = 0
else
exptime = math.floor(exptime + 0.5)
end
ngx.log(ngx.DEBUG, "Touch : ", key, ", Exp : ", exptime)
local sock = self.sock
if not sock then
ngx.log(ngx.DEBUG, "Socket not initialized")
return nil, "not initialized"
end
local bytes, err = sock:send("touch " .. self.escape_key(key) .. " "
.. exptime .. "\r\n")
if not bytes then
return nil, err
end
local resp, data = read_response_line(self)
if resp == "DONE" then
return 1, nil
elseif resp == "ERROR" then
return nil, data
else
return nil, _M.PROTOCOL_ERROR
end
end
function _M.quit(self)
local sock = self.sock
if not sock then
return nil, "not initialized"
end
local bytes, err = sock:send("quit\r\n")
if not bytes then
return nil, err
end
return 1
end
function _M.set_timeout(self, timeout)
local sock = self.sock
if not sock then
return nil, "not initialized"
end
sock:settimeout(timeout)
return 1
end
function _M.connect(self, ...)
local sock = self.sock
if not sock then
return nil, "not initialized"
end
return sock:connect(...)
end
function _M.set_keepalive(self, ...)
local sock = self.sock
if not sock then
return nil, "not initialized"
end
return sock:setkeepalive(...)
end
function _M.get_reused_times(self)
local sock = self.sock
if not sock then
return nil, "not initialized"
end
return sock:getreusedtimes()
end
function _M.close(self)
local sock = self.sock
if not sock then
return nil, "not initialized"
end
return sock:close()
end
return _M