在使用非阻塞请求时尝试限制与服务器的连接数。这个想法是我经常查询同一主机上有很多子页面。我想打开固定数量的连接并重用它们,直到完成所有请求。
在我的示例中,我在同一主机上使用10个子页面。我不想同时获取所有连接,并打开与同一主机的10个连接,我想做5个,一旦连接完成,该连接就可以重用。
我没有找到Mojo :: UserAgent池的任何示例。
#!/usr/bin/env perl
use Mojolicious::Lite;
use Mojo::UserAgent;
use Mojo::IOLoop;
use Data::Dumper;
# Documentation browser under "/perldoc"
plugin 'PODRenderer';
my @url_lst = (
'https://stackoverflow.com/questions/1607904/vim-deleting-from-current-position-until-a-space',
'https://stackoverflow.com/questions/7409134/english-mnemonics-to-vims-shortcuts?noredirect=1&lq=1',
'https://stackoverflow.com/questions/102384/using-vims-tabs-like-buffers?rq=1',
'https://stackoverflow.com/questions/327411/how-do-you-prefer-to-switch-between-buffers-in-vim?noredirect=1&lq=1',
'https://stackoverflow.com/questions/105721/how-do-i-move-to-end-of-line-in-vim?rq=1',
'https://stackoverflow.com/questions/235839/indent-multiple-lines-quickly-in-vi?rq=1',
'https://stackoverflow.com/questions/2332340/indenting-a-bunch-of-lines-in-vim?noredirect=1&lq=1',
'https://stackoverflow.com/questions/657447/vim-clear-last-search-highlighting?rq=1',
'https://stackoverflow.com/questions/14863315/highlight-searches-in-vim-but-not-substitutions?noredirect=1&lq=1',
'https://stackoverflow.com/questions/1497958/how-do-i-use-vim-registers?rq=1',
);
my @req_lst;
my $ua = Mojo::UserAgent->new;
Mojo::IOLoop->singleton->recurring(5 => sub {
my $j = 0;
#foreach my $j (0..2) {
foreach my $i (0..4) {
$req_lst[$i] = $ua->get_p($url_lst[$i+($j*5)]);
print Dumper($url_lst[$i+($j*5)]);
}
Mojo::Promise->all($req_lst[0], $req_lst[1], $req_lst[2], $req_lst[3], $req_lst[4])->then(sub {
my (@lst) = @_;
#print Dumper(@lst);
})->wait;
#}
}
);
get '/' => sub {
my $c = shift;
$c->render(template => 'index');
};
app->start;
__DATA__
@@ index.html.ep
% layout 'default';
% title 'Welcome';
<h1>Welcome to the Mojolicious real-time web framework!</h1>
To learn more, you can browse through the documentation
<%= link_to 'here' => '/perldoc' %>.
@@ layouts/default.html.ep
<!DOCTYPE html>
<html>
<head><title><%= title %></title></head>
<body><%= content %></body>
</html>
之所以这样做,是因为我每秒访问服务器很多次以进行监视。该服务器一次仅支持五个打开的连接,其余的则断开。由于连接经过防火墙,因此每次与服务器建立新连接时,都会对其进行记录。通过仅打开五个连接并重用它们,将大大限制日志记录。