Advertisement
Guest User

Untitled

a guest
Jul 24th, 2017
83
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.15 KB | None | 0 0
  1. local _M = { _VERSION = "0.09"}
  2. local cjson = require 'cjson'
  3. local encode = cjson.encode
  4.  
  5. function _M.redis()
  6. local m = {}
  7. local mt = {
  8. __index = {
  9. host = '127.0.0.1',
  10. port = 6380,
  11. name = 'cdn'
  12. }
  13. }
  14.  
  15. local function new(opts)
  16. local m = {}
  17. local redis = require "resty.redis"
  18. -- redis.add_commands("sentinel")
  19. local red = redis:new()
  20.  
  21. local opts = opts or {}
  22. local opts = setmetatable(opts, mt)
  23. local ok, err = red:connect(opts.host, opts.port)
  24. if not ok then
  25. return nil, err
  26. end
  27.  
  28. function m.cmd(cmd, ...)
  29. local res, err = red[cmd](red, ...)
  30. assert(res, err)
  31. return res
  32. end
  33.  
  34. function m.init_pipeline()
  35. red:init_pipeline()
  36. return red
  37. end
  38.  
  39. function m.commit_pipeline()
  40. local res, err = red:commit_pipeline()
  41. assert(res, err)
  42. return res
  43. end
  44.  
  45. function m.keepalive()
  46. local ok, err = red:set_keepalive()
  47. assert(ok, err)
  48. end
  49.  
  50. function m.array_to_hash(array)
  51. return red:array_to_hash(array)
  52. end
  53.  
  54. m.red = red
  55. return m
  56. end
  57.  
  58. m.new = new
  59.  
  60. function m.discover_master()
  61. local redis = require "resty.redis"
  62. redis.add_commands("sentinel")
  63. local sentinels = {'10.100.3.38', '10.100.3.39', '10.100.3.40', '10.100.3.41'}
  64. local s = (sentinels[ math.random(#sentinels)])
  65.  
  66. ngx.log(ngx.ERR, s)
  67. local red, err = new({host = s, port = 17501})
  68. local res
  69. if red then
  70. local ok, err = pcall(function()
  71. res = red.cmd('sentinel', 'get-master-addr-by-name', 'cdn')
  72. red.keepalive()
  73. end)
  74. if not ok then
  75. return nil, err
  76. end
  77. else
  78. return nil, err
  79. end
  80.  
  81. local opts = {host = res[1], port = res[2]}
  82. ngx.log(ngx.ERR, encode(opts))
  83. return new(opts)
  84. end
  85.  
  86. function m.discover_slave()
  87. local redis = require "resty.redis"
  88. redis.add_commands("sentinel")
  89. local sentinels = {'10.100.3.38', '10.100.3.39', '10.100.3.40', '10.100.3.41'}
  90. local s = (sentinels[ math.random(#sentinels)])
  91.  
  92. ngx.log(ngx.ERR, s)
  93. local red, err = new({host = s, port = 17501})
  94. local res
  95. if red then
  96. local ok, err = pcall(function()
  97. res = red.cmd('sentinel', 'slaves', 'cdn')
  98. red.keepalive()
  99. end)
  100. if not ok then
  101. return nil, err
  102. end
  103. else
  104. return nil, err
  105. end
  106.  
  107. local res = (res[ math.random(#res)])
  108. local base = {}
  109. for i = 1, #res, 2 do
  110. base[res[i]] = res[i+1]
  111. end
  112.  
  113. local opts = {host = base.ip, port = base.port}
  114. ngx.log(ngx.ERR, encode(opts))
  115. return new(opts)
  116.  
  117. end
  118. return m
  119. end
  120.  
  121. function _M.mysql(opts)
  122.  
  123. local mt = {
  124. __index = {
  125. host = "10.100.10.6",
  126. port = 3306,
  127. database = "titanCDN",
  128. user = "titanCDN",
  129. password = "-M3[FYPpy@nH",
  130. }
  131. }
  132.  
  133. local m = {}
  134.  
  135. local mysql = require "resty.mysql"
  136. local db, err = mysql:new()
  137. if not db then
  138. return nil, err
  139. end
  140.  
  141. local opts = opts or {}
  142. local opts = setmetatable(opts, mt)
  143. local ok, err, errno, sqlstate = db:connect(opts)
  144. if not ok then
  145. return nil, err
  146. end
  147.  
  148. function m.send_query(sql)
  149. local bytes, err = db:send_query(sql)
  150. assert(bytes, err)
  151. local result = {}
  152. repeat
  153. local res, err, errno, sqlstate = db:read_result()
  154. assert(res, err)
  155. table.insert(result, res)
  156. until err ~= "again"
  157. return result
  158. end
  159.  
  160. function m.query(sql)
  161. local res, err = db:query(sql)
  162. assert(res, err)
  163. return res
  164. end
  165.  
  166.  
  167. function m.keepalive()
  168. local ok, err = db:set_keepalive()
  169. assert(ok, err)
  170. end
  171.  
  172. return m
  173. end
  174.  
  175. return _M
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement