Created
June 3, 2014 16:15
-
-
Save donaldh/090b39bb1f4732ad4b82 to your computer and use it in GitHub Desktop.
Async IO seems to be a bit racy
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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