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 is an L<AnyEvent> user, you need to make sure that you use and |
12 |
run a supported event loop. |
13 |
|
14 |
This module tries to make L<LWP|LWP> non-blocking with respect to other |
15 |
coroutines as much as possible, and with whatever means it takes. |
16 |
|
17 |
LWP really tries very hard to be blocking (and relies on a lot of |
18 |
undocumented functionality in IO::Socket), so this module had to be very |
19 |
invasive and must be loaded very early to take the proper effect. |
20 |
|
21 |
Note that the module L<AnyEvent::HTTP> might offer an alternative to the |
22 |
full L<LWP> that is designed to be non-blocking. |
23 |
|
24 |
Here is what it currently does (future versions of LWP might require |
25 |
different tricks): |
26 |
|
27 |
=over 4 |
28 |
|
29 |
=item It loads Coro::Select, overwriting the perl C<select> builtin I<globally>. |
30 |
|
31 |
This is necessary because LWP calls select quite often for timeouts and |
32 |
who-knows-what. |
33 |
|
34 |
Impact: everybody else uses this (slower) version of select, too. It should be quite |
35 |
compatible to perls builtin select, though. |
36 |
|
37 |
=item It overwrites Socket::inet_aton with Coro::Util::inet_aton. |
38 |
|
39 |
This is necessary because LWP might (and does) try to resolve hostnames |
40 |
this way. |
41 |
|
42 |
Impact: some code might not expect coroutine semantics, for example, when |
43 |
you fork you might prefer the blocking variant because other coroutines |
44 |
shouldn't actually run. |
45 |
|
46 |
=item It replaces the base class of Net::HTTP, Net::FTP, Net::NNTP. |
47 |
|
48 |
This is necessary because LWP does not always use select to see whether |
49 |
a filehandle can be read/written without blocking, so the base class |
50 |
C<IO::Socket::INET> needs to be replaced by C<Coro::Socket>. |
51 |
|
52 |
Impact: Coro::Socket is not at all compatible to IO::Socket::INET. While |
53 |
it duplicates some undocumented functionality required by LWP, it does not |
54 |
have all the methods of IO::Socket::INET and might act quite differently |
55 |
in practise. Also, protocols other than the above mentioned will still block, |
56 |
at least some of the time. |
57 |
|
58 |
=back |
59 |
|
60 |
All this likely makes other libraries than just LWP not block, but thats |
61 |
just a side effect you cannot rely on. |
62 |
|
63 |
Increases parallelism is not supported by all libraries, some might cache |
64 |
data globally. |
65 |
|
66 |
=cut |
67 |
|
68 |
package Coro::LWP; |
69 |
|
70 |
use common::sense; |
71 |
|
72 |
BEGIN { |
73 |
# suppress warnings |
74 |
local $^W = 0; |
75 |
require Net::Config; |
76 |
} |
77 |
|
78 |
# do it as early as possible |
79 |
use Coro::Select; |
80 |
|
81 |
# import these so they can grab Socket::inet_aton |
82 |
use AnyEvent::Util (); |
83 |
use AnyEvent::DNS (); |
84 |
|
85 |
use Coro::Util (); |
86 |
use Coro::Socket (); |
87 |
use Coro::AnyEvent (); |
88 |
|
89 |
use Socket (); |
90 |
|
91 |
use IO::Socket::INET (); |
92 |
|
93 |
use Net::HTTP (); |
94 |
use Net::FTP (); |
95 |
use Net::NNTP (); |
96 |
|
97 |
our $VERSION = 5.26; |
98 |
|
99 |
*Socket::inet_aton = \&Coro::Util::inet_aton; |
100 |
|
101 |
for (@Net::HTTP::ISA, @Net::FTP::ISA, @Net::NTTP::ISA) { |
102 |
$_ = Coro::LWP::Socket:: if $_ eq IO::Socket::INET::; |
103 |
} |
104 |
|
105 |
package Coro::LWP::Socket; |
106 |
|
107 |
no warnings; |
108 |
|
109 |
use base Coro::Socket::; |
110 |
|
111 |
sub new { |
112 |
my $self = shift; |
113 |
|
114 |
$self->SUPER::new (@_, partial => 1) |
115 |
} |
116 |
|
117 |
1; |
118 |
|
119 |
=head1 AUTHOR |
120 |
|
121 |
Marc Lehmann <schmorp@schmorp.de> |
122 |
http://home.schmorp.de/ |
123 |
|
124 |
=cut |
125 |
|
126 |
|