Lazy pirate example: wrong polling?
altmind opened this issue · comments
I'm checking an examples for REQ/REP sockets, and trying to make some sense from lazy pirate examples. Take a look at php sample http://zguide.zeromq.org/php:chapter4#toc3 http://zguide.zeromq.org/php:lpclient :
$context = new ZMQContext();
$client = client_socket($context);
$sequence = 0;
$retries_left = REQUEST_RETRIES;
$read = $write = array();
while ($retries_left) {
// We send a request, then we work to get a reply
$client->send(++$sequence);
$expect_reply = true;
while ($expect_reply) {
// Poll socket for a reply, with timeout
$poll = new ZMQPoll();
$poll->add($client, ZMQ::POLL_IN);
$events = $poll->poll($read, $write, REQUEST_TIMEOUT);
// If we got a reply, process it
if ($events > 0) {
// We got a reply from the server, must match sequence
$reply = $client->recv();
if (intval($reply) == $sequence) {
printf ("I: server replied OK (%s)%s", $reply, PHP_EOL);
$retries_left = REQUEST_RETRIES;
$expect_reply = false;
} else {
printf ("E: malformed reply from server: %s%s", $reply, PHP_EOL);
}
} elseif (--$retries_left == 0) {
echo "E: server seems to be offline, abandoning", PHP_EOL;
break;
} else {
echo "W: no response from server, retrying…", PHP_EOL;
// Old socket will be confused; close it and open a new one
$client = client_socket($context);
// Send request again, on new socket
$client->send($sequence);
}
}
}
The block in question is the last else
. My understanding that we can get there only when poll returned 0 pollin events, which can happen when not-final poll try for REQUEST_TIMEOUT and got nothing in the socket(yet). This looks like a normal situation - the poll data have just not returned yet - we should do $retries_left more attempts.
Why do we close the socket and send the message again in the inner while
? sending messages again should only happen when we did $retries_left poll attempts. Re-sending the request is handled in the outer while cycle, its unnecessary to kill the socket after 1st poll without data.