| 1 | ######################################################################## | ||
| 2 | # housekeeping | ||
| 3 | ######################################################################## | ||
| 4 | package LinkedList::Single::Example::Timer v2.0.0; | ||
| 5 | use v5.40; | ||
| 6 | |||
| 7 | use Carp qw( croak ); | ||
| 8 | |||
| 9 | use LinkedList::Single qw( v2 ); | ||
| 10 | |||
| 11 | ######################################################################## | ||
| 12 | # a queue of messages, each with the time it may start, in start order, | ||
| 13 | # held in a linked list that begins with a permanent anchor node: | ||
| 14 | # | ||
| 15 | # anchor -> m1 -> m2 -> ... -> last -> sentinel | ||
| 16 | # | ||
| 17 | # every message is a node [ $start, @msg ]; the anchor holds -inf. | ||
| 18 | # | ||
| 19 | # two cursors are kept beside the list: | ||
| 20 | # | ||
| 21 | # anchor the first node, never removed. it gives the first message a | ||
| 22 | # predecessor, which is what detach and append_to need. | ||
| 23 | # last the last node (the anchor when the queue is empty). the | ||
| 24 | # sentinel after it is where the next message is pushed, so a | ||
| 25 | # message that starts no earlier than the last one never walks. | ||
| 26 | # | ||
| 27 | # the dispatcher works in rounds: | ||
| 28 | # | ||
| 29 | # take( $now ) cut every message with start <= $now off the | ||
| 30 | # front, as one Fragment: the batch. | ||
| 31 | # process( $batch ) hand the batch's messages to a handler, in | ||
| 32 | # order, until the window closes or the batch is | ||
| 33 | # empty. the handler is called without any hold | ||
| 34 | # on the queue: the batch belongs to the caller. | ||
| 35 | # requeue( $batch ) whatever the window cut off goes back in front | ||
| 36 | # of the queue. | ||
| 37 | # | ||
| 38 | # the batch is a Fragment, so putting its unused part back in front of | ||
| 39 | # the queue is a relink of that part and of nothing else. the length of | ||
| 40 | # the queue behind it does not enter into the cost. the length of the | ||
| 41 | # part being put back does: attach_nodes walks the Fragment to find its | ||
| 42 | # tail (the POD has the numbers, and the module change that would make | ||
| 43 | # that O(1)). | ||
| 44 | # | ||
| 45 | # a batch is in start order and every message in it starts no later than | ||
| 46 | # the cut, which is no later than anything left in the queue. putting it | ||
| 47 | # back in front keeps the queue in order unless a message with a start | ||
| 48 | # time in the past was enqueued while the batch was away. requeue checks | ||
| 49 | # for that (the batch's latest start against the head's), and merges the | ||
| 50 | # batch item by item when the order would break. | ||
| 51 | # | ||
| 52 | # the stored cursors are only stepped off of, never advanced, and are | ||
| 53 | # only used with operations that leave every node where it is: detach, | ||
| 54 | # append_to, push, append and drop. | ||
| 55 | ######################################################################## | ||
| 56 | |||
| 57 | my $before_all = -9**9**9; | ||
| 58 | |||
| 59 | sub new( $class, %arg ) | ||
| 60 | { | ||
| 61 | my $list = LinkedList::Single->new; | ||
| 62 | |||
| 63 | # an empty list: push fills the sentinel in place. | ||
| 64 | |||
| 65 | $list->push( $before_all ); | ||
| 66 | |||
| 67 | my $anchor = $list->cursor; | ||
| 68 | |||
| 69 | bless | ||
| 70 | { | ||
| 71 | clock => $arg{ clock } // sub { time } | ||
| 72 | , window => $arg{ window } // 60 | ||
| 73 | , list => $list | ||
| 74 | , anchor => $anchor | ||
| 75 | , last => $anchor | ||
| 76 | , latest => $before_all | ||
| 77 | , size => 0 | ||
| 78 | |||
| 79 | # statistics. | ||
| 80 | # | ||
| 81 | # walked nodes this module's code stepped over to find a place | ||
| 82 | # for an out-of-order message, or for the cut | ||
| 83 | # merged messages put back one at a time because of a late | ||
| 84 | # arrival | ||
| 85 | |||
| 86 | , walked => 0 | ||
| 87 | , merged => 0 | ||
| 88 | } | ||
| 89 | => $class | ||
| 90 | } | ||
| 91 | |||
| 92 | ######################################################################## | ||
| 93 | # public interface | ||
| 94 | ######################################################################## | ||
| 95 | |||
| 96 | # queue one message that may start at $start. | ||
| 97 | |||
| 98 | sub enqueue( $self, $start, @msg ) | ||
| 99 | { | ||
| 100 | $self->_insert( $start, @msg ); | ||
| 101 | |||
| 102 | ++$self->{ size }; | ||
| 103 | |||
| 104 | $self | ||
| 105 | } | ||
| 106 | |||
| 107 | # cut the messages that are due off the front of the queue. returns a | ||
| 108 | # batch, or undef if nothing is due. | ||
| 109 | |||
| 110 | sub take( $self, $now = $self->{ clock }->() ) | ||
| 111 | { | ||
| 112 | my $anchor = $self->{ anchor }; | ||
| 113 | my $cursor = $anchor->next( 0 ); | ||
| 114 | my $count = 0; | ||
| 115 | my $latest; | ||
| 116 | |||
| 117 | # the walk is the only look at the start times, and it stops at the | ||
| 118 | # first message that is not due. | ||
| 119 | |||
| 120 | for(;;) | ||
| 121 | { | ||
| 122 | my( $start ) = $cursor->next_data; | ||
| 123 | |||
| 124 | defined $start | ||
| 125 | && $start <= $now | ||
| 126 | or last; | ||
| 127 | |||
| 128 | $latest = $start; | ||
| 129 | |||
| 130 | $cursor->advance; | ||
| 131 | ++$count; | ||
| 132 | } | ||
| 133 | |||
| 134 | $self->{ walked } += $count; | ||
| 135 | |||
| 136 | $count | ||
| 137 | or return undef; | ||
| 138 | |||
| 139 | my $frag = $anchor->detach( $count ); | ||
| 140 | |||
| 141 | # nothing left behind the cut: the queue is empty again. ($cursor | ||
| 142 | # now stands on the last node of the batch, whose next is the | ||
| 143 | # batch's own sentinel, so it is the anchor that is asked.) | ||
| 144 | |||
| 145 | $anchor->next | ||
| 146 | or $self->_reset; | ||
| 147 | |||
| 148 | $self->{ size } -= $count; | ||
| 149 | |||
| 150 | return | ||
| 151 | { | ||
| 152 | frag => $frag | ||
| 153 | , latest => $latest | ||
| 154 | , left => $count | ||
| 155 | }; | ||
| 156 | } | ||
| 157 | |||
| 158 | # hand the batch to $handler, one message at a time, until the clock | ||
| 159 | # reaches $deadline (the default is a window from now), or the batch is | ||
| 160 | # used up. the handler gets ( $start, @msg ). returns the number | ||
| 161 | # dispatched. | ||
| 162 | # | ||
| 163 | # consume ends quietly on die "\n" and leaves the batch standing on the | ||
| 164 | # message it did not dispatch. any other exception is carped and also | ||
| 165 | # stops the batch there. | ||
| 166 | |||
| 167 | sub process( $self, $batch, $handler, $deadline = undef ) | ||
| 168 | { | ||
| 169 | $batch | ||
| 170 | or return 0; | ||
| 171 | |||
| 172 | my $clock = $self->{ clock }; | ||
| 173 | |||
| 174 | $deadline //= $clock->() + $self->{ window }; | ||
| 175 | |||
| 176 | my $frag = $batch->{ frag }; | ||
| 177 | my $done = 0; | ||
| 178 | |||
| 179 | $frag | ||
| 180 | or return 0; | ||
| 181 | |||
| 182 | $frag->consume | ||
| 183 | ( | ||
| 184 | sub( @item ) | ||
| 185 | { | ||
| 186 | $clock->() >= $deadline | ||
| 187 | and die "\n"; | ||
| 188 | |||
| 189 | $handler->( @item ); | ||
| 190 | |||
| 191 | ++$done; | ||
| 192 | } | ||
| 193 | ); | ||
| 194 | |||
| 195 | $batch->{ left } -= $done; | ||
| 196 | |||
| 197 | $done | ||
| 198 | } | ||
| 199 | |||
| 200 | # messages left in a batch. | ||
| 201 | |||
| 202 | sub left( $self, $batch ) | ||
| 203 | { | ||
| 204 | $batch | ||
| 205 | ? $batch->{ left } | ||
| 206 | : 0 | ||
| 207 | } | ||
| 208 | |||
| 209 | # put the unused part of a batch back in front of the queue. returns the | ||
| 210 | # number of messages put back. | ||
| 211 | |||
| 212 | sub requeue( $self, $batch ) | ||
| 213 | { | ||
| 214 | $batch | ||
| 215 | or return 0; | ||
| 216 | |||
| 217 | my $left = $batch->{ left } | ||
| 218 | or return 0; | ||
| 219 | |||
| 220 | my $frag = $batch->{ frag }; | ||
| 221 | my $anchor = $self->{ anchor }; | ||
| 222 | |||
| 223 | # the first message of what is queued now. | ||
| 224 | |||
| 225 | my( $head ) = $anchor->next_data; | ||
| 226 | |||
| 227 | if( ! defined $head ) | ||
| 228 | { | ||
| 229 | # the queue is empty: the batch is all of it. the cursor on its | ||
| 230 | # last node is made first, while it is still a Fragment. | ||
| 231 | |||
| 232 | $self->{ last } = $frag->tail; | ||
| 233 | $self->{ latest } = $batch->{ latest }; | ||
| 234 | |||
| 235 | $frag->append_to( $anchor ); | ||
| 236 | } | ||
| 237 | elsif( $batch->{ latest } <= $head ) | ||
| 238 | { | ||
| 239 | # the cheap case: in front, and in order. | ||
| 240 | |||
| 241 | $frag->append_to( $anchor ); | ||
| 242 | } | ||
| 243 | else | ||
| 244 | { | ||
| 245 | # a message that is due came in while the batch was out. the | ||
| 246 | # batch goes back one message at a time, each in its place. | ||
| 247 | |||
| 248 | $frag->consume( sub( @item ) { $self->_insert( @item ) } ); | ||
| 249 | |||
| 250 | $self->{ merged } += $left; | ||
| 251 | } | ||
| 252 | |||
| 253 | $self->{ size } += $left; | ||
| 254 | $batch->{ left } = 0; | ||
| 255 | |||
| 256 | $left | ||
| 257 | } | ||
| 258 | |||
| 259 | ######################################################################## | ||
| 260 | # utility | ||
| 261 | ######################################################################## | ||
| 262 | |||
| 263 | # the queue is empty: both cursors are as they were when it was new. | ||
| 264 | |||
| 265 | sub _reset( $self ) | ||
| 266 | { | ||
| 267 | $self->{ last } = $self->{ anchor }; | ||
| 268 | $self->{ latest } = $before_all; | ||
| 269 | |||
| 270 | $self | ||
| 271 | } | ||
| 272 | |||
| 273 | # put a message in its place. one that starts no earlier than the last | ||
| 274 | # is pushed on the end. any other is found by walking from the anchor to | ||
| 275 | # the last message that does not start after it, which leaves a cursor | ||
| 276 | # on the node it goes after. | ||
| 277 | |||
| 278 | sub _insert( $self, $start, @msg ) | ||
| 279 | { | ||
| 280 | if( $start >= $self->{ latest } ) | ||
| 281 | { | ||
| 282 | my $last = $self->{ last }; | ||
| 283 | |||
| 284 | $last->next->push( $start, @msg ); | ||
| 285 | |||
| 286 | $self->{ last } = $last->next; | ||
| 287 | $self->{ latest } = $start; | ||
| 288 | |||
| 289 | return $self | ||
| 290 | } | ||
| 291 | |||
| 292 | my $pred = $self->{ anchor }->next( 0 ); | ||
| 293 | my $walked = 0; | ||
| 294 | |||
| 295 | for(;;) | ||
| 296 | { | ||
| 297 | my( $next ) = $pred->next_data; | ||
| 298 | |||
| 299 | defined $next | ||
| 300 | && $next <= $start | ||
| 301 | or last; | ||
| 302 | |||
| 303 | $pred->advance; | ||
| 304 | ++$walked; | ||
| 305 | } | ||
| 306 | |||
| 307 | $self->{ walked } += $walked; | ||
| 308 | |||
| 309 | # the start is below the last one's, so something follows $pred and | ||
| 310 | # this does not touch the sentinel. | ||
| 311 | |||
| 312 | $pred->append( $start, @msg ); | ||
| 313 | |||
| 314 | $self | ||
| 315 | } | ||
| 316 | |||
| 317 | ######################################################################## | ||
| 318 | # introspection | ||
| 319 | ######################################################################## | ||
| 320 | |||
| 321 | # arrayref of [ $start, @msg ], earliest first. | ||
| 322 | |||
| 323 | sub contents( $self ) | ||
| 324 | { | ||
| 325 | my @item | ||
| 326 | = ( ( scalar $self->{ list }->extract ) // [] )->@*; | ||
| 327 | |||
| 328 | # the anchor is not a message. | ||
| 329 | |||
| 330 | shift @item; | ||
| 331 | |||
| 332 | \@item | ||
| 333 | } | ||
| 334 | |||
| 335 | # the start time of the first message, or undef. | ||
| 336 | |||
| 337 | sub next_start( $self ) | ||
| 338 | { | ||
| 339 | ( $self->{ anchor }->next_data )[ 0 ] | ||
| 340 | } | ||
| 341 | |||
| 342 | sub size( $self ) | ||
| 343 | { | ||
| 344 | $self->{ size } | ||
| 345 | } | ||
| 346 | |||
| 347 | sub window( $self ) | ||
| 348 | { | ||
| 349 | $self->{ window } | ||
| 350 | } | ||
| 351 | |||
| 352 | sub walked( $self ) | ||
| 353 | { | ||
| 354 | $self->{ walked } | ||
| 355 | } | ||
| 356 | |||
| 357 | sub merged( $self ) | ||
| 358 | { | ||
| 359 | $self->{ merged } | ||
| 360 | } | ||
| 361 | |||
| 362 | 1 | ||
| 363 | __END__ |