JustinTulloss / zeromq.node

Node.js bindings to the zeromq library
MIT License
1.65k stars 286 forks source link

Timing out a req/rep socket #211

Open nathasm opened 11 years ago

nathasm commented 11 years ago

It looks as though sndtimeo and rcvtimeo socket options are not supported. Is this correct? Or if it does, is there an example of how to set these options properly? What I have tried below doesn't seem to work.

var req = zmq.socket('req');
req.setsockopt(zmq.RCVTIMEO, 1000);
req.setsockopt(zmq.SNDTIMEO, 1000);
var sock = req.connect(endpoint);
sock.send(message);
sock.on('message', ...);
sock.on('error', ...);
sock.on('timeout', ...);

If the endpoint isn't up, I wait indefinitely.

stowns commented 10 years ago

Also trying to find a solution to this issue... perhaps something similar to what was suggested for the python lib https://github.com/zeromq/pyzmq/issues/132

kkoopa commented 10 years ago

That is not how the options are called, so it is evident that it will not work. The options are called ZMQ_RCVTIMEO and ZMQ_SNDTIMEO.

nathasm commented 10 years ago

I also tried the ZMQ_SNDTIMEO and ZMQ_RCVTIMEO options, I also set the ZMQ_LINGER, but to no avail.

stowns commented 10 years ago

the options may be working at the zmq binding level but from what I can see the _flush method in the lib will only emit 'error' messages if there is an exception raised.

kkoopa commented 10 years ago

It is supposed to emit an error on timeout. Problem seems to be that zmq_send and other operations should be blocking and have this behavior unless specified otherwise with a flag. For some reasone they seem not to block in zeromq.node, even though the flag is not set. I wonder if it could be the poller that is confusing it, because it works with libzmq.

lindem commented 10 years ago

Sorry to ask, but is there any workaround for this?

andreyvital commented 10 years ago

Same problem here guys... there's a workaround?

kkoopa commented 10 years ago

No workaraound that I know of. Node.js does not like blocking operations, so everything in zeromq.node is non-blocking. These operations will always return instantly, putting messages on the queue for actual sending whenever possible. If there was some way of deleting queued messages, it might be possible to start a timeout and cancel it upon success and let it run and delete the message and raise an error on timeout.

hems commented 10 years ago

So per say, the only work around so far is to create a timeout on our side, and close the previous connection once the timeout happens, right?

I have implemented this simple timeout on waterfront - which i use to interface with zmq https://github.com/nowhereapp/waterfront/blob/master/lib/connect.js#L90-L118

definitely feels like zeromq should be doing the job, not node.js

:v:

yamsellem commented 9 years ago

Any progress on this concern?

hems commented 9 years ago

@yamsellem my solution isn't perfect, but it will retry to execute req again... have a look.

GMolini commented 8 years ago

What was your solution? The link doesnt work

hems commented 8 years ago

@gumlym sorry man, it's been 1 year now. already dropped zeromq and waterfront. my solution now is totally based on redis and i couldn't be more happy.

GMolini commented 8 years ago

Ok never mind. I am using a workaround that just sets a a timeout whenever i send a message with nodejs (with the req socket), then if i havent received anything i just reset the socket (by closing it and opening it again), so that even if the message is produced after the timeout zeromq discards that message.