| 1 | package Net::Curl::Promiser; | ||
| 2 | |||
| 3 | 1 | ▶ | use strict; |
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||
| 4 | 1 | ▶ | use warnings; |
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||
| 5 | |||
| 6 | our $VERSION = '0.07'; | ||
| 7 | |||
| 8 | =encoding utf-8 | ||
| 9 | |||
| 10 | =head1 NAME | ||
| 11 | |||
| 12 | Net::Curl::Promiser - A Promise interface for L<Net::Curl::Multi> | ||
| 13 | |||
| 14 | =head1 DESCRIPTION | ||
| 15 | |||
| 16 | This module wraps L<Net::Curl::Multi> to facilitate asynchronous | ||
| 17 | requests with Promise objects. Net::Curl::Promiser interfaces with | ||
| 18 | Net::Curl::Multi’s polling callbacks to simplify your task of coordinating | ||
| 19 | multiple concurrent requests. | ||
| 20 | |||
| 21 | L<Net::Curl::Promiser> itself is a base class; you’ll need to provide | ||
| 22 | an interface to whatever event loop you use. See L</SUBCLASS INTERFACE> | ||
| 23 | below. | ||
| 24 | |||
| 25 | This distribution provides the following as both demonstrations and | ||
| 26 | portable implementations: | ||
| 27 | |||
| 28 | =over | ||
| 29 | |||
| 30 | =item * L<Net::Curl::Promiser::Mojo> (for L<Mojolicious>) | ||
| 31 | |||
| 32 | =item * L<Net::Curl::Promiser::AnyEvent> (for L<AnyEvent>) | ||
| 33 | |||
| 34 | =item * L<Net::Curl::Promiser::IOAsync> (for L<IO::Async>) | ||
| 35 | |||
| 36 | =item * L<Net::Curl::Promiser::Select> (for manually-written | ||
| 37 | C<select()> loops) | ||
| 38 | |||
| 39 | =back | ||
| 40 | |||
| 41 | (See the distribution’s F</examples> directory for one based on Linux’s | ||
| 42 | C<epoll>.) | ||
| 43 | |||
| 44 | =head1 PROMISE IMPLEMENTATION | ||
| 45 | |||
| 46 | This class’s default Promise implementation is L<Promise::ES6>. | ||
| 47 | You can use a different one by overriding the C<PROMISE_CLASS()> method in | ||
| 48 | a subclass, as long as the substitute class’s C<new()> method works the | ||
| 49 | same way as Promise::ES6’s (which itself follows the ECMAScript standard). | ||
| 50 | |||
| 51 | (NB: L<Net::Curl::Promiser::Mojo> uses L<Mojo::Promise> instead of | ||
| 52 | Promise::ES6.) | ||
| 53 | |||
| 54 | =head2 B<Experimental> L<Promise::XS> support | ||
| 55 | |||
| 56 | Try out experimental Promise::XS support by running with | ||
| 57 | C<NET_CURL_PROMISER_PROMISE_ENGINE=Promise::XS> in your environment. | ||
| 58 | This will override C<PROMISE_CLASS()>. | ||
| 59 | |||
| 60 | =cut | ||
| 61 | |||
| 62 | #---------------------------------------------------------------------- | ||
| 63 | |||
| 64 | 1 | ▶ | use Net::Curl::Multi (); |
|
Subroutine
BEGIN: called CC=1 SCAR=2.6
| |||
| 65 | |||
| 66 | use constant _DEBUG => 1; | ||
| 67 | |||
| 68 | use constant _DEFAULT_TIMEOUT => 1000; | ||
| 69 | |||
| 70 | use constant PROMISE_CLASS => 'Promise::ES6'; | ||
| 71 | |||
| 72 | #---------------------------------------------------------------------- | ||
| 73 | |||
| 74 | =head1 GENERAL-USE METHODS | ||
| 75 | |||
| 76 | The following are of interest to any code that uses this module: | ||
| 77 | |||
| 78 | =head2 I<CLASS>->new(@ARGS) | ||
| 79 | |||
| 80 | Instantiates this class. This creates an underlying | ||
| 81 | L<Net::Curl::Multi> object and calls the subclass’s C<_INIT()> | ||
| 82 | method at the end, passing a reference to @ARGS. | ||
| 83 | |||
| 84 | (Most end classes of this module do not require @ARGS.) | ||
| 85 | |||
| 86 | =cut | ||
| 87 | |||
| 88 | sub new { | ||
| 89 | my ($class, @args) = @_; | ||
| 90 | |||
| 91 | my %props = ( | ||
| 92 | callbacks => {}, | ||
| 93 | to_fail => {}, | ||
| 94 | ); | ||
| 95 | |||
| 96 | my $self = bless \%props, $class; | ||
| 97 | |||
| 98 | my $multi = Net::Curl::Multi->new(); | ||
| 99 | $self->{'multi'} = $multi; | ||
| 100 | |||
| 101 | $multi->setopt( | ||
| 102 | Net::Curl::Multi::CURLMOPT_SOCKETDATA, | ||
| 103 | $self, | ||
| 104 | ); | ||
| 105 | |||
| 106 | $multi->setopt( | ||
| 107 | Net::Curl::Multi::CURLMOPT_SOCKETFUNCTION, | ||
| 108 | \&_socket_fn, | ||
| 109 | ); | ||
| 110 | |||
| 111 | $self->_INIT(\@args); | ||
| 112 | |||
| 113 | return $self; | ||
| 114 | } | ||
| 115 | |||
| 116 | #---------------------------------------------------------------------- | ||
| 117 | |||
| 118 | =head2 promise($EASY) = I<OBJ>->add_handle( $EASY ) | ||
| 119 | |||
| 120 | A passthrough to the underlying L<Net::Curl::Multi> object’s | ||
| 121 | method of the same name, but the return is given as a Promise object. | ||
| 122 | |||
| 123 | That promise resolves with the passed-in $EASY object. | ||
| 124 | It rejects with either the error given to C<fail_handle()> or the | ||
| 125 | error that L<Net::Curl::Multi> object’s C<info_read()> returns. | ||
| 126 | |||
| 127 | B<IMPORTANT:> As with libcurl itself, HTTP-level failures | ||
| 128 | (e.g., 4xx and 5xx responses) are B<NOT> considered failures at this level. | ||
| 129 | |||
| 130 | =cut | ||
| 131 | |||
| 132 | sub add_handle { | ||
| 133 | my ($self, $easy) = @_; | ||
| 134 | |||
| 135 | $self->{'multi'}->add_handle($easy); | ||
| 136 | |||
| 137 | my $env_engine = $ENV{'NET_CURL_PROMISER_PROMISE_ENGINE'} || q<>; | ||
| 138 | |||
| 139 | my $promise; | ||
| 140 | |||
| 141 | if ($env_engine eq 'Promise::XS') { | ||
| 142 | require Promise::XS; | ||
| 143 | |||
| 144 | my $deferred = Promise::XS::deferred(); | ||
| 145 | $self->{'deferred'}{$easy} = $deferred; | ||
| 146 | $promise = $deferred->promise(); | ||
| 147 | } | ||
| 148 | elsif ($env_engine) { | ||
| 149 | die "bad promise engine: [$env_engine]"; | ||
| 150 | } | ||
| 151 | else { | ||
| 152 | my $module = $self->PROMISE_CLASS() . '.pm'; | ||
| 153 | $module =~ s<::></>g; | ||
| 154 | require $module; | ||
| 155 | |||
| 156 | $promise = $self->PROMISE_CLASS()->new( sub { | ||
| 157 | $self->{'callbacks'}{$easy} = \@_; | ||
| 158 | } ); | ||
| 159 | } | ||
| 160 | |||
| 161 | return $promise; | ||
| 162 | } | ||
| 163 | |||
| 164 | =head2 $obj = I<OBJ>->fail_handle( $EASY, $REASON ) | ||
| 165 | |||
| 166 | Prematurely fails $EASY. The given $REASON will be the associated | ||
| 167 | Promise object’s rejection value. | ||
| 168 | |||
| 169 | =cut | ||
| 170 | |||
| 171 | sub fail_handle { | ||
| 172 | my ($self, $easy, $reason) = @_; | ||
| 173 | |||
| 174 | $self->{'to_fail'}{$easy} = [ $easy, $reason ]; | ||
| 175 | |||
| 176 | return $self; | ||
| 177 | } | ||
| 178 | |||
| 179 | #---------------------------------------------------------------------- | ||
| 180 | |||
| 181 | =head2 $obj = I<OBJ>->setopt( … ) | ||
| 182 | |||
| 183 | A passthrough to the underlying L<Net::Curl::Multi> object’s | ||
| 184 | method of the same name. Returns I<OBJ> to facilitate chaining. | ||
| 185 | |||
| 186 | C<CURLMOPT_SOCKETFUNCTION> or C<CURLMOPT_SOCKETDATA> are set internally; | ||
| 187 | any attempt to set them via this interface will prompt an error. | ||
| 188 | |||
| 189 | =cut | ||
| 190 | |||
| 191 | sub setopt { | ||
| 192 | my $self = shift; | ||
| 193 | |||
| 194 | for my $opt ( qw( SOCKETFUNCTION SOCKETDATA ) ) { | ||
| 195 | my $fullopt = "CURLMOPT_$opt"; | ||
| 196 | |||
| 197 | if ($_[0] == Net::Curl::Multi->can($fullopt)->()) { | ||
| 198 | my $ref = ref $self; | ||
| 199 | die "Don’t set $fullopt via $ref!"; | ||
| 200 | } | ||
| 201 | } | ||
| 202 | |||
| 203 | $self->{'multi'}->setopt(@_); | ||
| 204 | return $self; | ||
| 205 | } | ||
| 206 | |||
| 207 | =head2 $obj = I<OBJ>->handles( … ) | ||
| 208 | |||
| 209 | A passthrough to the underlying L<Net::Curl::Multi> object’s | ||
| 210 | method of the same name. | ||
| 211 | |||
| 212 | =cut | ||
| 213 | |||
| 214 | sub handles { | ||
| 215 | return shift()->{'multi'}->handles(); | ||
| 216 | } | ||
| 217 | |||
| 218 | #---------------------------------------------------------------------- | ||
| 219 | |||
| 220 | =head1 EVENT LOOP METHODS | ||
| 221 | |||
| 222 | The following are needed only when you’re managing an event loop directly: | ||
| 223 | |||
| 224 | =head2 $num = I<OBJ>->get_timeout() | ||
| 225 | |||
| 226 | Returns the underlying L<Net::Curl::Multi> object’s C<timeout()> | ||
| 227 | value, with a suitable (positive) default substituted if that value is | ||
| 228 | less than 0. | ||
| 229 | |||
| 230 | (NB: This value is in I<milliseconds>.) | ||
| 231 | |||
| 232 | This may not suit your needs; if you wish/need, you can handle timeouts | ||
| 233 | via the L<CURLMOPT_TIMERFUNCTION|Net::Curl::Multi/CURLMOPT_TIMERFUNCTION> | ||
| 234 | callback instead. | ||
| 235 | |||
| 236 | This should only be called (if it’s called at all) from event loop logic. | ||
| 237 | |||
| 238 | =cut | ||
| 239 | |||
| 240 | sub get_timeout { | ||
| 241 | my ($self) = @_; | ||
| 242 | |||
| 243 | my $timeout = $self->{'multi'}->timeout(); | ||
| 244 | |||
| 245 | return( $timeout < 0 ? _DEFAULT_TIMEOUT() : $timeout ); | ||
| 246 | } | ||
| 247 | |||
| 248 | #---------------------------------------------------------------------- | ||
| 249 | |||
| 250 | =head2 $obj = I<OBJ>->process( @ARGS ) | ||
| 251 | |||
| 252 | Tell the underlying L<Net::Curl::Multi> object which socket events have | ||
| 253 | happened. | ||
| 254 | |||
| 255 | If, in fact, no events have happened, then this calls | ||
| 256 | C<socket_action(CURL_SOCKET_TIMEOUT)> on the | ||
| 257 | L<Net::Curl::Multi> object (similar to C<time_out()>). | ||
| 258 | |||
| 259 | Finally, this reaps whatever pending HTTP responses may be ready and | ||
| 260 | resolves or rejects the corresponding Promise objects. | ||
| 261 | |||
| 262 | This should only be called from event loop logic. | ||
| 263 | |||
| 264 | Returns I<OBJ>. | ||
| 265 | |||
| 266 | =cut | ||
| 267 | |||
| 268 | sub process { | ||
| 269 | my ($self, @fd_action_args) = @_; | ||
| 270 | |||
| 271 | my $fd_action_hr = $self->_GET_FD_ACTION(\@fd_action_args); | ||
| 272 | |||
| 273 | if (%$fd_action_hr) { | ||
| 274 | for my $fd (keys %$fd_action_hr) { | ||
| 275 | $self->{'multi'}->socket_action( $fd, $fd_action_hr->{$fd} ); | ||
| 276 | } | ||
| 277 | } | ||
| 278 | else { | ||
| 279 | $self->{'multi'}->socket_action( Net::Curl::Multi::CURL_SOCKET_TIMEOUT() ); | ||
| 280 | } | ||
| 281 | |||
| 282 | $self->_process_pending(); | ||
| 283 | |||
| 284 | return $self; | ||
| 285 | } | ||
| 286 | |||
| 287 | #---------------------------------------------------------------------- | ||
| 288 | |||
| 289 | =head2 $is_active = I<OBJ>->time_out(); | ||
| 290 | |||
| 291 | Tell the underlying L<Net::Curl::Multi> object that a timeout happened, | ||
| 292 | and reap whatever pending HTTP responses may be ready. | ||
| 293 | |||
| 294 | Calls C<socket_action(CURL_SOCKET_TIMEOUT)> on the | ||
| 295 | underlying L<Net::Curl::Multi> object. The return is the same as | ||
| 296 | that operation returns. | ||
| 297 | |||
| 298 | Since C<process()> can also do the work of this function, a call to this | ||
| 299 | function is just an optimization. | ||
| 300 | |||
| 301 | This should only be called from event loop logic. | ||
| 302 | |||
| 303 | =cut | ||
| 304 | |||
| 305 | sub time_out { | ||
| 306 | my ($self) = @_; | ||
| 307 | |||
| 308 | my $is_active = $self->{'multi'}->socket_action( Net::Curl::Multi::CURL_SOCKET_TIMEOUT() ); | ||
| 309 | |||
| 310 | $self->_process_pending(); | ||
| 311 | |||
| 312 | return $is_active; | ||
| 313 | } | ||
| 314 | |||
| 315 | #---------------------------------------------------------------------- | ||
| 316 | |||
| 317 | =head1 SUBCLASS INTERFACE | ||
| 318 | |||
| 319 | B<NOTE:> The distribution provides several ready-built end classes; | ||
| 320 | unless you’re managing your own event loop, you don’t need to concern | ||
| 321 | yourself with this. | ||
| 322 | |||
| 323 | To use Net::Curl::Promiser, you’ll need a subclass that defines | ||
| 324 | the following methods: | ||
| 325 | |||
| 326 | =over | ||
| 327 | |||
| 328 | =item * C<_INIT(\@ARGS)>: Called at the end of C<new()>. Receives a reference | ||
| 329 | to the arguments given to C<new()>. | ||
| 330 | |||
| 331 | =item * C<_SET_POLL_IN($FD)>: Tells the event loop that the given file | ||
| 332 | descriptor is ready to read. | ||
| 333 | |||
| 334 | =item * C<_SET_POLL_OUT($FD)>: Like C<_SET_POLL_IN()> but for a write event. | ||
| 335 | |||
| 336 | =item * C<_SET_POLL_INOUT($FD)>: Like C<_SET_POLL_IN()> but registers | ||
| 337 | a read and write event simultaneously. | ||
| 338 | |||
| 339 | =item * C<_STOP_POLL($FD)>: Tells the event loop that the given file | ||
| 340 | descriptor is finished. | ||
| 341 | |||
| 342 | =item * C<_GET_FD_ACTION(\@ARGS)>: Receives a reference to the arguments | ||
| 343 | given to C<process()> and returns a reference to a hash of | ||
| 344 | ( $fd => $event_mask ). $event_mask is the sum of | ||
| 345 | C<Net::Curl::Multi::CURL_CSELECT_IN()> and/or | ||
| 346 | C<Net::Curl::Multi::CURL_CSELECT_OUT()>, depending on which events | ||
| 347 | are available. | ||
| 348 | |||
| 349 | =back | ||
| 350 | |||
| 351 | =cut | ||
| 352 | |||
| 353 | #---------------------------------------------------------------------- | ||
| 354 | |||
| 355 | sub _socket_fn { | ||
| 356 | my ( $fd, $action, $self ) = @_[2, 3, 5]; | ||
| 357 | |||
| 358 | if ($action == Net::Curl::Multi::CURL_POLL_IN) { | ||
| 359 | $self->_SET_POLL_IN($fd); | ||
| 360 | } | ||
| 361 | elsif ($action == Net::Curl::Multi::CURL_POLL_OUT) { | ||
| 362 | $self->_SET_POLL_OUT($fd); | ||
| 363 | } | ||
| 364 | elsif ($action == Net::Curl::Multi::CURL_POLL_INOUT) { | ||
| 365 | $self->_SET_POLL_INOUT($fd); | ||
| 366 | } | ||
| 367 | elsif ($action == Net::Curl::Multi::CURL_POLL_REMOVE) { | ||
| 368 | $self->_STOP_POLL($fd); | ||
| 369 | |||
| 370 | # In case we got a read and a remove right away. | ||
| 371 | # This *may* not be needed but doesn’t seem to hurt. | ||
| 372 | $self->_process_pending(); | ||
| 373 | } | ||
| 374 | else { | ||
| 375 | warn "$self: Unrecognized action $action on FD $fd\n"; | ||
| 376 | } | ||
| 377 | |||
| 378 | return 0; | ||
| 379 | } | ||
| 380 | |||
| 381 | sub _finish_handle { | ||
| 382 | my ($self, $easy, $cb_idx, $payload) = @_; | ||
| 383 | |||
| 384 | my $err = $@; | ||
| 385 | |||
| 386 | # Don’t depend on the caller to report failures. | ||
| 387 | # (AnyEvent, for example, blackholes them.) | ||
| 388 | warn if !eval { | ||
| 389 | delete $self->{'to_fail'}{$easy}; | ||
| 390 | |||
| 391 | if ( my $cb_ar = delete $self->{'callbacks'}{$easy} ) { | ||
| 392 | $cb_ar->[$cb_idx]->($payload); | ||
| 393 | } | ||
| 394 | elsif ( my $deferred = delete $self->{'deferred'}{$easy} ) { | ||
| 395 | if ($cb_idx) { | ||
| 396 | $deferred->reject($payload); | ||
| 397 | } | ||
| 398 | else { | ||
| 399 | $deferred->resolve($payload); | ||
| 400 | } | ||
| 401 | } | ||
| 402 | else { | ||
| 403 | |||
| 404 | # This shouldn’t happen, but just in case: | ||
| 405 | require Data::Dumper; | ||
| 406 | print STDERR Data::Dumper::Dumper( ORPHAN => $easy => $payload ); | ||
| 407 | } | ||
| 408 | |||
| 409 | $self->{'multi'}->remove_handle( $easy ); | ||
| 410 | |||
| 411 | 1; | ||
| 412 | }; | ||
| 413 | |||
| 414 | $@ = $err; | ||
| 415 | |||
| 416 | return; | ||
| 417 | } | ||
| 418 | |||
| 419 | sub _clear_failed { | ||
| 420 | my ($self) = @_; | ||
| 421 | |||
| 422 | for my $val_ar ( values %{ $self->{'to_fail'} } ) { | ||
| 423 | my ($easy, $reason) = @$val_ar; | ||
| 424 | $self->_finish_handle( $easy, 1, $reason ); | ||
| 425 | } | ||
| 426 | |||
| 427 | %{ $self->{'to_fail'} } = (); | ||
| 428 | |||
| 429 | return; | ||
| 430 | } | ||
| 431 | |||
| 432 | sub _process_pending { | ||
| 433 | my ($self) = @_; | ||
| 434 | |||
| 435 | $self->_clear_failed(); | ||
| 436 | |||
| 437 | while ( my ( $msg, $easy, $result ) = $self->{'multi'}->info_read() ) { | ||
| 438 | |||
| 439 | if ($msg != Net::Curl::Multi::CURLMSG_DONE()) { | ||
| 440 | die "Unrecognized info_read() message: [$msg]"; | ||
| 441 | } | ||
| 442 | |||
| 443 | $self->_finish_handle( | ||
| 444 | $easy, | ||
| 445 | ($result == 0) ? ( 0 => $easy ) : ( 1 => $result ), | ||
| 446 | ); | ||
| 447 | } | ||
| 448 | |||
| 449 | return; | ||
| 450 | } | ||
| 451 | |||
| 452 | #---------------------------------------------------------------------- | ||
| 453 | |||
| 454 | =head1 EXAMPLES | ||
| 455 | |||
| 456 | See the distribution’s F</examples> directory. | ||
| 457 | |||
| 458 | =head1 SEE ALSO | ||
| 459 | |||
| 460 | If you use L<AnyEvent>, then L<AnyEvent::XSPromises> with | ||
| 461 | L<AnyEvent::YACurl> may be a nicer fit for you. | ||
| 462 | |||
| 463 | =head1 REPOSITORY | ||
| 464 | |||
| 465 | L<https://github.com/FGasper/p5-Net-Curl-Promiser> | ||
| 466 | |||
| 467 | =head1 LICENSE & COPYRIGHT | ||
| 468 | |||
| 469 | Copyright 2019-2020 Gasper Software Consulting. | ||
| 470 | |||
| 471 | This library is licensed under the same terms as Perl itself. | ||
| 472 | |||
| 473 | =cut | ||
| 474 | |||
| 475 | 1; | ||