Hi!
I need some help in debugging my script. Sometimes it hangs and i don't
know why.
There's a channel with all the values, and then i create N promises to
take a value from the channel and to write it to a socket. (one producer
with multiple consumers)
I'm attaching two scripts (client.raku and server.raku). The problem is
in the client.raku (42 lines). The server.raku script is just a slightly
modified version of IO::Socket::Async::SSL documentation example.
To reproduce the problem first start the server (raku ./server.raku),
and then run the client script (raku client.raku)
raku --version:
Welcome to Rakudo(tm) v2020.12-5-g3beb71cc9.
Implementing the Raku(tm) programming language v6.d.
Built on MoarVM version 2020.12-7-g6bf54d784.
Thank you,
David Santiago
#!/usr/bin/env perl6
use IO::Socket::Async::SSL;
sub MAIN() {
my $counter = 0;
react {
my %ssl-config =
certificate-file => 'server-crt.pem',
private-key-file => 'server-key.pem';
whenever IO::Socket::Async::SSL.listen('localhost', 4433, |%ssl-config)
-> $conn {
say "Got conn!";
await $conn.print(get_fortune());
#await $conn.print(get_fortune());
whenever $conn -> $line {
say $line;
if $line.contains("SENDING") {
await $conn.print("340 OK {$counter++}\r\n");
} else {
await $conn.print("200 OK\r\n");
#$conn.close;
}
}
}
}
}
sub get_fortune(-->Str) {
return run('fortune', :out).out.slurp(:close)~"200 OK\r\n";
}
multi MAIN() {
my $channel = Channel.new();
$channel.send($_) for ^29;
$channel.close();
my @promises = do for ^4 -> $consumer {
start {
my $p = IO::Socket::Async::SSL.connect('localhost', 4433, insecure
=> True);
my $conn;
# Read the MOTD
await $p.then: -> $prom {
$conn = $prom.result;
react whenever $conn.Supply.lines -> $line {
say "[$consumer] Intro: $line";
done if $line ~~ /^200/;
}
}
# Start consuming
$channel.Supply.tap(-> $val {
await $conn.print("SENDING\r\n").then: -> $promise {
react whenever $conn.Supply -> $line {
print "[$consumer] $line";
if $line ~~ /^340/ {
await $conn.print("[$consumer]: value $val\r\n");
} else {
done;
}
}
}
}, done=>{say "[$consumer] DONE!";});
say "[$consumer] TAP DONE!";
}
}
await Promise.allof(@promises);
say $_.status for @promises;
}