forked from mailru/tntlua
-
Notifications
You must be signed in to change notification settings - Fork 1
/
signals.lua
68 lines (58 loc) · 1.81 KB
/
signals.lua
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
-- Signals
--
-- From time to time You need to synchronize Your application that
-- are connected to tarantool and run in different hosts.
--
-- You can use signals that are passed through tarantool
--
-- example:
--
-- the first host says:
--
-- signals.send('my-signal', 'hello world')
--
-- the other host can catch the signal usng:
--
-- -- will sleep until signal will be received
-- local result = signals.wait('my-signal')
--
-- -- will sleep until signal will be received or timeout exceeded
-- local result = signals.wait('my-signal', 2)
--
-- Notes:
--
-- 1. signals aren't safe: if producer generates too many signals and
-- there are no consumers to handle the signals some signals can be lost
-- 2. signals is stored in memory (not in tarantool's spaces)
-- 3. don't send 'nil' through signals: the value used as timeout indicator
-- 4. if you send one signal and there are several consumers that are
-- waiting the signal, then only one of them will receive the signal
signals = {
channel = {},
wait = function(name, timeout)
if timeout ~= nil then
timeout = tonumber(timeout)
if timeout < 0 then
timeout = 0
end
end
if signals.channel[ name ] == nil then
signals.channel[ name ] = box.ipc.channel(1)
end
local res
if timeout ~= nil then
res = signals.channel[ name ]:get(timeout)
else
res = signals.channel[ name ]:get()
end
if res ~= nil then
return res
end
end,
send = function(name, value)
if signals.channel[ name ] == nil then
signals.channel[ name ] = box.ipc.channel(1)
end
signals.channel[ name ]:put(value, 0)
end
}