line |
stmt |
bran |
cond |
sub |
pod |
time |
code |
1
|
|
|
|
|
|
|
package MR::IProto::Connection::Async; |
2
|
|
|
|
|
|
|
|
3
|
|
|
|
|
|
|
=head1 NAME |
4
|
|
|
|
|
|
|
|
5
|
|
|
|
|
|
|
MR::IProto::Connection::Async - async communication |
6
|
|
|
|
|
|
|
|
7
|
|
|
|
|
|
|
=head1 DESCRIPTION |
8
|
|
|
|
|
|
|
|
9
|
|
|
|
|
|
|
Used to perform asynchronous communication. |
10
|
|
|
|
|
|
|
|
11
|
|
|
|
|
|
|
=cut |
12
|
|
|
|
|
|
|
|
13
|
1
|
|
|
1
|
|
5
|
use Mouse; |
|
1
|
|
|
|
|
3
|
|
|
1
|
|
|
|
|
7
|
|
14
|
|
|
|
|
|
|
extends 'MR::IProto::Connection'; |
15
|
|
|
|
|
|
|
|
16
|
1
|
|
|
1
|
|
7432
|
use AnyEvent::Handle; |
|
1
|
|
|
|
|
65220
|
|
|
1
|
|
|
|
|
657
|
|
17
|
1
|
|
|
1
|
|
17
|
use Scalar::Util qw(weaken); |
|
1
|
|
|
|
|
2
|
|
|
1
|
|
|
|
|
1713
|
|
18
|
|
|
|
|
|
|
|
19
|
|
|
|
|
|
|
has _handle => ( |
20
|
|
|
|
|
|
|
is => 'ro', |
21
|
|
|
|
|
|
|
isa => 'AnyEvent::Handle', |
22
|
|
|
|
|
|
|
predicate => '_has_handle', |
23
|
|
|
|
|
|
|
lazy_build => 1, |
24
|
|
|
|
|
|
|
); |
25
|
|
|
|
|
|
|
|
26
|
|
|
|
|
|
|
has _queue => ( |
27
|
|
|
|
|
|
|
is => 'ro', |
28
|
|
|
|
|
|
|
isa => 'ArrayRef', |
29
|
|
|
|
|
|
|
lazy_build => 1, |
30
|
|
|
|
|
|
|
); |
31
|
|
|
|
|
|
|
|
32
|
|
|
|
|
|
|
has _in_progress => ( |
33
|
|
|
|
|
|
|
is => 'rw', |
34
|
|
|
|
|
|
|
isa => 'Int', |
35
|
|
|
|
|
|
|
default => 0, |
36
|
|
|
|
|
|
|
); |
37
|
|
|
|
|
|
|
|
38
|
|
|
|
|
|
|
has _callbacks => ( |
39
|
|
|
|
|
|
|
is => 'ro', |
40
|
|
|
|
|
|
|
isa => 'HashRef', |
41
|
|
|
|
|
|
|
lazy_build => 1, |
42
|
|
|
|
|
|
|
); |
43
|
|
|
|
|
|
|
|
44
|
|
|
|
|
|
|
has _read_reply => ( |
45
|
|
|
|
|
|
|
is => 'ro', |
46
|
|
|
|
|
|
|
isa => 'CodeRef', |
47
|
|
|
|
|
|
|
lazy_build => 1, |
48
|
|
|
|
|
|
|
); |
49
|
|
|
|
|
|
|
|
50
|
|
|
|
|
|
|
has _no_reply => ( |
51
|
|
|
|
|
|
|
is => 'ro', |
52
|
|
|
|
|
|
|
isa => 'ArrayRef', |
53
|
|
|
|
|
|
|
lazy_build => 1, |
54
|
|
|
|
|
|
|
); |
55
|
|
|
|
|
|
|
|
56
|
|
|
|
|
|
|
has _on_drain => ( |
57
|
|
|
|
|
|
|
is => 'ro', |
58
|
|
|
|
|
|
|
isa => 'CodeRef', |
59
|
|
|
|
|
|
|
lazy_build => 1, |
60
|
|
|
|
|
|
|
); |
61
|
|
|
|
|
|
|
|
62
|
|
|
|
|
|
|
=head1 PUBLIC METHODS |
63
|
|
|
|
|
|
|
|
64
|
|
|
|
|
|
|
=over |
65
|
|
|
|
|
|
|
|
66
|
|
|
|
|
|
|
=item send |
67
|
|
|
|
|
|
|
|
68
|
|
|
|
|
|
|
Enqueue message send. |
69
|
|
|
|
|
|
|
For list of arguments see L. |
70
|
|
|
|
|
|
|
|
71
|
|
|
|
|
|
|
=cut |
72
|
|
|
|
|
|
|
|
73
|
0
|
|
0
|
0
|
0
|
|
sub fh { return $_[0]->_has_handle && $_[0]->_handle } |
74
|
|
|
|
|
|
|
|
75
|
|
|
|
|
|
|
sub send { |
76
|
0
|
|
|
0
|
1
|
|
my $self = shift; |
77
|
0
|
0
|
|
|
|
|
if( $self->_in_progress < $self->max_parallel ) { |
78
|
0
|
|
|
|
|
|
$self->_in_progress( $self->_in_progress + 1 ); |
79
|
0
|
|
|
|
|
|
$self->_send(@_); |
80
|
|
|
|
|
|
|
} |
81
|
|
|
|
|
|
|
else { |
82
|
0
|
|
|
|
|
|
push @{$self->_queue}, [@_]; |
|
0
|
|
|
|
|
|
|
83
|
|
|
|
|
|
|
} |
84
|
0
|
|
|
|
|
|
return; |
85
|
|
|
|
|
|
|
} |
86
|
|
|
|
|
|
|
|
87
|
|
|
|
|
|
|
=item set_timeout( $timeout ) |
88
|
|
|
|
|
|
|
|
89
|
|
|
|
|
|
|
Set timeout value for existing connection. |
90
|
|
|
|
|
|
|
|
91
|
|
|
|
|
|
|
=cut |
92
|
|
|
|
|
|
|
|
93
|
|
|
|
|
|
|
sub set_timeout { |
94
|
0
|
|
|
0
|
1
|
|
my ($self, $timeout) = @_; |
95
|
0
|
0
|
|
|
|
|
$self->_handle->timeout($timeout) if $self->_has_handle(); |
96
|
0
|
|
|
|
|
|
return; |
97
|
|
|
|
|
|
|
} |
98
|
|
|
|
|
|
|
|
99
|
|
|
|
|
|
|
=back |
100
|
|
|
|
|
|
|
|
101
|
|
|
|
|
|
|
=head1 PROTECTED METHODS |
102
|
|
|
|
|
|
|
|
103
|
|
|
|
|
|
|
=over |
104
|
|
|
|
|
|
|
|
105
|
|
|
|
|
|
|
=item _send( $msg, $payload, $callback, $no_reply ) |
106
|
|
|
|
|
|
|
|
107
|
|
|
|
|
|
|
Send message to server. |
108
|
|
|
|
|
|
|
|
109
|
|
|
|
|
|
|
=cut |
110
|
|
|
|
|
|
|
|
111
|
|
|
|
|
|
|
sub _send { |
112
|
0
|
|
|
0
|
|
|
my ($self, $msg, undef, $callback, $no_reply, $sync) = @_; |
113
|
0
|
0
|
|
|
|
|
$sync = $self->_choose_sync() unless defined $sync; |
114
|
0
|
|
|
|
|
|
my $header = $self->_pack_header($msg, length $_[2], $sync); |
115
|
0
|
|
|
|
|
|
my $server = $self->server; |
116
|
0
|
|
|
|
|
|
$self->_callbacks->{$sync} = $callback; |
117
|
0
|
|
|
|
|
|
$server->_send_started($sync, $msg, $_[2]); |
118
|
0
|
|
|
|
|
|
my $handle = $self->_handle; |
119
|
0
|
0
|
|
|
|
|
if( $server->debug >= 5 ) { |
120
|
0
|
|
|
|
|
|
$server->_debug_dump('send header: ', $header); |
121
|
0
|
|
|
|
|
|
$server->_debug_dump('send payload: ', $_[2]); |
122
|
|
|
|
|
|
|
} |
123
|
0
|
|
|
|
|
|
$handle->push_write($header); |
124
|
0
|
|
|
|
|
|
$handle->push_write($_[2]); |
125
|
0
|
0
|
|
|
|
|
if( $no_reply ) { |
126
|
0
|
|
|
|
|
|
push @{$self->_no_reply}, $sync; |
|
0
|
|
|
|
|
|
|
127
|
0
|
0
|
|
|
|
|
$handle->on_drain( $self->_on_drain ) unless defined $handle->{on_drain}; |
128
|
|
|
|
|
|
|
} |
129
|
|
|
|
|
|
|
else { |
130
|
0
|
|
|
|
|
|
$handle->push_read( chunk => 12, $self->_read_reply ); |
131
|
|
|
|
|
|
|
} |
132
|
0
|
|
|
|
|
|
return; |
133
|
|
|
|
|
|
|
} |
134
|
|
|
|
|
|
|
|
135
|
|
|
|
|
|
|
sub _build__read_reply { |
136
|
0
|
|
|
0
|
|
|
my ($self) = @_; |
137
|
0
|
|
|
|
|
|
my $server = $self->server; |
138
|
0
|
|
|
|
|
|
weaken($self); |
139
|
0
|
|
|
|
|
|
weaken($server); |
140
|
|
|
|
|
|
|
return sub { |
141
|
0
|
|
|
0
|
|
|
my ($handle, $data) = @_; |
142
|
0
|
|
|
|
|
|
my $dump_resp = $server->debug >= 6; |
143
|
0
|
0
|
|
|
|
|
$server->_debug_dump('recv header: ', $data) if $dump_resp; |
144
|
0
|
|
|
|
|
|
my ($msg, $payload_length, $sync) = $self->_unpack_header($data); |
145
|
|
|
|
|
|
|
$handle->unshift_read( chunk => $payload_length, sub { |
146
|
0
|
|
|
|
|
|
my ($handle, $data) = @_; |
147
|
0
|
0
|
|
|
|
|
$server->_debug_dump('recv payload: ', $data) if $dump_resp; |
148
|
0
|
|
|
|
|
|
$server->_recv_finished($sync, $msg, $data); |
149
|
0
|
|
|
|
|
|
$self->_finish_and_start(); |
150
|
0
|
|
|
|
|
|
delete($self->_callbacks->{$sync})->($msg, $data); |
151
|
0
|
|
|
|
|
|
return; |
152
|
0
|
|
|
|
|
|
}); |
153
|
0
|
|
|
|
|
|
return; |
154
|
0
|
|
|
|
|
|
}; |
155
|
|
|
|
|
|
|
} |
156
|
|
|
|
|
|
|
|
157
|
|
|
|
|
|
|
sub _try_to_send { |
158
|
0
|
|
|
0
|
|
|
my ($self) = @_; |
159
|
0
|
|
0
|
|
|
|
while( $self->_in_progress < $self->max_parallel && (my $task = shift @{ $self->_queue }) ) { |
|
0
|
|
|
|
|
|
|
160
|
0
|
|
|
|
|
|
$self->_in_progress( $self->_in_progress + 1 ); |
161
|
0
|
|
|
|
|
|
$self->_send(@$task); |
162
|
|
|
|
|
|
|
} |
163
|
0
|
|
|
|
|
|
return; |
164
|
|
|
|
|
|
|
} |
165
|
|
|
|
|
|
|
|
166
|
|
|
|
|
|
|
sub _finish_and_start { |
167
|
0
|
|
|
0
|
|
|
my ($self) = @_; |
168
|
0
|
0
|
|
|
|
|
if( my $task = shift @{$self->_queue} ) { |
|
0
|
|
|
|
|
|
|
169
|
0
|
|
|
|
|
|
$self->_send(@$task); |
170
|
|
|
|
|
|
|
} |
171
|
|
|
|
|
|
|
else { |
172
|
0
|
|
|
|
|
|
$self->_in_progress( $self->_in_progress - 1 ); |
173
|
|
|
|
|
|
|
} |
174
|
0
|
|
|
|
|
|
return; |
175
|
|
|
|
|
|
|
} |
176
|
|
|
|
|
|
|
|
177
|
|
|
|
|
|
|
sub _build__handle { |
178
|
0
|
|
|
0
|
|
|
my ($self) = @_; |
179
|
0
|
|
|
|
|
|
my $server = $self->server; |
180
|
0
|
0
|
|
|
|
|
$server->_debug("connecting") if $server->debug >= 4; |
181
|
0
|
|
|
|
|
|
weaken($self); |
182
|
0
|
|
|
|
|
|
weaken($server); |
183
|
|
|
|
|
|
|
return AnyEvent::Handle->new( |
184
|
|
|
|
|
|
|
connect => [ $self->host, $self->port ], |
185
|
|
|
|
|
|
|
no_delay => $self->tcp_nodelay, |
186
|
|
|
|
|
|
|
keepalive => $self->tcp_keepalive, |
187
|
|
|
|
|
|
|
timeout => $self->timeout, |
188
|
|
|
|
|
|
|
on_prepare => sub { |
189
|
0
|
|
|
0
|
|
|
return $self->connect_timeout; |
190
|
|
|
|
|
|
|
}, |
191
|
|
|
|
|
|
|
on_connect => sub { |
192
|
0
|
|
|
0
|
|
|
my ($handle) = @_; |
193
|
0
|
0
|
|
|
|
|
$server->_debug("connected") if $server->debug >= 1; |
194
|
0
|
|
|
|
|
|
return; |
195
|
|
|
|
|
|
|
}, |
196
|
|
|
|
|
|
|
on_error => sub { |
197
|
0
|
|
|
0
|
|
|
my ($handle, $fatal, $message) = @_; |
198
|
0
|
|
|
|
|
|
my $errno = $!; |
199
|
0
|
0
|
|
|
|
|
$server->_debug(($fatal ? 'fatal ' : '') . 'error: ' . $message); |
200
|
0
|
|
|
|
|
|
my @callbacks; |
201
|
0
|
|
|
|
|
|
foreach my $sync ( keys %{$self->_callbacks} ) { |
|
0
|
|
|
|
|
|
|
202
|
0
|
|
|
|
|
|
$server->_recv_finished($sync, undef, undef, $message, $errno); |
203
|
0
|
|
|
|
|
|
$self->_in_progress( $self->_in_progress - 1 ); |
204
|
0
|
|
|
|
|
|
push @callbacks, $self->_callbacks->{$sync}; |
205
|
|
|
|
|
|
|
} |
206
|
0
|
|
|
|
|
|
$server->active(0); |
207
|
0
|
|
|
|
|
|
$self->_clear_handle(); |
208
|
0
|
|
|
|
|
|
$self->_clear_callbacks(); |
209
|
0
|
|
|
|
|
|
$self->_clear_no_reply(); |
210
|
0
|
0
|
|
|
|
|
$server->_debug('closing socket') if $server->debug >= 1; |
211
|
0
|
|
|
|
|
|
$handle->destroy(); |
212
|
0
|
|
|
|
|
|
$self->_try_to_send(); |
213
|
0
|
|
|
|
|
|
$_->(undef, undef, $message, $errno) foreach @callbacks; |
214
|
0
|
|
|
|
|
|
return; |
215
|
|
|
|
|
|
|
}, |
216
|
|
|
|
|
|
|
on_timeout => sub { |
217
|
0
|
|
|
0
|
|
|
my ($handle) = @_; |
218
|
0
|
0
|
|
|
|
|
return unless keys %{$self->_callbacks}; |
|
0
|
|
|
|
|
|
|
219
|
0
|
0
|
|
|
|
|
$handle->_error( Errno::ETIMEDOUT ) if keys %{$self->_callbacks}; |
|
0
|
|
|
|
|
|
|
220
|
0
|
|
|
|
|
|
return; |
221
|
|
|
|
|
|
|
}, |
222
|
0
|
|
|
|
|
|
); |
223
|
|
|
|
|
|
|
} |
224
|
|
|
|
|
|
|
|
225
|
|
|
|
|
|
|
sub _build__on_drain { |
226
|
0
|
|
|
0
|
|
|
my ($self) = @_; |
227
|
0
|
|
|
|
|
|
my $server = $self->server; |
228
|
0
|
|
|
|
|
|
weaken($self); |
229
|
0
|
|
|
|
|
|
weaken($server); |
230
|
|
|
|
|
|
|
return sub { |
231
|
0
|
|
|
0
|
|
|
my ($handle) = @_; |
232
|
0
|
0
|
|
|
|
|
if( $self->_has_no_reply() ) { |
233
|
0
|
|
|
|
|
|
foreach my $sync ( @{$self->_no_reply} ) { |
|
0
|
|
|
|
|
|
|
234
|
0
|
|
|
|
|
|
$server->_recv_finished($sync, undef, undef); |
235
|
0
|
|
|
|
|
|
$self->_in_progress( $self->_in_progress - 1 ); |
236
|
0
|
|
|
|
|
|
delete($self->_callbacks->{$sync})->(undef, undef); |
237
|
|
|
|
|
|
|
} |
238
|
0
|
|
|
|
|
|
$self->_clear_no_reply(); |
239
|
0
|
|
|
|
|
|
$self->_try_to_send(); |
240
|
0
|
|
|
|
|
|
$handle->on_drain(undef); |
241
|
|
|
|
|
|
|
} |
242
|
0
|
|
|
|
|
|
return; |
243
|
0
|
|
|
|
|
|
}; |
244
|
|
|
|
|
|
|
} |
245
|
|
|
|
|
|
|
|
246
|
|
|
|
|
|
|
sub _build__queue { |
247
|
0
|
|
|
0
|
|
|
my ($self) = @_; |
248
|
0
|
|
|
|
|
|
return []; |
249
|
|
|
|
|
|
|
} |
250
|
|
|
|
|
|
|
|
251
|
|
|
|
|
|
|
sub _build__callbacks { |
252
|
0
|
|
|
0
|
|
|
my ($self) = @_; |
253
|
0
|
|
|
|
|
|
return {}; |
254
|
|
|
|
|
|
|
} |
255
|
|
|
|
|
|
|
|
256
|
|
|
|
|
|
|
sub _build__no_reply { |
257
|
0
|
|
|
0
|
|
|
my ($self) = @_; |
258
|
0
|
|
|
|
|
|
return []; |
259
|
|
|
|
|
|
|
} |
260
|
|
|
|
|
|
|
|
261
|
|
|
|
|
|
|
around _choose_sync => sub { |
262
|
|
|
|
|
|
|
my ($orig, $self) = @_; |
263
|
|
|
|
|
|
|
my $sync; |
264
|
|
|
|
|
|
|
my $callbacks = $self->_callbacks; |
265
|
|
|
|
|
|
|
for( 1 .. 50 ) { |
266
|
|
|
|
|
|
|
$sync = $self->$orig(); |
267
|
|
|
|
|
|
|
return $sync unless exists $callbacks->{$sync}; |
268
|
|
|
|
|
|
|
} |
269
|
|
|
|
|
|
|
die "Can't choose sync value after 50 iterations"; |
270
|
|
|
|
|
|
|
}; |
271
|
|
|
|
|
|
|
|
272
|
0
|
|
|
0
|
0
|
|
sub Close { die "This is not what should be done" } |
273
|
|
|
|
|
|
|
|
274
|
|
|
|
|
|
|
=back |
275
|
|
|
|
|
|
|
|
276
|
|
|
|
|
|
|
=head1 SEE ALSO |
277
|
|
|
|
|
|
|
|
278
|
|
|
|
|
|
|
L, L. |
279
|
|
|
|
|
|
|
|
280
|
|
|
|
|
|
|
=cut |
281
|
|
|
|
|
|
|
|
282
|
1
|
|
|
1
|
|
6
|
no Mouse; |
|
1
|
|
|
|
|
2
|
|
|
1
|
|
|
|
|
10
|
|
283
|
|
|
|
|
|
|
__PACKAGE__->meta->make_immutable(); |
284
|
|
|
|
|
|
|
|
285
|
|
|
|
|
|
|
1; |