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