| 1 | package Mojo::Reactor::Epoll; | ||
| 2 | use Mojo::Base 'Mojo::Reactor::Poll'; | ||
| 3 | |||
| 4 | $ENV{MOJO_REACTOR} ||= 'Mojo::Reactor::Epoll'; | ||
| 5 | |||
| 6 | use Carp 'croak'; | ||
| 7 | use Linux::Epoll; | ||
| 8 | use List::Util 'min'; | ||
| 9 | use Mojo::Util qw(md5_sum steady_time); | ||
| 10 | use Scalar::Util 'weaken'; | ||
| 11 | use Time::HiRes 'usleep'; | ||
| 12 | |||
| 13 | use constant DEBUG => $ENV{MOJO_REACTOR_EPOLL_DEBUG} || 0; | ||
| 14 | |||
| 15 | our $VERSION = '0.011'; | ||
| 16 | |||
| 17 | sub io { | ||
| 18 | my ($self, $handle, $cb) = @_; | ||
| 19 | my $fd = fileno($handle) // croak 'Handle is closed'; | ||
| 20 | $self->{io}{$fd}{cb} = $cb; | ||
| 21 | warn "-- Set IO watcher for $fd\n" if DEBUG; | ||
| 22 | return $self->watch($handle, 1, 1); | ||
| 23 | } | ||
| 24 | |||
| 25 | sub one_tick { | ||
| 26 | my $self = shift; | ||
| 27 | |||
| 28 | # Just one tick | ||
| 29 | local $self->{running} = 1 unless $self->{running}; | ||
| 30 | |||
| 31 | # Wait for one event | ||
| 32 | my $i; | ||
| 33 | until ($i || !$self->{running}) { | ||
| 34 | # Stop automatically if there is nothing to watch | ||
| 35 | return $self->stop unless keys %{$self->{timers}} || keys %{$self->{io}}; | ||
| 36 | |||
| 37 | # Calculate ideal timeout based on timers | ||
| 38 | my $min = min map { $_->{time} } values %{$self->{timers}}; | ||
| 39 | my $timeout = defined $min ? ($min - steady_time) : 0.5; | ||
| 40 | $timeout = 0 if $timeout < 0; | ||
| 41 | |||
| 42 | # I/O | ||
| 43 | if (my $watched = keys %{$self->{io}}) { | ||
| 44 | # Set max events to half the number of descriptors, to a minimum of 10 | ||
| 45 | my $maxevents = int $watched/2; | ||
| 46 | $maxevents = 10 if $maxevents < 10; | ||
| 47 | |||
| 48 | my $epoll = $self->{epoll} // $self->_create_epoll; | ||
| 49 | |||
| 50 | my $count = $epoll->wait($maxevents, $timeout); | ||
| 51 | $i += $count if defined $count; | ||
| 52 | } | ||
| 53 | |||
| 54 | # Wait for timeout if epoll can't be used | ||
| 55 | elsif ($timeout) { usleep $timeout * 1000000 } | ||
| 56 | |||
| 57 | # Timers (time should not change in between timers) | ||
| 58 | my $now = steady_time; | ||
| 59 | for my $id (keys %{$self->{timers}}) { | ||
| 60 | next unless my $t = $self->{timers}{$id}; | ||
| 61 | next unless $t->{time} <= $now; | ||
| 62 | |||
| 63 | # Recurring timer | ||
| 64 | if ($t->{recurring}) { $t->{time} = $now + $t->{after} } | ||
| 65 | |||
| 66 | # Normal timer | ||
| 67 | else { $self->remove($id) } | ||
| 68 | |||
| 69 | ++$i and $self->_try('Timer', $t->{cb}) if $t->{cb}; | ||
| 70 | } | ||
| 71 | } | ||
| 72 | } | ||
| 73 | |||
| 74 | sub recurring { shift->_timer(1, @_) } | ||
| 75 | |||
| 76 | sub remove { | ||
| 77 | my ($self, $remove) = @_; | ||
| 78 | if (ref $remove) { | ||
| 79 | my $fd = fileno($remove) // croak 'Handle is closed'; | ||
| 80 | if (exists $self->{io}{$fd} and exists $self->{io}{$fd}{epoll_cb}) { | ||
| 81 | $self->{epoll}->delete($remove); | ||
| 82 | } | ||
| 83 | warn "-- Removed IO watcher for $fd\n" if DEBUG; | ||
| 84 | return !!delete $self->{io}{$fd}; | ||
| 85 | } else { | ||
| 86 | warn "-- Removed timer $remove\n" if DEBUG; | ||
| 87 | return !!delete $self->{timers}{$remove}; | ||
| 88 | } | ||
| 89 | } | ||
| 90 | |||
| 91 | sub reset { | ||
| 92 | my $self = shift; | ||
| 93 | delete @{$self}{qw(epoll pending_watch)}; | ||
| 94 | $self->SUPER::reset; | ||
| 95 | } | ||
| 96 | |||
| 97 | sub timer { shift->_timer(0, @_) } | ||
| 98 | |||
| 99 | sub watch { | ||
| 100 | my ($self, $handle, $read, $write) = @_; | ||
| 101 | |||
| 102 | my $fd = fileno $handle; | ||
| 103 | croak 'I/O watcher not active' unless my $io = $self->{io}{$fd}; | ||
| 104 | |||
| 105 | my $epoll = $self->{epoll}; | ||
| 106 | unless (defined $epoll) { | ||
| 107 | push @{$self->{pending_watch}}, [$handle, $read, $write]; | ||
| 108 | return $self; | ||
| 109 | } | ||
| 110 | |||
| 111 | my @events; | ||
| 112 | push @events, 'in', 'prio' if $read; | ||
| 113 | push @events, 'out' if $write; | ||
| 114 | |||
| 115 | my $op = exists $io->{epoll_cb} ? 'modify' : 'add'; | ||
| 116 | |||
| 117 | weaken $self; | ||
| 118 | my $cb = $io->{epoll_cb} // sub { | ||
| 119 | my ($events) = @_; | ||
| 120 | if ($events->{in} or $events->{prio} or $events->{hup} or $events->{err}) { | ||
| 121 | return unless exists $self->{io}{$fd}; | ||
| 122 | $self->_try('I/O watcher', $self->{io}{$fd}{cb}, 0); | ||
| 123 | } | ||
| 124 | if ($events->{out} or $events->{hup} or $events->{err}) { | ||
| 125 | return unless exists $self->{io}{$fd}; | ||
| 126 | $self->_try('I/O watcher', $self->{io}{$fd}{cb}, 1); | ||
| 127 | } | ||
| 128 | }; | ||
| 129 | $epoll->$op($handle, \@events, $cb); | ||
| 130 | |||
| 131 | # Cache callback for future modify operations, after successfully added to epoll | ||
| 132 | $io->{epoll_cb} //= $cb; | ||
| 133 | |||
| 134 | return $self; | ||
| 135 | } | ||
| 136 | |||
| 137 | sub _create_epoll { | ||
| 138 | my $self = shift; | ||
| 139 | $self->{epoll} = Linux::Epoll->new; | ||
| 140 | $self->watch(@$_) for @{delete $self->{pending_watch} // []}; | ||
| 141 | return $self->{epoll}; | ||
| 142 | } | ||
| 143 | |||
| 144 | sub _id { | ||
| 145 | my $self = shift; | ||
| 146 | my $id; | ||
| 147 | do { $id = md5_sum 't' . steady_time . rand } while $self->{timers}{$id}; | ||
| 148 | return $id; | ||
| 149 | } | ||
| 150 | |||
| 151 | sub _timer { | ||
| 152 | my ($self, $recurring, $after, $cb) = @_; | ||
| 153 | |||
| 154 | my $id = $self->_id; | ||
| 155 | my $timer = $self->{timers}{$id} = { | ||
| 156 | cb => $cb, | ||
| 157 | after => $after, | ||
| 158 | recurring => $recurring, | ||
| 159 | time => steady_time + $after, | ||
| 160 | }; | ||
| 161 | |||
| 162 | if (DEBUG) { | ||
| 163 | my $is_recurring = $recurring ? ' (recurring)' : ''; | ||
| 164 | warn "-- Set timer $id after $after seconds$is_recurring\n"; | ||
| 165 | } | ||
| 166 | |||
| 167 | return $id; | ||
| 168 | } | ||
| 169 | |||
| 170 | sub _try { | ||
| 171 | my ($self, $what, $cb) = (shift, shift, shift); | ||
| 172 | eval { $self->$cb(@_); 1 } or $self->emit(error => "$what failed: $@"); | ||
| 173 | } | ||
| 174 | |||
| 175 | 1; | ||
| 176 | |||
| 177 | =head1 NAME | ||
| 178 | |||
| 179 | Mojo::Reactor::Epoll - epoll backend for Mojo::Reactor | ||
| 180 | |||
| 181 | =head1 SYNOPSIS | ||
| 182 | |||
| 183 | use Mojo::Reactor::Epoll; | ||
| 184 | |||
| 185 | # Watch if handle becomes readable or writable | ||
| 186 | my $reactor = Mojo::Reactor::Epoll->new; | ||
| 187 | $reactor->io($first => sub { | ||
| 188 | my ($reactor, $writable) = @_; | ||
| 189 | say $writable ? 'First handle is writable' : 'First handle is readable'; | ||
| 190 | }); | ||
| 191 | |||
| 192 | # Change to watching only if handle becomes writable | ||
| 193 | $reactor->watch($first, 0, 1); | ||
| 194 | |||
| 195 | # Turn file descriptor into handle and watch if it becomes readable | ||
| 196 | my $second = IO::Handle->new_from_fd($fd, 'r'); | ||
| 197 | $reactor->io($second => sub { | ||
| 198 | my ($reactor, $writable) = @_; | ||
| 199 | say $writable ? 'Second handle is writable' : 'Second handle is readable'; | ||
| 200 | })->watch($second, 1, 0); | ||
| 201 | |||
| 202 | # Add a timer | ||
| 203 | $reactor->timer(15 => sub { | ||
| 204 | my $reactor = shift; | ||
| 205 | $reactor->remove($first); | ||
| 206 | $reactor->remove($second); | ||
| 207 | say 'Timeout!'; | ||
| 208 | }); | ||
| 209 | |||
| 210 | # Start reactor if necessary | ||
| 211 | $reactor->start unless $reactor->is_running; | ||
| 212 | |||
| 213 | # Or in an application using Mojo::IOLoop | ||
| 214 | use Mojo::Reactor::Epoll; | ||
| 215 | use Mojo::IOLoop; | ||
| 216 | |||
| 217 | # Or in a Mojolicious application | ||
| 218 | $ MOJO_REACTOR=Mojo::Reactor::Epoll hypnotoad script/myapp | ||
| 219 | |||
| 220 | =head1 DESCRIPTION | ||
| 221 | |||
| 222 | L<Mojo::Reactor::Epoll> is an event reactor for L<Mojo::IOLoop> that uses the | ||
| 223 | L<epoll(7)> Linux subsystem. The usage is exactly the same as other | ||
| 224 | L<Mojo::Reactor> implementations such as L<Mojo::Reactor::Poll>. | ||
| 225 | L<Mojo::Reactor::Epoll> will be used as the default backend for L<Mojo::IOLoop> | ||
| 226 | if it is loaded before L<Mojo::IOLoop> or any module using the loop. However, | ||
| 227 | when invoking a L<Mojolicious> application through L<morbo> or L<hypnotoad>, | ||
| 228 | the reactor must be set as the default by setting the C<MOJO_REACTOR> | ||
| 229 | environment variable to C<Mojo::Reactor::Epoll>. | ||
| 230 | |||
| 231 | =head1 EVENTS | ||
| 232 | |||
| 233 | L<Mojo::Reactor::Epoll> inherits all events from L<Mojo::Reactor::Poll>. | ||
| 234 | |||
| 235 | =head1 METHODS | ||
| 236 | |||
| 237 | L<Mojo::Reactor::Epoll> inherits all methods from L<Mojo::Reactor::Poll> and | ||
| 238 | implements the following new ones. | ||
| 239 | |||
| 240 | =head2 io | ||
| 241 | |||
| 242 | $reactor = $reactor->io($handle => sub {...}); | ||
| 243 | |||
| 244 | Watch handle for I/O events, invoking the callback whenever handle becomes | ||
| 245 | readable or writable. | ||
| 246 | |||
| 247 | # Callback will be invoked twice if handle becomes readable and writable | ||
| 248 | $reactor->io($handle => sub { | ||
| 249 | my ($reactor, $writable) = @_; | ||
| 250 | say $writable ? 'Handle is writable' : 'Handle is readable'; | ||
| 251 | }); | ||
| 252 | |||
| 253 | =head2 one_tick | ||
| 254 | |||
| 255 | $reactor->one_tick; | ||
| 256 | |||
| 257 | Run reactor until an event occurs or no events are being watched anymore. Note | ||
| 258 | that this method can recurse back into the reactor, so you need to be careful. | ||
| 259 | |||
| 260 | # Don't block longer than 0.5 seconds | ||
| 261 | my $id = $reactor->timer(0.5 => sub {}); | ||
| 262 | $reactor->one_tick; | ||
| 263 | $reactor->remove($id); | ||
| 264 | |||
| 265 | =head2 recurring | ||
| 266 | |||
| 267 | my $id = $reactor->recurring(0.25 => sub {...}); | ||
| 268 | |||
| 269 | Create a new recurring timer, invoking the callback repeatedly after a given | ||
| 270 | amount of time in seconds. | ||
| 271 | |||
| 272 | =head2 remove | ||
| 273 | |||
| 274 | my $bool = $reactor->remove($handle); | ||
| 275 | my $bool = $reactor->remove($id); | ||
| 276 | |||
| 277 | Remove handle or timer. | ||
| 278 | |||
| 279 | =head2 reset | ||
| 280 | |||
| 281 | $reactor->reset; | ||
| 282 | |||
| 283 | Remove all handles and timers. | ||
| 284 | |||
| 285 | =head2 timer | ||
| 286 | |||
| 287 | my $id = $reactor->timer(0.5 => sub {...}); | ||
| 288 | |||
| 289 | Create a new timer, invoking the callback after a given amount of time in | ||
| 290 | seconds. | ||
| 291 | |||
| 292 | =head2 watch | ||
| 293 | |||
| 294 | $reactor = $reactor->watch($handle, $readable, $writable); | ||
| 295 | |||
| 296 | Change I/O events to watch handle for with true and false values. Note that | ||
| 297 | this method requires an active I/O watcher. | ||
| 298 | |||
| 299 | # Watch only for readable events | ||
| 300 | $reactor->watch($handle, 1, 0); | ||
| 301 | |||
| 302 | # Watch only for writable events | ||
| 303 | $reactor->watch($handle, 0, 1); | ||
| 304 | |||
| 305 | # Watch for readable and writable events | ||
| 306 | $reactor->watch($handle, 1, 1); | ||
| 307 | |||
| 308 | # Pause watching for events | ||
| 309 | $reactor->watch($handle, 0, 0); | ||
| 310 | |||
| 311 | =head1 CAVEATS | ||
| 312 | |||
| 313 | The epoll notification facility is exclusive to Linux systems. | ||
| 314 | |||
| 315 | The epoll handle is not usable across forks, and this is not currently managed | ||
| 316 | for you, though it is not created until the loop is started to allow for | ||
| 317 | preforking deployments such as L<hypnotoad>. | ||
| 318 | |||
| 319 | =head1 BUGS | ||
| 320 | |||
| 321 | Report any issues on the public bugtracker. | ||
| 322 | |||
| 323 | =head1 AUTHOR | ||
| 324 | |||
| 325 | Dan Book, C<dbook@cpan.org> | ||
| 326 | |||
| 327 | =head1 COPYRIGHT AND LICENSE | ||
| 328 | |||
| 329 | Copyright 2015, Dan Book. | ||
| 330 | |||
| 331 | This library is free software; you may redistribute it and/or modify it under | ||
| 332 | the terms of the Artistic License version 2.0. | ||
| 333 | |||
| 334 | =head1 SEE ALSO | ||
| 335 | |||
| 336 | L<Mojolicious>, L<Mojo::IOLoop>, L<Linux::Epoll> |