lua-zmq icon indicating copy to clipboard operation
lua-zmq copied to clipboard

poller remove

Open moteus opened this issue 11 years ago • 0 comments

ZMQ_Poller.remove create hole and after call poll socket id may change. This is testcase:

zmq    = require "zmq"

local wait do
    local ztimer = require "lzmq.timer" 
    wait = function (ms)
        ztimer.sleep(ms or 100)
    end
end

ctx = zmq.init(1)
pub = ctx:socket(zmq.PUB)
sub1 = ctx:socket(zmq.SUB)
sub2 = ctx:socket(zmq.SUB)
sub3 = ctx:socket(zmq.SUB)
sub1:subscribe("")
sub2:subscribe("")
sub3:subscribe("")

sub1:set_rcvtimeo(100)
sub2:set_rcvtimeo(100)
sub3:set_rcvtimeo(100)

pub:bind("inproc://test.pub")
sub1:connect("inproc://test.pub")
sub2:connect("inproc://test.pub")
sub3:connect("inproc://test.pub")

local names = {
    [sub1] = "sub1";
    [sub2] = "sub2";
    [sub3] = "sub3";
}

local t = {}

poller = zmq.ZMQ_Poller()
t[assert(poller:add(sub1, zmq.POLLIN))] = sub1
t[assert(poller:add(sub2, zmq.POLLIN))] = sub2
t[assert(poller:add(sub3, zmq.POLLIN))] = sub3

wait()

pub:send("hello")

print("poll:", poller:poll(100))
print("-------------------")
while true do
    local id = poller:next_revents_idx()
    if id == -1 then break end
    print(id, t[id], names[t[id]], t[id]:recv())
end
print("===================")

pub:send("hello")

print("remove:", poller:remove(sub2))
print("sub3 id is:", poller:modify(sub3, zmq.POLLIN)) --<<<<< sub3 has id = 2
print("poll:", poller:poll(100))                      --<<<<< internally call `poller_compact_items`
print("sub3 id is:", poller:modify(sub3, zmq.POLLIN)) --<<<<< and now sub3 has id = 1
print("-------------------")
while true do
    local id = poller:next_revents_idx() -- and here we gat id = 1 for sub3
    if id == -1 then break end
    -- assert(sub2 ~= t[id]) -- 
    print(id, t[id], names[t[id]])
end

--[[ example with zmq.poller

poller = require "zmq.poller".new()
 ...
poller:add(sub1, zmq.POLLIN, function(skt) end)
poller:add(sub2, zmq.POLLIN, function(skt) end)
poller:add(sub3, zmq.POLLIN, function(skt) end)

poller:remove(sub2)
poller:modify(sub3, zmq.POLLIN, function(skt)  end)

poller:poll(100) -- we get attempt call nil value (id = 1 now is sub3, but there no callback - we remove it for sub2)
--]]

moteus avatar Jul 01 '13 11:07 moteus