IO::Async 和 Future::Utils 的并发请求的 Perl 问题

Perl Issue with concurrent requests with IO::Async and Future::Utils

我正在尝试使用 IO 循环将并发请求 (5) 发送到主机池 (3),但代码在 3 个请求后停止。我在启动这段代码时得到了帮助,但我现在当然理解了其中的大部分内容。我不明白为什么处理的请求数与我的主机池中的主机数相关联。代码的objective是从给定IP判断路由信息。

use strict;
use warnings;
use Net::OpenSSH;
use IO::Async::Loop;
use Future::Utils 'fmap_concat';

my @hosts = qw(host1 host2 host3);
my @ssh;
my $user = 'myuser';
my $pass = 'mypassword';

foreach my $host (@hosts) {
  my $ssh = Net::OpenSSH->new(host => $host, user => $user, password => $pass, master_opts => [-o => "StrictHostKeyChecking=no"]);
  die "Failed to connect to $host: " . $ssh->error if $ssh->error;
  push @ssh, $ssh;
}

my @ipv4 = (
  'ip1','ip2','ip3','ip4','ip5'
);

my $loop = IO::Async::Loop->new;

my $future = fmap_concat {
  my $ip = shift;
  my $ssh = shift @ssh;
  my $cmd = 'show ip route '.$ip.' | i \*';
  my @remote_cmd = $ssh->make_remote_command($cmd);
  return $loop->run_process(command => \@remote_cmd)
    ->transform(done => sub { [@_] })
    ->on_ready(sub { push @ssh, $ssh });
} generate => sub { return () unless @ssh and @ipv4; shift @ipv4 }, concurrent => scalar @ssh;

my @results = $future->get;

foreach my $result (@results) {
  my ($exit, $stdout) = @$result;
  print $stdout, "\n";
}

这是结果

Connection to host1 closed by remote host.
Connection to host2 closed by remote host.
Connection to host3 closed by remote host.
 * ip1, from host1, 3w0d ago, via GigabitEthernet0/0/0

 * ip2, from host2, 7w0d ago, via GigabitEthernet0/0/0

 * ip3, from host3, 3w0d ago, via GigabitEthernet0/0/1

研究该问题后,我发现 Cisco 等网络设备在处理同一连接上的多个请求时可能会出现问题。因此,代码在每次调用 future 时都会打开一个新连接,而不是使用预先打开的连接池。

use strict;
use warnings;
use Net::OpenSSH;
use IO::Async::Loop;
use Future::Utils 'fmap_concat';

my @hosts = qw(host1 host2 host3);
my @ssh;
my $user = 'myuser';
my $pass = 'mypassword';

my @ipv4 = (
  'ip1','ip2','ip3','ip4','ip5'
);

my $loop = IO::Async::Loop->new;

my $future = fmap_concat {
  my $ip = shift;
  my $host = shift @hosts;
  my $ssh = Net::OpenSSH->new(host => $host, user => $user, password => $pass, master_opts => [-o => "StrictHostKeyChecking=no"]);
    die "Failed to connect to $host: " . $ssh->error if $ssh->error;
  my $cmd = 'show ip route '.$ip.' | i \*|^Routing entry';
  my @remote_cmd = $ssh->make_remote_command($cmd);
  return $loop->run_process(command => \@remote_cmd)
  ->transform(done => sub { [@_] })
  ->on_ready(sub { push @hosts, $host ; });
} generate => sub { return () unless @hosts and @ipv4; shift @ipv4 }, concurrent => scalar @hosts;

my @results = $future->get;

foreach my $result (@results) {
  my ($exit, $stdout) = @$result;
  print $stdout, "\n";
}

但这导致底层 openssh 库出现其他问题。 当 future 再次在 $host 上调用时,似乎存在竞争条件,ssh 连接未正确释放。 undef $ssh 修复了它

->on_ready(sub { undef $ssh; push @hosts, $host ; });