Skip to content

Commit

Permalink
handle EWOULDBLOCK by retrying the operation when the socket is ready…
Browse files Browse the repository at this point in the history
… again
  • Loading branch information
jrockway committed Jan 18, 2011
1 parent c4efc54 commit 5582baa
Showing 1 changed file with 21 additions and 3 deletions.
24 changes: 21 additions & 3 deletions lib/AnyEvent/ZeroMQ/Handle.pm
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ use ZeroMQ::Raw::Constants qw(ZMQ_NOBLOCK ZMQ_IDENTITY);
use Params::Util qw(_CODELIKE);
use Scalar::Util qw(weaken);
use Try::Tiny;
use POSIX qw(EAGAIN EWOULDBLOCK);

use true;
use namespace::autoclean;
Expand Down Expand Up @@ -132,7 +133,12 @@ sub _read_once {
$cb->($self, $msg->data);
}
catch {
$self->handle_error($_);
if($! == EWOULDBLOCK || $! == EAGAIN){
return;
}
else {
$self->handle_error($_);
}
};
}

Expand Down Expand Up @@ -197,12 +203,24 @@ sub write {
my $wrote_something = 0;
while($self->writable && $self->has_write_todo){
$wrote_something++;
my $buf;
try {
my $msg = $self->build_message(shift @{$self->write_buffer});
$buf = shift @{$self->write_buffer};
my $msg = $self->build_message($buf);
$self->socket->send($msg, ZMQ_NOBLOCK) if $msg;
}
catch {
$self->handle_error($_);
if($! == EWOULDBLOCK || $! == EAGAIN){
# the got_to_send ensures the string is generated by
# zmq and not by a dying write callback. if you
# supplied a callback that died with "would block",
# then it would be executed again and again and your
# program would lock up. bad.
unshift @{$self->write_buffer}, $buf if defined $buf;
}
else {
$self->handle_error($_);
}
}
}

Expand Down

0 comments on commit 5582baa

Please sign in to comment.