How to disable ZeroMQ socket after timeout?

I successfully used ZeroMQ with PHP and wrote a job server. I have an admin script that checks the server is up and doing something like pinging with a timeout.

Everything works fine when the server is running, but when it is turned off (and ZMQPoll will expire as expected), my script does what it should - BUT

There seems to be no disconnect() socket method, since I can tell PHP that the socket is dead and I don't want it to hang?

This is the code snippet below my admin script -

  // ... // waiting for dead server on zmqsock to respond after sending a message // $poll = new ZMQPoll; $poll->add( $this->zmqsock, ZMQ::POLL_IN ); $readable = $writeable = array(); $poll->poll( $readable, $writeable, $timeout * 1000 ); if( $errors = $poll->getLastErrors() ) { foreach ( $errors as $err ) { throw new Exception($err); } } if( ! $readable ){ // clean up everything, raise errors, etc.. $poll->clear(); unset( $poll, $this->zmqsock, $this->zmqcontext ); // Script hangs here exit(0); } // .. 
+6
source share
1 answer

ZeroMQ will try to send pending messages when it shuts down - you can control this by setting the socket option ZMQ :: SOCKOPT_LINGER (which you must complete before connecting), which should allow you to quickly shut down.

Take a look at the ZMQ_LINGER bit at http://api.zeromq.org/2-1:zmq-setsockopt

+7
source

Source: https://habr.com/ru/post/917613/


All Articles