Net-Async-HTTP
view release on metacpan or search on metacpan
use IO::Async::Loop;
use Net::Async::HTTP;
use URI;
my $loop = IO::Async::Loop->new();
my $http = Net::Async::HTTP->new();
$loop->add( $http );
my $response = await $http->do_request(
uri => URI->new( "http://www.cpan.org/" ),
);
print "Front page of http://www.cpan.org/ is:\n";
print $response->as_string;
DESCRIPTION
This object class implements an asynchronous HTTP user agent. It sends
requests to servers, returning Future instances to yield responses when
they are received. The object supports multiple concurrent connections
to servers, and allows multiple requests in the pipeline to any one
connection. Normally, only one such object will be needed per program
to support any number of requests.
As well as using futures the module also supports a callback-based
interface.
This module optionally supports SSL connections, if IO::Async::SSL is
installed. If so, SSL can be requested either by passing a URI with the
https scheme, or by passing a true value as the SSL parameter.
Connection Pooling
There are three ways in which connections to HTTP server hosts are
managed by this object, controlled by the value of
max_connections_per_host. This controls when new connections are
established to servers, as compared to waiting for existing connections
to be free, as new requests are made to them.
They are:
max_connections_per_host = 1
This is the default setting. In this mode, there will be one
connection per host on which there are active or pending requests. If
new requests are made while an existing one is outstanding, they will
be queued to wait for it.
If pipelining is active on the connection (because both the pipeline
option is true and the connection is known to be an HTTP/1.1 server),
then requests will be pipelined into the connection awaiting their
response. If not, they will be queued awaiting a response to the
previous before sending the next.
max_connections_per_host > 1
In this mode, there can be more than one connection per host. If a
new request is made, it will try to re-use idle connections if there
are any, or if they are all busy it will create a new connection to
the host, up to the configured limit.
max_connections_per_host = 0
In this mode, there is no upper limit to the number of connections
per host. Every new request will try to reuse an idle connection, or
else create a new one if all the existing ones are busy.
These modes all apply per hostname / server port pair; they do not
affect the behaviour of connections made to differing hostnames, or
differing ports on the same hostname.
PARAMETERS
The following named parameters may be passed to new or configure:
user_agent => STRING
A string to set in the User-Agent HTTP header. If not supplied, one
will be constructed that declares Net::Async::HTTP and the version
number.
headers => ARRAY or HASH
Since version 0.45.
A set of extra headers to apply to every outgoing request. May be
specified either as an even-sized array containing key/value pairs, or
a hash.
Individual header values may be added or changed without replacing the
entire set by using the configure method and passing a key called
+headers:
$http->configure( +headers => { One_More => "Key" } );
max_redirects => INT
Optional. How many levels of redirection to follow. If not supplied,
will default to 3. Give 0 to disable redirection entirely.
max_in_flight => INT
Optional. The maximum number of in-flight requests to allow per host
when pipelining is enabled and supported on that host. If more requests
are made over this limit they will be queued internally by the object
and not sent to the server until responses are received. If not
supplied, will default to 4. Give 0 to disable the limit entirely.
max_connections_per_host => INT
Optional. Controls the maximum number of connections per
hostname/server port pair, before requests will be queued awaiting one
to be free. Give 0 to disable the limit entirely. See also the
"Connection Pooling" section documented above.
Currently, if not supplied it will default to 1. However, it has been
found in practice that most programs will raise this limit to something
higher, perhaps 3 or 4. Therefore, a future version of this module may
set a higher value.
To test if your application will handle this correctly, you can set a
different default by setting an environment variable:
$ NET_ASYNC_HTTP_MAXCONNS=3 perl ...
timeout => NUM
( run in 1.068 second using v1.01-cache-2.11-cpan-e1769b4cff6 )