Permalink
Browse files

handle EWOULDBLOCK by retrying the operation when the socket is ready…

… again
  • Loading branch information...
1 parent c4efc54 commit 5582baa33f5ad4e946ea7d2f6ee1929a2a41bbda @jrockway committed Jan 18, 2011
Showing with 21 additions and 3 deletions.
  1. +21 −3 lib/AnyEvent/ZeroMQ/Handle.pm
@@ -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;
@@ -132,7 +133,12 @@ sub _read_once {
$cb->($self, $msg->data);
}
catch {
- $self->handle_error($_);
+ if($! == EWOULDBLOCK || $! == EAGAIN){
+ return;
+ }
+ else {
+ $self->handle_error($_);
+ }
};
}
@@ -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($_);
+ }
}
}

0 comments on commit 5582baa

Please sign in to comment.