Skip to content

Instantly share code, notes, and snippets.

@donaldh
Created June 3, 2014 16:15
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save donaldh/090b39bb1f4732ad4b82 to your computer and use it in GitHub Desktop.
Save donaldh/090b39bb1f4732ad4b82 to your computer and use it in GitHub Desktop.
Async IO seems to be a bit racy
my $server = IO::Socket::Async.listen('localhost', 5000).tap(-> $conn {
my $message = '';
$conn.chars_supply.tap(-> $chars {
$message ~= $chars;
if $message.index("\n") -> $end {
$conn.send($message.substr(0, $end) ~ ' back').then({ $conn.close });
}
}, quit => { say $_; });
});
sub client(Str $message) {
my $p = Promise.new;
my $v = $p.vow;
my $client = IO::Socket::Async.connect('localhost', 5000).then(-> $sr {
if $sr.status == Kept {
my $socket = $sr.result;
my @chunks;
$socket.chars_supply.tap(-> $chars { @chunks.push($chars) },
done => {
$socket.close();
$v.keep(@chunks.join());
},
quit => { $v.break($_); });
$socket.send($message ~ "\n").then(-> $wr {
if $wr.status == Broken {
$v.break($wr.cause);
$socket.close();
}
});
}
else {
$v.break($sr.cause);
}
});
$p
}
say 'Starting ...';
for 1..10 {
say await client("hello {$_}");
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment