srvRedisMgr.lua 1.8 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889
  1. --[[
  2. Descripttion:
  3. version:
  4. Author: Neo,Huang
  5. Date: 2022-07-04 14:49:56
  6. LastEditors: Neo,Huang
  7. LastEditTime: 2022-07-04 14:51:59
  8. --]]
  9. local skynet = require("skynet")
  10. local snax = require("snax")
  11. local lib_logger = require("log.lib_logger")
  12. local logger
  13. local REDIS_CONFIG = {}
  14. local REDIS_DB_POOL = {}
  15. local function init_redis_pool(name, cf)
  16. REDIS_CONFIG[name] = cf
  17. local pool = {}
  18. -- 每个链接创建3个服务
  19. for i = 1, #cf do
  20. local dbs = {}
  21. -- 每个服务创建10个链接
  22. for j = 1, 3 do
  23. local db = skynet.newservice("srvRedis", cjson_encode(cf[i]), 10)
  24. dbs[j] = db
  25. end
  26. pool[i] = dbs
  27. end
  28. REDIS_DB_POOL[name] = pool
  29. end
  30. local function destory_redis_pool(name)
  31. local pool = REDIS_DB_POOL[name]
  32. if not pool then
  33. return
  34. end
  35. for i = 1, #pool do
  36. local dbs = pool[i]
  37. for j = 1, #dbs do
  38. local db = dbs[j]
  39. snax.kill(db)
  40. end
  41. end
  42. end
  43. function init(cf)
  44. logger = lib_logger.get_logger("redisMgr")
  45. end
  46. function exit()
  47. for k, v in pairs(REDIS_DB_POOL) do
  48. destory_redis_pool(k)
  49. end
  50. REDIS_DB_POOL = {}
  51. end
  52. function response.init(name, cf)
  53. logger.info("init %s", name)
  54. if REDIS_CONFIG[name] then
  55. logger.warn("name %s already init", cf)
  56. return
  57. end
  58. init_redis_pool(name, cf)
  59. end
  60. function response.acquire(name)
  61. local cf = REDIS_CONFIG[name]
  62. if not REDIS_CONFIG[name] then
  63. logger.warn("sup acquire not %s config", name)
  64. return
  65. end
  66. local pool
  67. while true do
  68. pool = REDIS_DB_POOL[name]
  69. if not pool or #pool < #cf then
  70. skynet.sleep(100)
  71. else
  72. break
  73. end
  74. end
  75. if #pool == 0 then
  76. logger.error("sup response.acquire pool is emtpy")
  77. end
  78. return pool
  79. end