blib/lib/HPCI/TimeoutQueue.pm untested

filter: Statementstmt 0.0% 0 / 79 Branchbran 0.0% 0 / 22 Conditioncond 0.0% 0 / 21 MC/DCmcdc n/a Subroutinesub 0.0% 0 / 11 Podpod 0.0% 0 / 9 totaltotal 0.0% 0 / 142 CC 12 SCAR 25.0
CC
12
Cov
0%
add_timeout
30.0
expire_timeouts
12.0
_expire_timeouts
12.0
CRAP 12.2
1 package HPCI::TimeoutQueue;
2  
3 ### INCLUDES ##################################################################
4  
5 # safe Perl
6 use warnings;
7 use strict;
8  
9 =head1 NAME
10  
11 HPCI::TimeoutQueue;
12  
13 =head1 SYNOPSIS
14  
15 Manage a collection of requests for timeout notifications.
16  
17 =head1 DESCRIPTION
18  
19 This is used internally to HPCI - no user-serviceable parts to be
20 found here.
21  
22 A TimeoutQueue keeps a list of outstanding timeout entries.
23 Each entry consists of a time when it will trigger (passed in as
24 seconds until trigger, stored internally as epoch time of the trigger
25 moment), an object to be informed, along with the name of the method
26 to call to inform the object, and any additional arguments required.
27  
28 They are kept in a list, sorted in trigger order.
29  
30 When entries are deleted before being triggered, they are deleted
31 only if they are at an end of the list. They are not removed if
32 they are in the middle of the list; instead the object field is
33 set to undef.
34  
35 When a new object is being inserted, it will replace a "deleted"
36 entry (undef object) if there is one in an acceptable spot.
37  
38 =head1 METHODS
39  
40 =head2 new
41  
42 No arguments, initials a new TimeoutQueue object.
43  
44 =cut
45  
46 sub new {
47 my $class = shift;
48 $class = ref($class) || $class;
49 bless [], $class;
50 }
51  
52 =head2 delete_timeouts( $object [, method_name [, arg ...] ] )
53  
54 Deletes all timeouts which match the specified object (and method
55 and arg values if provided).
56  
57 It may sometimes make sense to provide arg values that the method
58 will ignore, if there is a need to be able to selectively disable
59 specific timeouts while leaving others intact.
60  
61 =cut
62  
63 sub delete_timeouts {
64 my $self = shift;
65 for my $entry (@$self) {
66 $entry->[1] = undef if cmp_args( $entry, \@_ );
67 }
68 $self->_clean_front;
69 $self->_clean_back;
70 }
71  
72 sub cmp_args {
73 my( $entry, $args ) = @_;
74 my $ind = 1;
75 for (@$args) {
76 return 0 unless $entry->[$ind++] eq $_;
77 }
78 }
79  
80 =head2 expire_timeouts
81  
82 Trigger all timeouts that have expired. Returns a list of them in
83 array context, a count of the number triggered in scalar context,
84 or nothing.
85  
86 =cut
87  
88 sub expire_timeouts {
89 my $self = shift;
90 my $now = time;
91 if (wantarray) {
92 my @list;
93 $self->_expire_timeouts(
94 sub {
95 my $elem = shift;
96 $elem = [ @$elem ];
97 shift @$elem;
98 push @list, $elem;
99 },
100 @_
101 );
102 return \@list;
103 }
104 elsif (defined wantarray) {
105 my $count = 0;
106 $self->_expire_timeouts( sub { ++$count }, @_ );
107 return $count;
108 }
109 else {
110 $self->_expire_timeouts( undef, @_ );
111 return;
112 }
113 }
114  
115 sub _expire_timeouts {
116 my $self = shift;
117 my $callback = shift;
118 my $now = time;
119 while (@$self && $self->[0][0] < $now) {
120 my $entry = shift @$self;
121 my ( undef, $obj, $meth, @args ) = @$entry;
122 if (defined $obj && $obj->can($meth)) {
123 $obj->$meth(@args);
124 $callback->( $entry ) if $callback;
125 }
126 }
127 $self->_clean_front;
128 return;
129 }
130  
131 =head2 time_to_next_timeout
132  
133 Return the time until the next timeout will trigger. A zero or
134 negative means that there is one or more not yet triggered timeout
135 that is already (over)due.
136  
137 Returns undef if the queue is empty.
138  
139 =cut
140  
141 sub time_to_next_timeout {
142 my $self = shift;
143 return undef unless @$self;
144 return $self->[0][0] - time;
145 }
146  
147 =head2 add_timeout( seconds_until_trigger, object, method[, args ...] )
148  
149 Add a timeout trigger entry to the queue.
150  
151 The time until the trigger is given in seconds, but is converted
152 to seconds from epoch by adding the current time to it.
153  
154 If the trigger time is reached without the entry being deleted,
155 the specified object will be notified using the specified method
156 (and any additional specified arguments).
157  
158 This method is complicated somewhat because it tries to find a
159 previously deleted entry that can be replaced.
160  
161 =cut
162  
163 sub add_timeout {
164 my $self = shift;
165 my $time = shift;
166 my $obj = shift;
167 my $meth = shift;
168  
169 $time += time;
170 my $entry = [ $time, $obj, $meth, @_ ];
171  
172 # push on the end if we can
173 # note: it is not worth the bother of checking for a deleted entry
174 # that has the same trigger time, so we might push instead of
175 # replacing on some rare occassions. Here, it is better to do a
176 # reasonable job quickly than a perfect job slowly.
177 if (0 == scalar(@$self) || $time >= $self->[-1][0])
178 {
179 push @$self, $entry;
180 return;
181 }
182  
183 # list is not empty, last element <= trigger time
184  
185 # unshift onto the beginning if we can
186 if ($time <= $self->[0][0]) {
187 unshift @$self, $entry;
188 return;
189 }
190  
191 # list has 2+ entries (otheriwse we would have pushed or unshifted above)
192 # also, replace or splice must occur BETWEEN first and last element
193 my $ind = $#$self;
194 --$ind while $self->[$ind][0] > $time; # will stop at 0 or higher
195 # ind is highest index without a later time, cant be last
196 if (not defined $self->[$ind+1][1]) {
197 $self->[$ind+1] = $entry;
198 return;
199 }
200  
201 --$ind while $self->[$ind][0] == $time && defined $self->[$ind][1];
202 # ind is highest index with a earlier time, or is replaceable with equal time
203 if (not defined $self->[$ind][1]) {
204 $self->[$ind] = $entry;
205 return;
206 }
207  
208 # cant replace anything without copying chunk of the array
209 splice @$self, $ind, 0, $entry;
210 return;
211 }
212  
213 =head2 _clean_front
214  
215 =head2 _clean_back
216  
217 The state of the TimeoutQueue, execpt in the middle of internal
218 operations, guarantees that the first and last entries in the list
219 (if there are any entries) are not deleted elements.
220  
221 _clean_front and _clean_back are internal methods used to
222 re-establish this assertion whenever any action is taken that might
223 have disrupted it.
224  
225 =cut
226  
227 sub _clean_front {
228 my $self = shift;
229 shift @$self while scalar(@$self) && not defined $self->[0][1];
230 }
231  
232 sub _clean_back {
233 my $self = shift;
234 pop @$self while scalar(@$self) && not defined $self->[-1][1];
235 }
236  
237 =head1 AUTHOR
238  
239 John Macdonald - Boutros Lab
240  
241 =head1 ACKNOWLEDGEMENTS
242  
243 Paul Boutros, Phd, PI - Boutros Lab
244  
245 The Ontario Institute for Cancer Research
246  
247 =cut
248  
249 1;