Commit | Line | Data |
9e72f0cf |
1 | package Object::Remote::MiniLoop; |
2 | |
3 | use IO::Select; |
befabdee |
4 | use Time::HiRes qw(time); |
f4a85080 |
5 | use Object::Remote::Logging qw( :log :dlog router ); |
9e72f0cf |
6 | use Moo; |
7 | |
e1a0b9ca |
8 | BEGIN { |
9 | $SIG{PIPE} = sub { log_debug { "Got a PIPE signal" } }; |
10 | |
11 | router()->exclude_forwarding |
12 | } |
4e446335 |
13 | |
9e72f0cf |
14 | # this is ro because we only actually set it using local in sub run |
9e72f0cf |
15 | has is_running => (is => 'ro', clearer => 'stop'); |
353556c4 |
16 | #maximum duration that select() will block - undef means indefinite, |
17 | #0 means no blocking, otherwise maximum time in seconds |
18 | has block_duration => ( is => 'rw' ); |
9e72f0cf |
19 | |
20 | has _read_watches => (is => 'ro', default => sub { {} }); |
21 | has _read_select => (is => 'ro', default => sub { IO::Select->new }); |
22 | |
fbd3b8ec |
23 | has _write_watches => (is => 'ro', default => sub { {} }); |
24 | has _write_select => (is => 'ro', default => sub { IO::Select->new }); |
25 | |
befabdee |
26 | has _timers => (is => 'ro', default => sub { [] }); |
27 | |
9e72f0cf |
28 | sub pass_watches_to { |
29 | my ($self, $new_loop) = @_; |
a63cd862 |
30 | log_debug { "passing watches to new run loop" }; |
9e72f0cf |
31 | foreach my $fh ($self->_read_select->handles) { |
32 | $new_loop->watch_io( |
33 | handle => $fh, |
34 | on_read_ready => $self->_read_watches->{$fh} |
35 | ); |
36 | } |
fbd3b8ec |
37 | foreach my $fh ($self->_write_select->handles) { |
38 | $new_loop->watch_io( |
39 | handle => $fh, |
40 | on_write_ready => $self->_write_watches->{$fh} |
41 | ); |
42 | } |
9e72f0cf |
43 | } |
44 | |
45 | sub watch_io { |
46 | my ($self, %watch) = @_; |
47 | my $fh = $watch{handle}; |
90115979 |
48 | Dlog_debug { "Adding IO watch for $_" } $fh; |
6b7b2732 |
49 | |
9e72f0cf |
50 | if (my $cb = $watch{on_read_ready}) { |
90115979 |
51 | log_trace { "IO watcher is registering with select for reading" }; |
9e72f0cf |
52 | $self->_read_select->add($fh); |
53 | $self->_read_watches->{$fh} = $cb; |
54 | } |
fbd3b8ec |
55 | if (my $cb = $watch{on_write_ready}) { |
90115979 |
56 | log_trace { "IO watcher is registering with select for writing" }; |
fbd3b8ec |
57 | $self->_write_select->add($fh); |
58 | $self->_write_watches->{$fh} = $cb; |
59 | } |
498c4ad5 |
60 | return; |
9e72f0cf |
61 | } |
62 | |
63 | sub unwatch_io { |
64 | my ($self, %watch) = @_; |
65 | my $fh = $watch{handle}; |
9d64d2d9 |
66 | Dlog_debug { "Removing IO watch for $_" } $fh; |
9e72f0cf |
67 | if ($watch{on_read_ready}) { |
9d64d2d9 |
68 | log_trace { "IO watcher is removing read from select()" }; |
9e72f0cf |
69 | $self->_read_select->remove($fh); |
70 | delete $self->_read_watches->{$fh}; |
71 | } |
fbd3b8ec |
72 | if ($watch{on_write_ready}) { |
9d64d2d9 |
73 | log_trace { "IO watcher is removing write from select()" }; |
fbd3b8ec |
74 | $self->_write_select->remove($fh); |
75 | delete $self->_write_watches->{$fh}; |
76 | } |
befabdee |
77 | return; |
78 | } |
79 | |
f8080c1c |
80 | sub _sort_timers { |
81 | my ($self, @new) = @_; |
82 | my $timers = $self->_timers; |
83 | |
84 | log_trace { "Sorting timers" }; |
85 | |
86 | @{$timers} = sort { $a->[0] <=> $b->[0] } @{$timers}, @new; |
87 | return; |
88 | } |
89 | |
befabdee |
90 | sub watch_time { |
91 | my ($self, %watch) = @_; |
f8080c1c |
92 | my $at; |
93 | |
94 | Dlog_trace { "watch_time() invoked with $_" } \%watch; |
95 | |
96 | if (exists($watch{every})) { |
97 | $at = time() + $watch{every}; |
98 | } elsif (exists($watch{after})) { |
99 | $at = time() + $watch{after}; |
100 | } elsif (exists($watch{at})) { |
37efeb68 |
101 | $at = $watch{at}; |
f8080c1c |
102 | } else { |
37efeb68 |
103 | die "watch_time requires every, after or at"; |
f8080c1c |
104 | } |
105 | |
befabdee |
106 | die "watch_time requires code" unless my $code = $watch{code}; |
107 | my $timers = $self->_timers; |
f8080c1c |
108 | my $new = [ $at => $code, $watch{every} ]; |
109 | $self->_sort_timers($new); |
c824fdf3 |
110 | log_debug { "Created new timer with id '$new' that expires at '$at'" }; |
befabdee |
111 | return "$new"; |
112 | } |
113 | |
114 | sub unwatch_time { |
115 | my ($self, $id) = @_; |
8d757beb |
116 | log_trace { "Removing timer with id of '$id'" }; |
befabdee |
117 | @$_ = grep !($_ eq $id), @$_ for $self->_timers; |
118 | return; |
9e72f0cf |
119 | } |
120 | |
5d59cb98 |
121 | sub _next_timer_expires_delay { |
122 | my ($self) = @_; |
123 | my $timers = $self->_timers; |
353556c4 |
124 | my $delay_max = $self->block_duration; |
5d59cb98 |
125 | |
126 | return $delay_max unless @$timers; |
127 | my $duration = $timers->[0]->[0] - time; |
128 | |
129 | log_trace { "next timer fires in '$duration' seconds " }; |
130 | |
131 | if ($duration < 0) { |
132 | $duration = 0; |
a6786dda |
133 | } elsif (defined $delay_max && $duration > $delay_max) { |
134 | $duration = $delay_max; |
5d59cb98 |
135 | } |
a6786dda |
136 | |
5d59cb98 |
137 | return $duration; |
138 | } |
139 | |
9e72f0cf |
140 | sub loop_once { |
141 | my ($self) = @_; |
142 | my $read = $self->_read_watches; |
fbd3b8ec |
143 | my $write = $self->_write_watches; |
6b7b2732 |
144 | our $Loop_Entered = 1; |
5d59cb98 |
145 | my $read_count = 0; |
146 | my $write_count = 0; |
a63cd862 |
147 | my @c = caller; |
5d59cb98 |
148 | my $wait_time = $self->_next_timer_expires_delay; |
8f43bcd9 |
149 | log_trace { |
150 | sprintf("Run loop: loop_once() has been invoked by $c[1]:$c[2] with read:%i write:%i select timeout:%s", |
151 | scalar(keys(%$read)), scalar(keys(%$write)), defined $wait_time ? $wait_time : 'indefinite' ) |
152 | }; |
fbd3b8ec |
153 | my ($readable, $writeable) = IO::Select->select( |
5d59cb98 |
154 | $self->_read_select, $self->_write_select, undef, $wait_time |
155 | ); |
6b7b2732 |
156 | log_trace { |
5d59cb98 |
157 | my $readable_count = defined $readable ? scalar(@$readable) : 0; |
158 | my $writable_count = defined $writeable ? scalar(@$writeable) : 0; |
159 | "Run loop: select returned readable:$readable_count writeable:$writable_count"; |
a63cd862 |
160 | }; |
9e72f0cf |
161 | # I would love to trap errors in the select call but IO::Select doesn't |
162 | # differentiate between an error and a timeout. |
163 | # -- no, love, mst. |
6b7b2732 |
164 | |
165 | local $Loop_Entered; |
166 | |
a63cd862 |
167 | log_trace { "Reading from all ready filehandles" }; |
9e72f0cf |
168 | foreach my $fh (@$readable) { |
5d59cb98 |
169 | next unless $read->{$fh}; |
170 | $read_count++; |
171 | $read->{$fh}(); |
6b7b2732 |
172 | last if $Loop_Entered; |
fbd3b8ec |
173 | } |
a63cd862 |
174 | log_trace { "Writing to all ready filehandles" }; |
fbd3b8ec |
175 | foreach my $fh (@$writeable) { |
5d59cb98 |
176 | next unless $write->{$fh}; |
177 | $write_count++; |
178 | $write->{$fh}(); |
6b7b2732 |
179 | last if $Loop_Entered; |
9e72f0cf |
180 | } |
7790ca36 |
181 | |
5d59cb98 |
182 | log_trace { "Read from $read_count filehandles; wrote to $write_count filehandles" }; |
befabdee |
183 | my $timers = $self->_timers; |
184 | my $now = time(); |
a63cd862 |
185 | log_trace { "Checking timers" }; |
befabdee |
186 | while (@$timers and $timers->[0][0] <= $now) { |
f8080c1c |
187 | my $active = $timers->[0]; |
b7a853b3 |
188 | Dlog_trace { "Found timer that needs to be executed: '$active'" }; |
f8080c1c |
189 | |
190 | if (defined($active->[2])) { |
191 | #handle the case of an 'every' timer |
192 | $active->[0] = time() + $active->[2]; |
193 | Dlog_trace { "scheduling timer for repeat execution at $_"} $active->[0]; |
194 | $self->_sort_timers; |
195 | } else { |
196 | #it doesn't repeat again so get rid of it |
197 | shift(@$timers); |
198 | } |
199 | |
200 | #execute the timer |
201 | $active->[1]->(); |
202 | |
6b7b2732 |
203 | last if $Loop_Entered; |
befabdee |
204 | } |
f8080c1c |
205 | |
6b7b2732 |
206 | log_trace { "Run loop: single loop is completed" }; |
befabdee |
207 | return; |
9e72f0cf |
208 | } |
209 | |
6c597351 |
210 | sub want_run { |
211 | my ($self) = @_; |
5d59cb98 |
212 | Dlog_debug { "Run loop: Incrimenting want_running, is now $_" } |
213 | ++$self->{want_running}; |
6c597351 |
214 | } |
215 | |
216 | sub run_while_wanted { |
217 | my ($self) = @_; |
9d64d2d9 |
218 | log_debug { my $wr = $self->{want_running}; "Run loop: run_while_wanted() invoked; want_running: $wr" }; |
6c597351 |
219 | $self->loop_once while $self->{want_running}; |
a63cd862 |
220 | log_debug { "Run loop: run_while_wanted() completed" }; |
befabdee |
221 | return; |
6c597351 |
222 | } |
223 | |
224 | sub want_stop { |
225 | my ($self) = @_; |
5d59cb98 |
226 | if (! $self->{want_running}) { |
227 | log_debug { "Run loop: want_stop() was called but want_running was not true" }; |
228 | return; |
229 | } |
230 | Dlog_debug { "Run loop: decrimenting want_running, is now $_" } |
231 | --$self->{want_running}; |
6c597351 |
232 | } |
233 | |
9e72f0cf |
234 | sub run { |
235 | my ($self) = @_; |
6b7b2732 |
236 | log_trace { "Run loop: run() invoked" }; |
9e72f0cf |
237 | local $self->{is_running} = 1; |
238 | while ($self->is_running) { |
239 | $self->loop_once; |
240 | } |
6b7b2732 |
241 | log_trace { "Run loop: run() completed" }; |
befabdee |
242 | return; |
9e72f0cf |
243 | } |
244 | |
245 | 1; |