1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889 |
- --[[
- Descripttion:
- version:
- Author: Neo,Huang
- Date: 2022-07-04 14:49:56
- LastEditors: Neo,Huang
- LastEditTime: 2022-07-04 14:51:59
- --]]
- local skynet = require("skynet")
- local snax = require("snax")
- local lib_logger = require("log.lib_logger")
- local logger
- local REDIS_CONFIG = {}
- local REDIS_DB_POOL = {}
- local function init_redis_pool(name, cf)
- REDIS_CONFIG[name] = cf
- local pool = {}
- -- 每个链接创建3个服务
- for i = 1, #cf do
- local dbs = {}
- -- 每个服务创建10个链接
- for j = 1, 3 do
- local db = skynet.newservice("srvRedis", cjson_encode(cf[i]), 10)
- dbs[j] = db
- end
- pool[i] = dbs
- end
- REDIS_DB_POOL[name] = pool
- end
- local function destory_redis_pool(name)
- local pool = REDIS_DB_POOL[name]
- if not pool then
- return
- end
- for i = 1, #pool do
- local dbs = pool[i]
- for j = 1, #dbs do
- local db = dbs[j]
- snax.kill(db)
- end
- end
- end
- function init(cf)
- logger = lib_logger.get_logger("redisMgr")
- end
- function exit()
- for k, v in pairs(REDIS_DB_POOL) do
- destory_redis_pool(k)
- end
- REDIS_DB_POOL = {}
- end
- function response.init(name, cf)
- logger.info("init %s", name)
- if REDIS_CONFIG[name] then
- logger.warn("name %s already init", cf)
- return
- end
- init_redis_pool(name, cf)
- end
- function response.acquire(name)
- local cf = REDIS_CONFIG[name]
- if not REDIS_CONFIG[name] then
- logger.warn("sup acquire not %s config", name)
- return
- end
- local pool
- while true do
- pool = REDIS_DB_POOL[name]
- if not pool or #pool < #cf then
- skynet.sleep(100)
- else
- break
- end
- end
- if #pool == 0 then
- logger.error("sup response.acquire pool is emtpy")
- end
- return pool
- end
|