clustersender.lua 2.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384
  1. local skynet = require "skynet"
  2. local sc = require "skynet.socketchannel"
  3. local socket = require "skynet.socket"
  4. local cluster = require "skynet.cluster.core"
  5. local channel
  6. local session = 1
  7. local node, nodename, init_host, init_port = ...
  8. local command = {}
  9. local function send_request(addr, msg, sz)
  10. -- msg is a local pointer, cluster.packrequest will free it
  11. local current_session = session
  12. local request, new_session, padding = cluster.packrequest(addr, session, msg, sz)
  13. session = new_session
  14. local tracetag = skynet.tracetag()
  15. if tracetag then
  16. if tracetag:sub(1,1) ~= "(" then
  17. -- add nodename
  18. local newtag = string.format("(%s-%s-%d)%s", nodename, node, session, tracetag)
  19. skynet.tracelog(tracetag, string.format("session %s", newtag))
  20. tracetag = newtag
  21. end
  22. skynet.tracelog(tracetag, string.format("cluster %s", node))
  23. channel:request(cluster.packtrace(tracetag))
  24. end
  25. return channel:request(request, current_session, padding)
  26. end
  27. function command.req(...)
  28. local ok, msg = pcall(send_request, ...)
  29. if ok then
  30. if type(msg) == "table" then
  31. skynet.ret(cluster.concat(msg))
  32. else
  33. skynet.ret(msg)
  34. end
  35. else
  36. skynet.error(msg)
  37. skynet.response()(false)
  38. end
  39. end
  40. function command.push(addr, msg, sz)
  41. local request, new_session, padding = cluster.packpush(addr, session, msg, sz)
  42. if padding then -- is multi push
  43. session = new_session
  44. end
  45. channel:request(request, nil, padding)
  46. end
  47. local function read_response(sock)
  48. local sz = socket.header(sock:read(2))
  49. local msg = sock:read(sz)
  50. return cluster.unpackresponse(msg) -- session, ok, data, padding
  51. end
  52. function command.changenode(host, port)
  53. if not host then
  54. skynet.error(string.format("Close cluster sender %s:%d", channel.__host, channel.__port))
  55. channel:close()
  56. else
  57. channel:changehost(host, tonumber(port))
  58. channel:connect(true)
  59. end
  60. skynet.ret(skynet.pack(nil))
  61. end
  62. skynet.start(function()
  63. channel = sc.channel {
  64. host = init_host,
  65. port = tonumber(init_port),
  66. response = read_response,
  67. nodelay = true,
  68. }
  69. skynet.dispatch("lua", function(session , source, cmd, ...)
  70. local f = assert(command[cmd])
  71. f(...)
  72. end)
  73. end)