mongodbpool.lua 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101
  1. local skynet = require "skynet"
  2. require "skynet.manager"
  3. local setting_template = require "settings"
  4. local skynet_node_name = ...
  5. local CMD = {}
  6. local pool = {}
  7. local next_id = 0
  8. local maxconn = 1
  9. local function next_conn()
  10. local id = next_id % maxconn + 1
  11. next_id = next_id + 1
  12. if id > maxconn then
  13. id = 1
  14. end
  15. return pool[id]
  16. end
  17. local function getconn(key)
  18. if key and (type(key) == "number" or tonumber(key)) then
  19. local id = math.floor((tonumber(key) - 1) % maxconn) + 1
  20. return pool[id]
  21. else
  22. return next_conn()
  23. end
  24. end
  25. local function call_mongodb_slave(addr, cmd, ...)
  26. return skynet.call(addr, "lua", cmd, ...)
  27. end
  28. local function send_mongodb_slave(addr, cmd, ...)
  29. skynet.send(addr, "lua", cmd, ...)
  30. end
  31. local function start()
  32. local settings = setting_template.db_cnf[skynet_node_name]
  33. INFO("mongodbpool 启动", skynet_node_name, inspect(settings))
  34. maxconn = tonumber(settings.mongodb_maxinst) or 1
  35. for i = 1, maxconn do
  36. local mongodb_slave = skynet.newservice("mongodb_slave")
  37. skynet.call(mongodb_slave, "lua", "start", settings.mongodb_cnf)
  38. table.insert(pool, mongodb_slave)
  39. end
  40. end
  41. function CMD.find(table_name, cname, ...)
  42. local executer = getconn()
  43. return call_mongodb_slave(executer, "find", table_name, cname, ...)
  44. end
  45. function CMD.findOne(table_name, cname, conds)
  46. local executer = getconn()
  47. return call_mongodb_slave(executer, "findOne", table_name, cname, conds)
  48. end
  49. function CMD.aggregate(dbname, cname, operation)
  50. local executer = getconn()
  51. return call_mongodb_slave(executer, "aggregate", table_name, cname, operation)
  52. end
  53. function CMD.upsert(table_name, cname, datas, conds)
  54. local executer = getconn()
  55. return call_mongodb_slave(executer, "update", table_name, cname, conds, datas)
  56. end
  57. function CMD.updateMany(table_name, cname, datas, conds)
  58. local executer = getconn()
  59. return call_mongodb_slave(executer, "updateMany", table_name, cname, conds, datas)
  60. end
  61. function CMD.insert(table_name, cname, datas)
  62. local executer = getconn()
  63. return call_mongodb_slave(executer, "insert", table_name, cname, datas)
  64. end
  65. function CMD.batch_insert(table_name, cname, datas)
  66. local executer = getconn()
  67. return call_mongodb_slave(executer, "batch_insert", table_name, cname, datas)
  68. end
  69. function CMD.del(table_name, cname, conds)
  70. local executer = getconn()
  71. return call_mongodb_slave(executer, "del", table_name, cname, conds)
  72. end
  73. skynet.start(function()
  74. start()
  75. skynet.dispatch("lua", function(_, _, cmd, ...)
  76. local f = assert(CMD[cmd], cmd .. " not found")
  77. skynet.retpack(f(...))
  78. end)
  79. skynet.register('.' .. SERVICE_NAME)
  80. end)