Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- local _M = { _VERSION = "0.09"}
- local cjson = require 'cjson'
- local encode = cjson.encode
- function _M.redis()
- local m = {}
- local mt = {
- __index = {
- host = '127.0.0.1',
- port = 6380,
- name = 'cdn'
- }
- }
- local function new(opts)
- local m = {}
- local redis = require "resty.redis"
- -- redis.add_commands("sentinel")
- local red = redis:new()
- local opts = opts or {}
- local opts = setmetatable(opts, mt)
- local ok, err = red:connect(opts.host, opts.port)
- if not ok then
- return nil, err
- end
- function m.cmd(cmd, ...)
- local res, err = red[cmd](red, ...)
- assert(res, err)
- return res
- end
- function m.init_pipeline()
- red:init_pipeline()
- return red
- end
- function m.commit_pipeline()
- local res, err = red:commit_pipeline()
- assert(res, err)
- return res
- end
- function m.keepalive()
- local ok, err = red:set_keepalive()
- assert(ok, err)
- end
- function m.array_to_hash(array)
- return red:array_to_hash(array)
- end
- m.red = red
- return m
- end
- m.new = new
- function m.discover_master()
- local redis = require "resty.redis"
- redis.add_commands("sentinel")
- local sentinels = {'10.100.3.38', '10.100.3.39', '10.100.3.40', '10.100.3.41'}
- local s = (sentinels[ math.random(#sentinels)])
- ngx.log(ngx.ERR, s)
- local red, err = new({host = s, port = 17501})
- local res
- if red then
- local ok, err = pcall(function()
- res = red.cmd('sentinel', 'get-master-addr-by-name', 'cdn')
- red.keepalive()
- end)
- if not ok then
- return nil, err
- end
- else
- return nil, err
- end
- local opts = {host = res[1], port = res[2]}
- ngx.log(ngx.ERR, encode(opts))
- return new(opts)
- end
- function m.discover_slave()
- local redis = require "resty.redis"
- redis.add_commands("sentinel")
- local sentinels = {'10.100.3.38', '10.100.3.39', '10.100.3.40', '10.100.3.41'}
- local s = (sentinels[ math.random(#sentinels)])
- ngx.log(ngx.ERR, s)
- local red, err = new({host = s, port = 17501})
- local res
- if red then
- local ok, err = pcall(function()
- res = red.cmd('sentinel', 'slaves', 'cdn')
- red.keepalive()
- end)
- if not ok then
- return nil, err
- end
- else
- return nil, err
- end
- local res = (res[ math.random(#res)])
- local base = {}
- for i = 1, #res, 2 do
- base[res[i]] = res[i+1]
- end
- local opts = {host = base.ip, port = base.port}
- ngx.log(ngx.ERR, encode(opts))
- return new(opts)
- end
- return m
- end
- function _M.mysql(opts)
- local mt = {
- __index = {
- host = "10.100.10.6",
- port = 3306,
- database = "titanCDN",
- user = "titanCDN",
- password = "-M3[FYPpy@nH",
- }
- }
- local m = {}
- local mysql = require "resty.mysql"
- local db, err = mysql:new()
- if not db then
- return nil, err
- end
- local opts = opts or {}
- local opts = setmetatable(opts, mt)
- local ok, err, errno, sqlstate = db:connect(opts)
- if not ok then
- return nil, err
- end
- function m.send_query(sql)
- local bytes, err = db:send_query(sql)
- assert(bytes, err)
- local result = {}
- repeat
- local res, err, errno, sqlstate = db:read_result()
- assert(res, err)
- table.insert(result, res)
- until err ~= "again"
- return result
- end
- function m.query(sql)
- local res, err = db:query(sql)
- assert(res, err)
- return res
- end
- function m.keepalive()
- local ok, err = db:set_keepalive()
- assert(ok, err)
- end
- return m
- end
- return _M
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement