| 1 | package Object::Remote::MiniLoop; | ||
| 2 | |||
| 3 | 11 | ▶ | use IO::Select; |
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||
| 4 | 11 | ▶ | use Time::HiRes qw(time); |
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||
| 5 | 0 | ▶ | use Object::Remote::Logging qw( :log :dlog router ); |
|
Subroutine
BEGIN: called CC=1 SCAR=2.6
| |||
| 6 | use Moo; | ||
| 7 | |||
| 8 | BEGIN { | ||
| 9 | router()->exclude_forwarding | ||
| 10 | } | ||
| 11 | |||
| 12 | # this is ro because we only actually set it using local in sub run | ||
| 13 | has is_running => (is => 'ro', clearer => 'stop'); | ||
| 14 | #maximum duration that select() will block - undef means indefinite, | ||
| 15 | #0 means no blocking, otherwise maximum time in seconds | ||
| 16 | has block_duration => ( is => 'rw' ); | ||
| 17 | |||
| 18 | has _read_watches => (is => 'ro', default => sub { {} }); | ||
| 19 | has _read_select => (is => 'ro', default => sub { IO::Select->new }); | ||
| 20 | |||
| 21 | has _write_watches => (is => 'ro', default => sub { {} }); | ||
| 22 | has _write_select => (is => 'ro', default => sub { IO::Select->new }); | ||
| 23 | |||
| 24 | has _timers => (is => 'ro', default => sub { [] }); | ||
| 25 | |||
| 26 | sub watch_io { | ||
| 27 | my ($self, %watch) = @_; | ||
| 28 | my $fh = $watch{handle}; | ||
| 29 | Dlog_debug { "Adding IO watch for $_" } $fh; | ||
| 30 | |||
| 31 | if (my $cb = $watch{on_read_ready}) { | ||
| 32 | log_trace { "IO watcher is registering with select for reading" }; | ||
| 33 | $self->_read_select->add($fh); | ||
| 34 | $self->_read_watches->{$fh} = $cb; | ||
| 35 | } | ||
| 36 | if (my $cb = $watch{on_write_ready}) { | ||
| 37 | log_trace { "IO watcher is registering with select for writing" }; | ||
| 38 | $self->_write_select->add($fh); | ||
| 39 | $self->_write_watches->{$fh} = $cb; | ||
| 40 | } | ||
| 41 | return; | ||
| 42 | } | ||
| 43 | |||
| 44 | sub unwatch_io { | ||
| 45 | my ($self, %watch) = @_; | ||
| 46 | my $fh = $watch{handle}; | ||
| 47 | Dlog_debug { "Removing IO watch for $_" } $fh; | ||
| 48 | if ($watch{on_read_ready}) { | ||
| 49 | log_trace { "IO watcher is removing read from select()" }; | ||
| 50 | $self->_read_select->remove($fh); | ||
| 51 | delete $self->_read_watches->{$fh}; | ||
| 52 | } | ||
| 53 | if ($watch{on_write_ready}) { | ||
| 54 | log_trace { "IO watcher is removing write from select()" }; | ||
| 55 | $self->_write_select->remove($fh); | ||
| 56 | delete $self->_write_watches->{$fh}; | ||
| 57 | } | ||
| 58 | return; | ||
| 59 | } | ||
| 60 | |||
| 61 | sub _sort_timers { | ||
| 62 | my ($self, @new) = @_; | ||
| 63 | my $timers = $self->_timers; | ||
| 64 | |||
| 65 | log_trace { "Sorting timers" }; | ||
| 66 | |||
| 67 | @{$timers} = sort { $a->[0] <=> $b->[0] } @{$timers}, @new; | ||
| 68 | return; | ||
| 69 | } | ||
| 70 | |||
| 71 | sub watch_time { | ||
| 72 | my ($self, %watch) = @_; | ||
| 73 | my $at; | ||
| 74 | |||
| 75 | Dlog_trace { "watch_time() invoked with $_" } \%watch; | ||
| 76 | |||
| 77 | if (exists($watch{after})) { | ||
| 78 | $at = time() + $watch{after}; | ||
| 79 | } elsif (exists($watch{at})) { | ||
| 80 | $at = $watch{at}; | ||
| 81 | } else { | ||
| 82 | die "watch_time requires after or at"; | ||
| 83 | } | ||
| 84 | |||
| 85 | die "watch_time requires code" unless my $code = $watch{code}; | ||
| 86 | my $timers = $self->_timers; | ||
| 87 | my $new = [ $at => $code ]; | ||
| 88 | $self->_sort_timers($new); | ||
| 89 | log_debug { "Created new timer with id '$new' that expires at '$at'" }; | ||
| 90 | return "$new"; | ||
| 91 | } | ||
| 92 | |||
| 93 | sub unwatch_time { | ||
| 94 | my ($self, $id) = @_; | ||
| 95 | log_trace { "Removing timer with id of '$id'" }; | ||
| 96 | @$_ = grep !($_ eq $id), @$_ for $self->_timers; | ||
| 97 | return; | ||
| 98 | } | ||
| 99 | |||
| 100 | sub _next_timer_expires_delay { | ||
| 101 | my ($self) = @_; | ||
| 102 | my $timers = $self->_timers; | ||
| 103 | my $delay_max = $self->block_duration; | ||
| 104 | |||
| 105 | return $delay_max unless @$timers; | ||
| 106 | my $duration = $timers->[0]->[0] - time; | ||
| 107 | |||
| 108 | log_trace { "next timer fires in '$duration' seconds" }; | ||
| 109 | |||
| 110 | if ($duration < 0) { | ||
| 111 | $duration = 0; | ||
| 112 | } elsif (defined $delay_max && $duration > $delay_max) { | ||
| 113 | $duration = $delay_max; | ||
| 114 | } | ||
| 115 | |||
| 116 | return $duration; | ||
| 117 | } | ||
| 118 | |||
| 119 | sub loop_once { | ||
| 120 | my ($self) = @_; | ||
| 121 | my $read = $self->_read_watches; | ||
| 122 | my $write = $self->_write_watches; | ||
| 123 | my $read_count = 0; | ||
| 124 | my $write_count = 0; | ||
| 125 | my @c = caller; | ||
| 126 | my $wait_time = $self->_next_timer_expires_delay; | ||
| 127 | log_trace { | ||
| 128 | sprintf("Run loop: loop_once() has been invoked by $c[1]:$c[2] with read:%i write:%i select timeout:%s", | ||
| 129 | scalar(keys(%$read)), scalar(keys(%$write)), defined $wait_time ? $wait_time : 'indefinite' ) | ||
| 130 | }; | ||
| 131 | my ($readable, $writeable) = IO::Select->select( | ||
| 132 | $self->_read_select, $self->_write_select, undef, $wait_time | ||
| 133 | ); | ||
| 134 | log_trace { | ||
| 135 | my $readable_count = defined $readable ? scalar(@$readable) : 0; | ||
| 136 | my $writable_count = defined $writeable ? scalar(@$writeable) : 0; | ||
| 137 | "Run loop: select returned readable:$readable_count writeable:$writable_count"; | ||
| 138 | }; | ||
| 139 | # I would love to trap errors in the select call but IO::Select doesn't | ||
| 140 | # differentiate between an error and a timeout. | ||
| 141 | # -- no, love, mst. | ||
| 142 | |||
| 143 | log_trace { "Reading from ready filehandles" }; | ||
| 144 | foreach my $fh (@$readable) { | ||
| 145 | next unless $read->{$fh}; | ||
| 146 | $read_count++; | ||
| 147 | $read->{$fh}(); | ||
| 148 | #FIXME this is a rough workaround for race conditions that can cause deadlocks | ||
| 149 | #under load | ||
| 150 | last; | ||
| 151 | } | ||
| 152 | log_trace { "Writing to ready filehandles" }; | ||
| 153 | foreach my $fh (@$writeable) { | ||
| 154 | next unless $write->{$fh}; | ||
| 155 | $write_count++; | ||
| 156 | $write->{$fh}(); | ||
| 157 | #FIXME this is a rough workaround for race conditions that can cause deadlocks | ||
| 158 | #under load | ||
| 159 | last; | ||
| 160 | } | ||
| 161 | |||
| 162 | #moving the timers above the read() section exposes a deadlock | ||
| 163 | log_trace { "Read from $read_count filehandles; wrote to $write_count filehandles" }; | ||
| 164 | my $timers = $self->_timers; | ||
| 165 | my $now = time(); | ||
| 166 | log_trace { "Checking timers" }; | ||
| 167 | while (@$timers and $timers->[0][0] <= $now) { | ||
| 168 | my $active = $timers->[0]; | ||
| 169 | Dlog_trace { "Found timer that needs to be executed: '$active'" }; | ||
| 170 | shift(@$timers); | ||
| 171 | |||
| 172 | #execute the timer | ||
| 173 | $active->[1]->(); | ||
| 174 | } | ||
| 175 | |||
| 176 | log_trace { "Run loop: single loop is completed" }; | ||
| 177 | return; | ||
| 178 | } | ||
| 179 | |||
| 180 | sub run { | ||
| 181 | my ($self) = @_; | ||
| 182 | log_trace { "Run loop: run() invoked" }; | ||
| 183 | local $self->{is_running} = 1; | ||
| 184 | while ($self->is_running) { | ||
| 185 | $self->loop_once; | ||
| 186 | } | ||
| 187 | log_trace { "Run loop: run() completed" }; | ||
| 188 | return; | ||
| 189 | } | ||
| 190 | |||
| 191 | sub new_future { | ||
| 192 | return Future->new; | ||
| 193 | } | ||
| 194 | |||
| 195 | our @await; | ||
| 196 | |||
| 197 | sub await { | ||
| 198 | my ($self, $f) = @_; | ||
| 199 | log_trace { my $ir = $f->is_ready; "await_future() invoked; is_ready: $ir" }; | ||
| 200 | return $f if $f->is_ready; | ||
| 201 | require Object::Remote; | ||
| 202 | my $loop = Object::Remote->current_loop; | ||
| 203 | { | ||
| 204 | local @await = (@await, $f); | ||
| 205 | $f->on_ready(sub { | ||
| 206 | log_trace { my $l = @await; "future has become ready, length of \@await: '$l'" }; | ||
| 207 | if ($f == $await[-1]) { | ||
| 208 | log_trace { "This future is not waiting on anything so calling stop on the run loop" }; | ||
| 209 | $loop->stop; | ||
| 210 | } | ||
| 211 | }); | ||
| 212 | log_trace { "Starting run loop for newly created future" }; | ||
| 213 | $loop->run; | ||
| 214 | } | ||
| 215 | if (@await and $await[-1]->is_ready) { | ||
| 216 | log_trace { "Last future in await list was ready, stopping run loop" }; | ||
| 217 | $loop->stop; | ||
| 218 | } | ||
| 219 | log_trace { "await_future() returning" }; | ||
| 220 | return $f; | ||
| 221 | } | ||
| 222 | |||
| 223 | sub await_all { | ||
| 224 | my $self = shift; | ||
| 225 | $self->await(Future->wait_all(@_)); | ||
| 226 | return; | ||
| 227 | } | ||
| 228 | |||
| 229 | 1; | ||