123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101 |
- local skynet = require "skynet"
- require "skynet.manager"
- local setting_template = require "settings"
- local skynet_node_name = ...
- local CMD = {}
- local pool = {}
- local next_id = 0
- local maxconn = 1
- local function next_conn()
- local id = next_id % maxconn + 1
- next_id = next_id + 1
- if id > maxconn then
- id = 1
- end
- return pool[id]
- end
- local function getconn(key)
- if key and (type(key) == "number" or tonumber(key)) then
- local id = math.floor((tonumber(key) - 1) % maxconn) + 1
- return pool[id]
- else
- return next_conn()
- end
- end
- local function call_mongodb_slave(addr, cmd, ...)
- return skynet.call(addr, "lua", cmd, ...)
- end
- local function send_mongodb_slave(addr, cmd, ...)
- skynet.send(addr, "lua", cmd, ...)
- end
- local function start()
- local settings = setting_template.db_cnf[skynet_node_name]
- INFO("mongodbpool 启动", skynet_node_name, inspect(settings))
- maxconn = tonumber(settings.mongodb_maxinst) or 1
- for i = 1, maxconn do
- local mongodb_slave = skynet.newservice("mongodb_slave")
- skynet.call(mongodb_slave, "lua", "start", settings.mongodb_cnf)
- table.insert(pool, mongodb_slave)
- end
- end
- function CMD.find(table_name, cname, ...)
- local executer = getconn()
- return call_mongodb_slave(executer, "find", table_name, cname, ...)
- end
- function CMD.findOne(table_name, cname, conds)
- local executer = getconn()
- return call_mongodb_slave(executer, "findOne", table_name, cname, conds)
- end
- function CMD.aggregate(dbname, cname, operation)
- local executer = getconn()
- return call_mongodb_slave(executer, "aggregate", table_name, cname, operation)
- end
- function CMD.upsert(table_name, cname, datas, conds)
- local executer = getconn()
- return call_mongodb_slave(executer, "update", table_name, cname, conds, datas)
- end
- function CMD.updateMany(table_name, cname, datas, conds)
- local executer = getconn()
- return call_mongodb_slave(executer, "updateMany", table_name, cname, conds, datas)
- end
- function CMD.insert(table_name, cname, datas)
- local executer = getconn()
- return call_mongodb_slave(executer, "insert", table_name, cname, datas)
- end
- function CMD.batch_insert(table_name, cname, datas)
- local executer = getconn()
- return call_mongodb_slave(executer, "batch_insert", table_name, cname, datas)
- end
- function CMD.del(table_name, cname, conds)
- local executer = getconn()
- return call_mongodb_slave(executer, "del", table_name, cname, conds)
- end
- skynet.start(function()
- start()
- skynet.dispatch("lua", function(_, _, cmd, ...)
- local f = assert(CMD[cmd], cmd .. " not found")
- skynet.retpack(f(...))
- end)
- skynet.register('.' .. SERVICE_NAME)
- end)
|