Search code examples
perllwp-useragent

How can I get LWP::UserAgent to cache connections?


In a perl script I'm using LWP::UserAgent like this:

my $ua = LWP::UserAgent->new( keep_alive => 10 );

After doing $ua->get a few thousand times to fetch URLs from, say, https://example.com, I would expect $ua->conn_cache to have ten open connections. But, if I use Data::Dumper and do:

print Dumper( $ua->conn_cache );

I see only one connection:

$VAR1 = bless( {
                 'cc_limit_total' => 10,
                 'cc_conns' => [
                                 [
                                   bless( \*Symbol::GEN1, 'LWP::Protocol::https::Socket' ),
                                   'https',
                                   'example.com:443',
                                   1638549871
                                 ]
                               ]
               }, 'LWP::ConnCache' );

Am I doing something wrong, or am I just misinterpreting what the dump of conn_cache is showing me? For what it's worth, the URLs I'm fetching differ only in their query parameters.

Edit: OK, I understand (see the answer below). Thanks. I'll look into things like LWP::Parallel::UserAgent instead.


Solution

  • The most efficient way to handle many sequential requests to the same host is to reuse a single persistent TCP connection to this host - and this is exactly what you see. More connections are needed if requests to the same host are sent in parallel instead sequential or if requests are sent to multiple hosts.

    To check the latter just do requests to example.com and example.org - you'll end up with two entries in the connection cache, one for example.com and one for example.org.