--[[ Descripttion:数据库链接管理 version: Author: Neo,Huang Date: 2022-07-04 14:49:56 LastEditors: Neo,Huang LastEditTime: 2022-07-04 15:26:04 --]] local skynet = require "skynet" local snax = require "snax" local lib_logger = require("log.lib_logger") local logger local MYSQL_DB_POOL = {} local MYSQL_CONFIG = {} local function init_mysql_pool(name, cf) local pool = {} MYSQL_CONFIG[name] = cf for i = 1, 5 do local db = skynet.newservice("srvMysql", cjson_encode(cf), 4) pool[i] = db end MYSQL_DB_POOL[name] = pool end local function destory_mysql_pool(name) local pool = MYSQL_DB_POOL[name] for i = 1, #pool do local db = pool[i] snax.kill(db) end pool = {} end function init() logger = lib_logger.get_logger("mysqlMgr") end function exit() for k, v in pairs(MYSQL_DB_POOL) do destory_mysql_pool(k) end MYSQL_DB_POOL = {} end function response.acquire(name) local cf = MYSQL_CONFIG[name] if not cf then logger.warn("not %s config mysql ", name) return end local pool = MYSQL_DB_POOL[name] while true do 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 function response.init(name, cf) logger.info("init %s", tostring(name)) if MYSQL_DB_POOL[name] then logger.warn("%s cf mysql already init", name) return end init_mysql_pool(name, cf) end