-
Notifications
You must be signed in to change notification settings - Fork 1
/
redis.lua
139 lines (100 loc) · 2.1 KB
/
redis.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
local redis = require "resty.redis"
local assert = assert
local print = print
local rawget = rawget
local setmetatable = setmetatable
local tonumber = tonumber
local byte = string.byte
local sub = string.sub
local function parse(sock)
local line, err = sock:receive()
if not line then
if err == "timeout" then
sock:close()
end
return nil, err
end
local result = line .. "\r\n"
local prefix = byte(line)
if prefix == 42 then -- char '*'
local num = tonumber(sub(line, 2))
if num <= 0 then
return result
end
for i = 1, num do
local res, err = parse(sock)
if res == nil then
return nil, err
end
result = result .. res
end
elseif prefix == 36 then -- char '$'
local size = tonumber(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 .. res .. crlf
end
return result
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 100,
_size = config.size or 10,
_auth = config.auth,
}
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 downstream_sock = assert(ngx.req.socket(true))
while true do
local res, err = parse(downstream_sock)
if not res then
return exit(err)
end
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
local upstream_sock = rawget(red, "_sock")
upstream_sock:send(res)
local res, err = parse(upstream_sock)
if not res then
return exit(err)
end
red:set_keepalive(timeout, size)
downstream_sock:send(res)
end
end
return _M