1 |
=head1 NAME |
2 |
|
3 |
Coro::LWP - make LWP non-blocking - as much as possible |
4 |
|
5 |
=head1 SYNOPSIS |
6 |
|
7 |
use Coro::LWP; # afterwards LWP should not block |
8 |
|
9 |
=head1 DESCRIPTION |
10 |
|
11 |
This module tries to make L<LWP|LWP> non-blocking with respect to other |
12 |
coroutines as much as possible, and with whatever means it takes. |
13 |
|
14 |
LWP really tries very hard to be blocking, so this module had to be very |
15 |
invasive and must be loaded very early to take the proper effect. |
16 |
|
17 |
Here is what it currently does (future versions of LWP might require |
18 |
different tricks): |
19 |
|
20 |
=over 4 |
21 |
|
22 |
=item It loads Coro::Select, overwriting the perl C<select> builtin I<globally>. |
23 |
|
24 |
This is necessary because LWP calls select quite often for timeouts and |
25 |
who-knows-what. |
26 |
|
27 |
Impact: everybody else uses this (slower) version of select, too. It should be quite |
28 |
compatible to perls builtin select, though. |
29 |
|
30 |
=item It overwrites Socket::inet_aton with Coro::Util::inet_aton. |
31 |
|
32 |
This is necessary because LWP might (and does) try to resolve hostnames |
33 |
this way. |
34 |
|
35 |
Impact: likely little, the two functions should be pretty equivalent. |
36 |
|
37 |
=item It replaces the base class of Net::HTTP, Net::FTP, Net::NNTP. |
38 |
|
39 |
This is necessary because LWP does not always use select to see wether |
40 |
a filehandle can be read/written without blocking, so the base class |
41 |
C<IO::Socket::INET> needs to be replaced by C<Coro::Socket>. |
42 |
|
43 |
Impact: Coro::Socket is not at all compatible to IO::Socket::INET. While |
44 |
it duplicates some undocumented functionality required by LWP, it does not |
45 |
have all the methods of IO::Socket::INET and might act quite differently |
46 |
in practise. Also, protocols other than the above mentioned will still block, |
47 |
at least some of the time. |
48 |
|
49 |
=back |
50 |
|
51 |
All this likely makes other libraries than just LWP not block, but thats |
52 |
just a side effect you cannot rely on. |
53 |
|
54 |
Increases parallelism is not supported by all libraries, some might cache |
55 |
data globally. |
56 |
|
57 |
=cut |
58 |
|
59 |
package Coro::LWP; |
60 |
|
61 |
use strict; |
62 |
|
63 |
use Coro::Select; |
64 |
use Coro::Util; |
65 |
use Coro::Socket; |
66 |
|
67 |
use Socket; |
68 |
use IO::Socket::INET; |
69 |
|
70 |
use Net::HTTP; |
71 |
use Net::FTP; |
72 |
use Net::NNTP; |
73 |
|
74 |
our $VERSION = '0.9'; |
75 |
|
76 |
*Socket::inet_aton = \&Coro::Util::inet_aton; |
77 |
|
78 |
for (@Net::HTTP::ISA, @Net::FTP::ISA, @Net::NTTP::ISA) { |
79 |
$_ = Coro::LWP::Socket:: if $_ eq IO::Socket::INET::; |
80 |
} |
81 |
|
82 |
package Coro::LWP::Socket; |
83 |
|
84 |
use base Coro::Socket::; |
85 |
|
86 |
sub new { |
87 |
my $self = shift; |
88 |
|
89 |
$self->SUPER::new (@_, partial => 1) |
90 |
} |
91 |
|
92 |
1; |
93 |
|
94 |
=head1 AUTHOR |
95 |
|
96 |
Marc Lehmann <schmorp@schmorp.de> |
97 |
http://home.schmorp.de/ |
98 |
|
99 |
=cut |
100 |
|
101 |
|