add skynet.queue

This commit is contained in:
Cloud Wu
2014-07-09 15:26:34 +08:00
parent 0855bf30d9
commit b5e10b8f9f
6 changed files with 76 additions and 1 deletions

View File

@@ -1,4 +1,5 @@
local skynet = require "skynet"
local queue = require "skynet.queue"
local i = 0
local hello = "hello"
@@ -8,9 +9,27 @@ function response.ping(hello)
return hello
end
-- response.sleep and accept.hello share one lock
local lock
function accept.sleep(queue, n)
if queue then
lock(
function()
print("queue=",queue, n)
skynet.sleep(n)
end)
else
print("queue=",queue, n)
skynet.sleep(n)
end
end
function accept.hello()
lock(function()
i = i + 1
print (i, hello)
end)
end
function response.error()
@@ -19,6 +38,8 @@ end
function init( ... )
print ("ping server start:", ...)
-- init queue
lock = queue()
-- You can return "queue" for queue service mode
-- return "queue"

18
test/testqueue.lua Normal file
View File

@@ -0,0 +1,18 @@
local skynet = require "skynet"
local snax = require "snax"
skynet.start(function()
local ps = snax.uniqueservice ("pingserver", "test queue")
for i=1, 10 do
ps.post.sleep(true,i*10)
ps.post.hello()
end
for i=1, 10 do
ps.post.sleep(false,i*10)
ps.post.hello()
end
skynet.exit()
end)