perl6 IO::Socket::Async 服务器死机,异常:连接被对等方重置

perl6 IO::Socket::Async server dies with the exception: connection reset by peer

这是回显服务器代码:

#!/usr/bin/env perl6
my $port = 3333 ;
say "listen port $port" ;

react {
    my $ids = 0 ;
    whenever IO::Socket::Async.listen('0.0.0.0', $port ) -> $conn {
        my $id = $ids++ ;
        $conn.print( "$id: hello\n") ;
        whenever $conn.Supply.lines -> $line {
            say "$id: $line" ;
            $conn.print( "$id : $line\n") ;
        }
    }
}

这里是客户端代码:

#!/usr/bin/env perl6
my $port = 3333 ;
my $conn = await IO::Socket::Async.connect('localhost', $port );
$conn.print: "{time}\n";

#$conn.Supply.tap(-> $v { print $v });

sleep 1 ;
$conn.close;

当客户端连接后未从服务器检索任何数据,然后关闭连接,服务器因以下错误而终止:

listen port 3333
0: 1524671069
An operation first awaited:
  in block <unit> at ./server2.p6 line 5

Died with the exception:
    connection reset by peer
      in block <unit> at ./server2.p6 line 5

X::AdHoc+{X::Await::Died}: connection reset by peer

如何优雅地捕获网络错误,使服务器更健壮?

如果您想处理 Supply(或任何可等待的,如 Promise)底层 whenever 退出(或 Promise broken),你可以在 whenever 中安装一个 QUIT 处理程序。它的工作方式很像一个异常处理程序,所以它会希望您以某种方式匹配异常,或者只是 default 如果您想将所有退出原因视为 "fine".

whenever $conn.Supply.lines -> $line {
    say "$id: $line" ;
    $conn.print( "$id : $line\n") ;
    QUIT {
        default {
            say "oh no, $_";
        }
    }
}

这将输出"oh no, connection reset by peer"并继续运行。