123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051 |
- local skynet = require "skynet"
- require "skynet.manager"
- local logger = require "logger"
- local cluster = require "skynet.cluster"
- local util = require "util"
- local skynet_pack = skynet.pack
- local skynet_redirect = skynet.redirect
- skynet.start(function()
- logger.info("Logserver start")
- local daemon = skynet.getenv("daemon")
- if not daemon then
- skynet.newservice("console")
- end
- skynet.newservice("debug_console",
- "0.0.0.0",
- config.debug_console)
- assert(config.slave)
- local slave = {}
- for i=1, #config.slave do
- local addr = skynet.newservice("logslave", i)
- skynet.call(addr, "lua", "start", config.slave[i])
- table.insert(slave, addr)
- end
- assert(#slave > 0)
- local thread = #slave
- local os_date = os.date
- local os_time = os.time
- local string_format = string.format
- local hashcode = util.hashcode
- skynet.dispatch("lua", function(session, source, cmd, sid, uid, ...)
- local ti = os_date("%Y-%m-%d", os_time())
- local key = string_format("%s:%s:%s", sid, ti, uid)
- local msg, sz = skynet_pack(cmd, key, ...)
- local balance = hashcode(sid) % thread
- if balance == 0 then
- balance = thread
- end
- skynet_redirect(slave[balance], source, "lua", session, msg, sz)
- end)
- cluster.reload(config.cluster)
- cluster.register("logserver", skynet.self())
- cluster.open "log"
- end)
|