logserver.lua 1.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051
  1. local skynet = require "skynet"
  2. require "skynet.manager"
  3. local logger = require "logger"
  4. local cluster = require "skynet.cluster"
  5. local util = require "util"
  6. local skynet_pack = skynet.pack
  7. local skynet_redirect = skynet.redirect
  8. skynet.start(function()
  9. logger.info("Logserver start")
  10. local daemon = skynet.getenv("daemon")
  11. if not daemon then
  12. skynet.newservice("console")
  13. end
  14. skynet.newservice("debug_console",
  15. "0.0.0.0",
  16. config.debug_console)
  17. assert(config.slave)
  18. local slave = {}
  19. for i=1, #config.slave do
  20. local addr = skynet.newservice("logslave", i)
  21. skynet.call(addr, "lua", "start", config.slave[i])
  22. table.insert(slave, addr)
  23. end
  24. assert(#slave > 0)
  25. local thread = #slave
  26. local os_date = os.date
  27. local os_time = os.time
  28. local string_format = string.format
  29. local hashcode = util.hashcode
  30. skynet.dispatch("lua", function(session, source, cmd, sid, uid, ...)
  31. local ti = os_date("%Y-%m-%d", os_time())
  32. local key = string_format("%s:%s:%s", sid, ti, uid)
  33. local msg, sz = skynet_pack(cmd, key, ...)
  34. local balance = hashcode(sid) % thread
  35. if balance == 0 then
  36. balance = thread
  37. end
  38. skynet_redirect(slave[balance], source, "lua", session, msg, sz)
  39. end)
  40. cluster.reload(config.cluster)
  41. cluster.register("logserver", skynet.self())
  42. cluster.open "log"
  43. end)