【问题标题】:Perl Issue with concurrent requests with IO::Async and Future::Utils使用 IO::Async 和 Future::Utils 并发请求的 Perl 问题
【发布时间】:2020-01-09 19:44:02
【问题描述】:

我正在尝试使用 IO 循环将并发请求 (5) 发送到主机池 (3),但代码在 3 个请求后停止。我在启动这段代码方面得到了帮助,但我现在当然理解了其中的大部分内容。我不明白为什么处理的请求数与我的主机池中的主机数相关联。该代码的目标是确定来自给定 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

【问题讨论】:

  • 我似乎无法重现这个问题,我什至尝试随机睡眠来改变期货返回的时间,但它总是安排最后两个一旦有空位......你使用的是最新的Future::Utils 的版本?
  • 如果出现奇怪的排序问题,可以尝试一下:用 ->followed_by(sub { my $f = shift; push @ssh, $ssh; return $f }); 替换 on_ready 处理程序 - 这将排序一个新的未来(并为其结果使用相同的未来),所以它会确保在未来序列准备好之前将$ssh 推回数组。
  • 我刚刚将 Future::Utils 从 0.42 -> 0.43 更新,但条件仍然存在
  • 我尝试用 follow_by 替换 on_ready ,但行为是一样的
  • 或许可以试试metacpan.org/pod/Net::OpenSSH#DEBUGGING中提到的调试,被远程关闭的三个连接是可疑的。

标签: perl asynchronous future


【解决方案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 库的其他问题。 当再次在 $host 上调用 future 时,似乎存在 ssh 连接未正确释放的竞争条件。 undef $ssh 修复了它

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

【讨论】:

  • 我认为你跳过了上下文(在 IRC 上提到)你现在为每个命令打开一个新的 SSH 连接。
  • 感谢@Grinnz 我编辑了帖子以反映我们的讨论
猜你喜欢
  • 2020-12-02
  • 2019-01-03
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2011-12-21
  • 1970-01-01
相关资源
最近更新 更多