| 1 | package Net::Curl::Promiser::Backend; | ||
| 2 | |||
| 3 | use strict; | ||
| 4 | use warnings; | ||
| 5 | |||
| 6 | use parent 'Net::Curl::Promiser::LeakDetector'; | ||
| 7 | |||
| 8 | use Net::Curl::Multi (); | ||
| 9 | |||
| 10 | use constant PROMISE_CLASS => 'Promise::ES6'; | ||
| 11 | |||
| 12 | sub new { | ||
| 13 | return bless { | ||
| 14 | ignore_leaks => $Net::Curl::Promiser::IGNORE_MEMORY_LEAKS, | ||
| 15 | to_fail => {}, | ||
| 16 | }, shift; | ||
| 17 | } | ||
| 18 | |||
| 19 | sub INIT_PROMISE {} | ||
| 20 | |||
| 21 | sub cancel_handle { | ||
| 22 | my ($self, $easy, $multi) = @_; | ||
| 23 | |||
| 24 | return $self->_fail_or_cancel($easy, undef, $multi); | ||
| 25 | } | ||
| 26 | |||
| 27 | sub fail_handle { | ||
| 28 | my ($self, $easy, $reason, $multi) = @_; | ||
| 29 | |||
| 30 | return $self->_fail_or_cancel($easy, \$reason, $multi); | ||
| 31 | } | ||
| 32 | |||
| 33 | sub _fail_or_cancel { | ||
| 34 | my ($self, $easy, $reason_sr, $multi) = @_; | ||
| 35 | |||
| 36 | $self->_is_pending($easy) or die "Cannot fail non-pending request!"; | ||
| 37 | |||
| 38 | $self->{'to_fail'}{$easy} = [ $easy, $reason_sr ]; | ||
| 39 | |||
| 40 | $self->_clear_failed($multi); | ||
| 41 | |||
| 42 | return $self; | ||
| 43 | } | ||
| 44 | |||
| 45 | sub add_handle { | ||
| 46 | my ($self, $easy, $multi) = @_; | ||
| 47 | |||
| 48 | if ($self->_is_pending($easy)) { | ||
| 49 | require Carp; | ||
| 50 | Carp::croak("Attempted to re-add in-progress handle"); | ||
| 51 | } | ||
| 52 | |||
| 53 | $multi->add_handle($easy); | ||
| 54 | |||
| 55 | my $env_engine = $ENV{'NET_CURL_PROMISER_PROMISE_ENGINE'} || q<>; | ||
| 56 | |||
| 57 | my $promise; | ||
| 58 | |||
| 59 | if ($env_engine eq 'Promise::XS') { | ||
| 60 | require Promise::XS; | ||
| 61 | |||
| 62 | my $deferred = Promise::XS::deferred(); | ||
| 63 | $self->{'deferred'}{$easy} = $deferred; | ||
| 64 | $promise = $deferred->promise(); | ||
| 65 | } | ||
| 66 | elsif ($env_engine) { | ||
| 67 | die "bad promise engine: [$env_engine]"; | ||
| 68 | } | ||
| 69 | else { | ||
| 70 | $self->PROMISE_CLASS()->can('new') or do { | ||
| 71 | my $class = $self->PROMISE_CLASS(); | ||
| 72 | |||
| 73 | local $@; | ||
| 74 | die if !eval "require $class"; | ||
| 75 | }; | ||
| 76 | |||
| 77 | $promise = $self->PROMISE_CLASS()->new( sub { | ||
| 78 | $self->{'callbacks'}{$easy} = \@_; | ||
| 79 | } ); | ||
| 80 | $self->INIT_PROMISE($promise); | ||
| 81 | } | ||
| 82 | |||
| 83 | return $promise; | ||
| 84 | } | ||
| 85 | |||
| 86 | sub process { | ||
| 87 | my ($self, $multi, $fd_action_args_ar) = @_; | ||
| 88 | |||
| 89 | my $fd_action_hr = $self->_GET_FD_ACTION($fd_action_args_ar); | ||
| 90 | |||
| 91 | if (%$fd_action_hr) { | ||
| 92 | for my $fd (keys %$fd_action_hr) { | ||
| 93 | $multi->socket_action( $fd, $fd_action_hr->{$fd} ); | ||
| 94 | } | ||
| 95 | } | ||
| 96 | else { | ||
| 97 | $multi->socket_action( Net::Curl::Multi::CURL_SOCKET_TIMEOUT() ); | ||
| 98 | } | ||
| 99 | |||
| 100 | $self->process_pending( $multi ); | ||
| 101 | |||
| 102 | return; | ||
| 103 | } | ||
| 104 | |||
| 105 | sub process_pending { | ||
| 106 | my ($self, $multi) = @_; | ||
| 107 | |||
| 108 | $self->_clear_failed($multi); | ||
| 109 | |||
| 110 | while ( my ( $msg, $easy, $result ) = $multi->info_read() ) { | ||
| 111 | |||
| 112 | if ($msg != Net::Curl::Multi::CURLMSG_DONE()) { | ||
| 113 | die "Unrecognized info_read() message: [$msg]"; | ||
| 114 | } | ||
| 115 | |||
| 116 | $self->_finish_handle( | ||
| 117 | $easy, | ||
| 118 | $multi, | ||
| 119 | ($result == 0) ? ( 0 => $easy ) : ( 1 => \$result ), | ||
| 120 | ); | ||
| 121 | } | ||
| 122 | |||
| 123 | return; | ||
| 124 | } | ||
| 125 | |||
| 126 | sub get_timeout { | ||
| 127 | my ($self, $multi) = @_; | ||
| 128 | |||
| 129 | return %{ $self->{'to_fail'} } ? 0 : $multi->timeout(); | ||
| 130 | } | ||
| 131 | |||
| 132 | sub time_out { | ||
| 133 | my ($self, $multi) = @_; | ||
| 134 | |||
| 135 | my $is_active = $multi->socket_action( Net::Curl::Multi::CURL_SOCKET_TIMEOUT() ); | ||
| 136 | |||
| 137 | $self->process_pending($multi); | ||
| 138 | |||
| 139 | return $is_active; | ||
| 140 | } | ||
| 141 | |||
| 142 | #---------------------------------------------------------------------- | ||
| 143 | |||
| 144 | sub _GET_FD_ACTION { | ||
| 145 | return +{ @{ $_[1] } }; | ||
| 146 | } | ||
| 147 | |||
| 148 | sub _is_pending { | ||
| 149 | my ($self, $easy) = @_; | ||
| 150 | |||
| 151 | return $self->{'callbacks'}{$easy} || $self->{'deferred'}{$easy}; | ||
| 152 | } | ||
| 153 | |||
| 154 | sub _finish_handle { | ||
| 155 | my ($self, $easy, $multi, $cb_idx, $payload) = @_; | ||
| 156 | |||
| 157 | # If $cb_idx == 0, then $payload is a promise resolution. | ||
| 158 | # If $cb_idx == 1, then $payload is either: | ||
| 159 | # undef - request canceled | ||
| 160 | # scalar ref - promise rejection | ||
| 161 | |||
| 162 | my $err = $@; | ||
| 163 | |||
| 164 | # Don’t depend on the caller to report failures. | ||
| 165 | # (AnyEvent, for example, blackholes them.) | ||
| 166 | warn if !eval { | ||
| 167 | delete $self->{'to_fail'}{$easy}; | ||
| 168 | |||
| 169 | # This has to precede the callbacks so that $easy can be added back | ||
| 170 | # into $self->{'multi'} within the callback. | ||
| 171 | $multi->remove_handle($easy); | ||
| 172 | |||
| 173 | if ( my $cb_ar = delete $self->{'callbacks'}{$easy} ) { | ||
| 174 | $cb_ar->[$cb_idx]->($cb_idx ? $$payload : $payload) if !$cb_idx || $payload; | ||
| 175 | } | ||
| 176 | elsif ( my $deferred = delete $self->{'deferred'}{$easy} ) { | ||
| 177 | if ($cb_idx) { | ||
| 178 | $deferred->reject($$payload) if $payload; | ||
| 179 | } | ||
| 180 | else { | ||
| 181 | $deferred->resolve($payload); | ||
| 182 | } | ||
| 183 | } | ||
| 184 | else { | ||
| 185 | |||
| 186 | # This shouldn’t happen, but just in case: | ||
| 187 | require Data::Dumper; | ||
| 188 | print STDERR Data::Dumper::Dumper( ORPHAN => $easy => $payload ); | ||
| 189 | } | ||
| 190 | |||
| 191 | 1; | ||
| 192 | }; | ||
| 193 | |||
| 194 | $@ = $err; | ||
| 195 | |||
| 196 | return; | ||
| 197 | } | ||
| 198 | |||
| 199 | #---------------------------------------------------------------------- | ||
| 200 | |||
| 201 | sub _clear_failed { | ||
| 202 | my ($self, $multi) = @_; | ||
| 203 | |||
| 204 | for my $val_ar ( values %{ $self->{'to_fail'} } ) { | ||
| 205 | my ($easy, $reason_sr) = @$val_ar; | ||
| 206 | $self->_finish_handle( $easy, $multi, 1, $reason_sr ); | ||
| 207 | } | ||
| 208 | |||
| 209 | %{ $self->{'to_fail'} } = (); | ||
| 210 | |||
| 211 | return; | ||
| 212 | } | ||
| 213 | |||
| 214 | 1; |