1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
|
local channel = require 'bee.channel'
local select = require 'bee.select'
local thread = require 'bee.thread'
local function channel_init(chan)
local selector = select.create()
selector:event_add(chan:fd(), select.SELECT_READ)
return { selector, chan }
end
local function channel_bpop(ctx)
local selector, chan = ctx[1], ctx[2]
while true do
for _ in selector:wait() do
local r = table.pack(chan:pop())
if r[1] == true then
return table.unpack(r, 2)
end
end
thread.sleep(10)
end
end
local taskPad = channel_init(channel.query('taskpad'))
local waiter = channel.query('waiter')
---@class pub_brave
local m = {}
m.type = 'brave'
m.ability = {}
m.queue = {}
--- 注册成为勇者
function m.register(id, privatePad)
m.id = id
if #m.queue > 0 then
for _, info in ipairs(m.queue) do
waiter:push(m.id, info.name, info.params)
end
end
m.queue = nil
m.start(privatePad)
end
--- 注册能力
function m.on(name, callback)
m.ability[name] = callback
end
--- 报告
function m.push(name, params)
if m.id then
waiter:push(m.id, name, params)
else
m.queue[#m.queue+1] = {
name = name,
params = params,
}
end
end
--- 开始找工作
function m.start(privatePad)
local reqPad = privatePad and channel_init(channel.query('req:' .. privatePad)) or taskPad
local resPad = privatePad and channel.query('res:' .. privatePad) or waiter
m.push('mem', collectgarbage 'count')
while true do
local name, id, params = channel_bpop(reqPad)
local ability = m.ability[name]
-- TODO
if not ability then
resPad:push(m.id, id)
log.error('Brave can not handle this work: ' .. name)
goto CONTINUE
end
local ok, res = xpcall(ability, log.error, params)
if ok then
resPad:push(m.id, id, res)
else
resPad:push(m.id, id)
end
m.push('mem', collectgarbage 'count')
::CONTINUE::
end
end
return m
|