]> Sergey Matveev's repositories - public-inbox.git/blob - lib/PublicInbox/DS.pm
avoid calling waitpid from children in DESTROY
[public-inbox.git] / lib / PublicInbox / DS.pm
1 # This library is free software; you can redistribute it and/or modify
2 # it under the same terms as Perl itself.
3 #
4 # This license differs from the rest of public-inbox
5 #
6 # This is a fork of the unmaintained Danga::Socket (1.61) with
7 # significant changes.  See Documentation/technical/ds.txt in our
8 # source for details.
9 #
10 # Do not expect this to be a stable API like Danga::Socket,
11 # but it will evolve to suite our needs and to take advantage of
12 # newer Linux and *BSD features.
13 # Bugs encountered were reported to bug-Danga-Socket@rt.cpan.org,
14 # fixed in Danga::Socket 1.62 and visible at:
15 # https://rt.cpan.org/Public/Dist/Display.html?Name=Danga-Socket
16 #
17 # fields:
18 # sock: underlying socket
19 # rbuf: scalarref, usually undef
20 # wbuf: arrayref of coderefs or tmpio (autovivified))
21 #        (tmpio = [ GLOB, offset, [ length ] ])
22 package PublicInbox::DS;
23 use strict;
24 use v5.10.1;
25 use parent qw(Exporter);
26 use bytes;
27 use POSIX qw(WNOHANG);
28 use IO::Handle qw();
29 use Fcntl qw(SEEK_SET :DEFAULT O_APPEND);
30 use Time::HiRes qw(clock_gettime CLOCK_MONOTONIC);
31 use Scalar::Util qw(blessed);
32 use PublicInbox::Syscall qw(:epoll);
33 use PublicInbox::Tmpfile;
34 use Errno qw(EAGAIN EINVAL);
35 use Carp qw(confess carp);
36 our @EXPORT_OK = qw(now msg_more dwaitpid);
37
38 my $nextq; # queue for next_tick
39 my $wait_pids; # list of [ pid, callback, callback_arg ]
40 my $later_queue; # list of callbacks to run at some later interval
41 my $EXPMAP; # fd -> idle_time
42 our $EXPTIME = 180; # 3 minutes
43 my ($later_timer, $reap_armed, $exp_timer);
44 my $ToClose; # sockets to close when event loop is done
45 our (
46      %DescriptorMap,             # fd (num) -> PublicInbox::DS object
47      $Epoll,                     # Global epoll fd (or DSKQXS ref)
48      $_io,                       # IO::Handle for Epoll
49
50      $PostLoopCallback,          # subref to call at the end of each loop, if defined (global)
51
52      $LoopTimeout,               # timeout of event loop in milliseconds
53      @Timers,                    # timers
54      $in_loop,
55      );
56
57 Reset();
58
59 #####################################################################
60 ### C L A S S   M E T H O D S
61 #####################################################################
62
63 =head2 C<< CLASS->Reset() >>
64
65 Reset all state
66
67 =cut
68 sub Reset {
69     %DescriptorMap = ();
70     $in_loop = $wait_pids = $later_queue = $reap_armed = undef;
71     $EXPMAP = {};
72     $nextq = $ToClose = $later_timer = $exp_timer = undef;
73     $LoopTimeout = -1;  # no timeout by default
74     @Timers = ();
75
76     $PostLoopCallback = undef;
77
78     $_io = undef; # closes real $Epoll FD
79     $Epoll = undef; # may call DSKQXS::DESTROY
80 }
81
82 =head2 C<< CLASS->SetLoopTimeout( $timeout ) >>
83
84 Set the loop timeout for the event loop to some value in milliseconds.
85
86 A timeout of 0 (zero) means poll forever. A timeout of -1 means poll and return
87 immediately.
88
89 =cut
90 sub SetLoopTimeout { $LoopTimeout = $_[1] + 0 }
91
92 =head2 C<< PublicInbox::DS::add_timer( $seconds, $coderef, $arg) >>
93
94 Add a timer to occur $seconds from now. $seconds may be fractional, but timers
95 are not guaranteed to fire at the exact time you ask for.
96
97 =cut
98 sub add_timer ($$;$) {
99     my ($secs, $coderef, $arg) = @_;
100
101     my $fire_time = now() + $secs;
102
103     my $timer = [$fire_time, $coderef, $arg];
104
105     if (!@Timers || $fire_time >= $Timers[-1][0]) {
106         push @Timers, $timer;
107         return $timer;
108     }
109
110     # Now, where do we insert?  (NOTE: this appears slow, algorithm-wise,
111     # but it was compared against calendar queues, heaps, naive push/sort,
112     # and a bunch of other versions, and found to be fastest with a large
113     # variety of datasets.)
114     for (my $i = 0; $i < @Timers; $i++) {
115         if ($Timers[$i][0] > $fire_time) {
116             splice(@Timers, $i, 0, $timer);
117             return $timer;
118         }
119     }
120
121     die "Shouldn't get here.";
122 }
123
124 # keeping this around in case we support other FD types for now,
125 # epoll_create1(EPOLL_CLOEXEC) requires Linux 2.6.27+...
126 sub set_cloexec ($) {
127     my ($fd) = @_;
128
129     $_io = IO::Handle->new_from_fd($fd, 'r+') or return;
130     defined(my $fl = fcntl($_io, F_GETFD, 0)) or return;
131     fcntl($_io, F_SETFD, $fl | FD_CLOEXEC);
132 }
133
134 # caller sets return value to $Epoll
135 sub _InitPoller
136 {
137     if (PublicInbox::Syscall::epoll_defined())  {
138         my $fd = epoll_create();
139         set_cloexec($fd) if (defined($fd) && $fd >= 0);
140         $fd;
141     } else {
142         my $cls;
143         for (qw(DSKQXS DSPoll)) {
144             $cls = "PublicInbox::$_";
145             last if eval "require $cls";
146         }
147         $cls->import(qw(epoll_ctl epoll_wait));
148         $cls->new;
149     }
150 }
151
152 =head2 C<< CLASS->EventLoop() >>
153
154 Start processing IO events. In most daemon programs this never exits. See
155 C<PostLoopCallback> below for how to exit the loop.
156
157 =cut
158
159 sub now () { clock_gettime(CLOCK_MONOTONIC) }
160
161 sub next_tick () {
162     my $q = $nextq or return;
163     $nextq = undef;
164     for (@$q) {
165         # we avoid "ref" on blessed refs to workaround a Perl 5.16.3 leak:
166         # https://rt.perl.org/Public/Bug/Display.html?id=114340
167         if (blessed($_)) {
168             $_->event_step;
169         } else {
170             $_->();
171         }
172     }
173 }
174
175 # runs timers and returns milliseconds for next one, or next event loop
176 sub RunTimers {
177     next_tick();
178
179     return (($nextq || $ToClose) ? 0 : $LoopTimeout) unless @Timers;
180
181     my $now = now();
182
183     # Run expired timers
184     while (@Timers && $Timers[0][0] <= $now) {
185         my $to_run = shift(@Timers);
186         $to_run->[1]->($to_run->[2]);
187     }
188
189     # timers may enqueue into nextq:
190     return 0 if ($nextq || $ToClose);
191
192     return $LoopTimeout unless @Timers;
193
194     # convert time to an even number of milliseconds, adding 1
195     # extra, otherwise floating point fun can occur and we'll
196     # call RunTimers like 20-30 times, each returning a timeout
197     # of 0.0000212 seconds
198     my $timeout = int(($Timers[0][0] - $now) * 1000) + 1;
199
200     # -1 is an infinite timeout, so prefer a real timeout
201     ($LoopTimeout < 0 || $LoopTimeout >= $timeout) ? $timeout : $LoopTimeout;
202 }
203
204 # We can't use waitpid(-1) safely here since it can hit ``, system(),
205 # and other things.  So we scan the $wait_pids list, which is hopefully
206 # not too big.  We keep $wait_pids small by not calling dwaitpid()
207 # until we've hit EOF when reading the stdout of the child.
208
209 sub reap_pids {
210         $reap_armed = undef;
211         my $tmp = $wait_pids or return;
212         $wait_pids = undef;
213         foreach my $ary (@$tmp) {
214                 my ($pid, $cb, $arg) = @$ary;
215                 my $ret = waitpid($pid, WNOHANG);
216                 if ($ret == 0) {
217                         push @$wait_pids, $ary; # autovivifies @$wait_pids
218                 } elsif ($ret == $pid) {
219                         if ($cb) {
220                                 eval { $cb->($arg, $pid) };
221                                 warn "E: dwaitpid($pid) in_loop: $@" if $@;
222                         }
223                 } else {
224                         warn "waitpid($pid, WNOHANG) = $ret, \$!=$!, \$?=$?";
225                 }
226         }
227         # we may not be done, yet, and could've missed/masked a SIGCHLD:
228         $reap_armed //= requeue(\&reap_pids) if $wait_pids;
229 }
230
231 # reentrant SIGCHLD handler (since reap_pids is not reentrant)
232 sub enqueue_reap () { $reap_armed //= requeue(\&reap_pids) }
233
234 sub in_loop () { $in_loop }
235
236 # Internal function: run the post-event callback, send read events
237 # for pushed-back data, and close pending connections.  returns 1
238 # if event loop should continue, or 0 to shut it all down.
239 sub PostEventLoop () {
240         # now we can close sockets that wanted to close during our event
241         # processing.  (we didn't want to close them during the loop, as we
242         # didn't want fd numbers being reused and confused during the event
243         # loop)
244         if (my $close_now = $ToClose) {
245                 $ToClose = undef; # will be autovivified on push
246                 @$close_now = map { fileno($_) } @$close_now;
247
248                 # order matters, destroy expiry times, first:
249                 delete @$EXPMAP{@$close_now};
250
251                 # ->DESTROY methods may populate ToClose
252                 delete @DescriptorMap{@$close_now};
253         }
254
255         # by default we keep running, unless a postloop callback cancels it
256         $PostLoopCallback ? $PostLoopCallback->(\%DescriptorMap) : 1;
257 }
258
259 sub EventLoop {
260     $Epoll //= _InitPoller();
261     local $in_loop = 1;
262     my @events;
263     do {
264         my $timeout = RunTimers();
265
266         # get up to 1000 events
267         epoll_wait($Epoll, 1000, $timeout, \@events);
268         for my $fd (@events) {
269             # it's possible epoll_wait returned many events, including some at the end
270             # that ones in the front triggered unregister-interest actions.  if we
271             # can't find the %sock entry, it's because we're no longer interested
272             # in that event.
273             $DescriptorMap{$fd}->event_step;
274         }
275     } while (PostEventLoop());
276     _run_later();
277 }
278
279 =head2 C<< CLASS->SetPostLoopCallback( CODEREF ) >>
280
281 Sets post loop callback function.  Pass a subref and it will be
282 called every time the event loop finishes.
283
284 Return 1 (or any true value) from the sub to make the loop continue, 0 or false
285 and it will exit.
286
287 The callback function will be passed two parameters: \%DescriptorMap
288
289 =cut
290 sub SetPostLoopCallback {
291     my ($class, $ref) = @_;
292
293     # global callback
294     $PostLoopCallback = (defined $ref && ref $ref eq 'CODE') ? $ref : undef;
295 }
296
297 #####################################################################
298 ### PublicInbox::DS-the-object code
299 #####################################################################
300
301 =head2 OBJECT METHODS
302
303 =head2 C<< CLASS->new( $socket ) >>
304
305 Create a new PublicInbox::DS subclass object for the given I<socket> which will
306 react to events on it during the C<EventLoop>.
307
308 This is normally (always?) called from your subclass via:
309
310   $class->SUPER::new($socket);
311
312 =cut
313 sub new {
314     my ($self, $sock, $ev) = @_;
315     $self->{sock} = $sock;
316     my $fd = fileno($sock);
317
318     $Epoll //= _InitPoller();
319 retry:
320     if (epoll_ctl($Epoll, EPOLL_CTL_ADD, $fd, $ev)) {
321         if ($! == EINVAL && ($ev & EPOLLEXCLUSIVE)) {
322             $ev &= ~EPOLLEXCLUSIVE;
323             goto retry;
324         }
325         die "couldn't add epoll watch for $fd: $!\n";
326     }
327     confess("DescriptorMap{$fd} defined ($DescriptorMap{$fd})")
328         if defined($DescriptorMap{$fd});
329
330     $DescriptorMap{$fd} = $self;
331 }
332
333
334 #####################################################################
335 ### I N S T A N C E   M E T H O D S
336 #####################################################################
337
338 sub requeue ($) { push @$nextq, $_[0] } # autovivifies
339
340 =head2 C<< $obj->close >>
341
342 Close the socket.
343
344 =cut
345 sub close {
346     my ($self) = @_;
347     my $sock = delete $self->{sock} or return;
348
349     # we need to flush our write buffer, as there may
350     # be self-referential closures (sub { $client->close })
351     # preventing the object from being destroyed
352     delete $self->{wbuf};
353
354     # if we're using epoll, we have to remove this from our epoll fd so we stop getting
355     # notifications about it
356     my $fd = fileno($sock);
357     epoll_ctl($Epoll, EPOLL_CTL_DEL, $fd, 0) and
358         confess("EPOLL_CTL_DEL: $!");
359
360     # we explicitly don't delete from DescriptorMap here until we
361     # actually close the socket, as we might be in the middle of
362     # processing an epoll_wait/etc that returned hundreds of fds, one
363     # of which is not yet processed and is what we're closing.  if we
364     # keep it in DescriptorMap, then the event harnesses can just
365     # looked at $pob->{sock} == undef and ignore it.  but if it's an
366     # un-accounted for fd, then it (understandably) freak out a bit
367     # and emit warnings, thinking their state got off.
368
369     # defer closing the actual socket until the event loop is done
370     # processing this round of events.  (otherwise we might reuse fds)
371     push @$ToClose, $sock; # autovivifies $ToClose
372
373     return 0;
374 }
375
376 # portable, non-thread-safe sendfile emulation (no pread, yet)
377 sub send_tmpio ($$) {
378     my ($sock, $tmpio) = @_;
379
380     sysseek($tmpio->[0], $tmpio->[1], SEEK_SET) or return;
381     my $n = $tmpio->[2] // 65536;
382     $n = 65536 if $n > 65536;
383     defined(my $to_write = sysread($tmpio->[0], my $buf, $n)) or return;
384     my $written = 0;
385     while ($to_write > 0) {
386         if (defined(my $w = syswrite($sock, $buf, $to_write, $written))) {
387             $written += $w;
388             $to_write -= $w;
389         } else {
390             return if $written == 0;
391             last;
392         }
393     }
394     $tmpio->[1] += $written; # offset
395     $tmpio->[2] -= $written if defined($tmpio->[2]); # length
396     $written;
397 }
398
399 sub epbit ($$) { # (sock, default)
400         $_[0]->can('stop_SSL') ? PublicInbox::TLS::epollbit() : $_[1];
401 }
402
403 # returns 1 if done, 0 if incomplete
404 sub flush_write ($) {
405     my ($self) = @_;
406     my $sock = $self->{sock} or return;
407     my $wbuf = $self->{wbuf} or return 1;
408
409 next_buf:
410     while (my $bref = $wbuf->[0]) {
411         if (ref($bref) ne 'CODE') {
412             while ($sock) {
413                 my $w = send_tmpio($sock, $bref); # bref is tmpio
414                 if (defined $w) {
415                     if ($w == 0) {
416                         shift @$wbuf;
417                         goto next_buf;
418                     }
419                 } elsif ($! == EAGAIN) {
420                     my $ev = epbit($sock, EPOLLOUT) or return $self->close;
421                     epwait($sock, $ev | EPOLLONESHOT);
422                     return 0;
423                 } else {
424                     return $self->close;
425                 }
426             }
427         } else { #(ref($bref) eq 'CODE') {
428             shift @$wbuf;
429             my $before = scalar(@$wbuf);
430             $bref->($self);
431
432             # bref may be enqueueing more CODE to call (see accept_tls_step)
433             return 0 if (scalar(@$wbuf) > $before);
434         }
435     } # while @$wbuf
436
437     delete $self->{wbuf};
438     1; # all done
439 }
440
441 sub rbuf_idle ($$) {
442     my ($self, $rbuf) = @_;
443     if ($$rbuf eq '') { # who knows how long till we can read again
444         delete $self->{rbuf};
445     } else {
446         $self->{rbuf} = $rbuf;
447     }
448 }
449
450 sub do_read ($$$;$) {
451     my ($self, $rbuf, $len, $off) = @_;
452     my $r = sysread(my $sock = $self->{sock}, $$rbuf, $len, $off // 0);
453     return ($r == 0 ? $self->close : $r) if defined $r;
454     # common for clients to break connections without warning,
455     # would be too noisy to log here:
456     if ($! == EAGAIN) {
457         my $ev = epbit($sock, EPOLLIN) or return $self->close;
458         epwait($sock, $ev | EPOLLONESHOT);
459         rbuf_idle($self, $rbuf);
460         0;
461     } else {
462         $self->close;
463     }
464 }
465
466 # drop the socket if we hit unrecoverable errors on our system which
467 # require BOFH attention: ENOSPC, EFBIG, EIO, EMFILE, ENFILE...
468 sub drop {
469     my $self = shift;
470     carp(@_);
471     $self->close;
472 }
473
474 # n.b.: use ->write/->read for this buffer to allow compatibility with
475 # PerlIO::mmap or PerlIO::scalar if needed
476 sub tmpio ($$$) {
477     my ($self, $bref, $off) = @_;
478     my $fh = tmpfile('wbuf', $self->{sock}, O_APPEND) or
479         return drop($self, "tmpfile $!");
480     $fh->autoflush(1);
481     my $len = bytes::length($$bref) - $off;
482     $fh->write($$bref, $len, $off) or return drop($self, "write ($len): $!");
483     [ $fh, 0 ] # [1] = offset, [2] = length, not set by us
484 }
485
486 =head2 C<< $obj->write( $data ) >>
487
488 Write the specified data to the underlying handle.  I<data> may be scalar,
489 scalar ref, code ref (to run when there).
490 Returns 1 if writes all went through, or 0 if there are writes in queue. If
491 it returns 1, caller should stop waiting for 'writable' events)
492
493 =cut
494 sub write {
495     my ($self, $data) = @_;
496
497     # nobody should be writing to closed sockets, but caller code can
498     # do two writes within an event, have the first fail and
499     # disconnect the other side (whose destructor then closes the
500     # calling object, but it's still in a method), and then the
501     # now-dead object does its second write.  that is this case.  we
502     # just lie and say it worked.  it'll be dead soon and won't be
503     # hurt by this lie.
504     my $sock = $self->{sock} or return 1;
505     my $ref = ref $data;
506     my $bref = $ref ? $data : \$data;
507     my $wbuf = $self->{wbuf};
508     if ($wbuf && scalar(@$wbuf)) { # already buffering, can't write more...
509         if ($ref eq 'CODE') {
510             push @$wbuf, $bref;
511         } else {
512             my $tmpio = $wbuf->[-1];
513             if ($tmpio && !defined($tmpio->[2])) { # append to tmp file buffer
514                 $tmpio->[0]->print($$bref) or return drop($self, "print: $!");
515             } else {
516                 my $tmpio = tmpio($self, $bref, 0) or return 0;
517                 push @$wbuf, $tmpio;
518             }
519         }
520         return 0;
521     } elsif ($ref eq 'CODE') {
522         $bref->($self);
523         return 1;
524     } else {
525         my $to_write = bytes::length($$bref);
526         my $written = syswrite($sock, $$bref, $to_write);
527
528         if (defined $written) {
529             return 1 if $written == $to_write;
530             requeue($self); # runs: event_step -> flush_write
531         } elsif ($! == EAGAIN) {
532             my $ev = epbit($sock, EPOLLOUT) or return $self->close;
533             epwait($sock, $ev | EPOLLONESHOT);
534             $written = 0;
535         } else {
536             return $self->close;
537         }
538
539         # deal with EAGAIN or partial write:
540         my $tmpio = tmpio($self, $bref, $written) or return 0;
541
542         # wbuf may be an empty array if we're being called inside
543         # ->flush_write via CODE bref:
544         push @{$self->{wbuf}}, $tmpio; # autovivifies
545         return 0;
546     }
547 }
548
549 use constant MSG_MORE => ($^O eq 'linux') ? 0x8000 : 0;
550
551 sub msg_more ($$) {
552     my $self = $_[0];
553     my $sock = $self->{sock} or return 1;
554     my $wbuf = $self->{wbuf};
555
556     if (MSG_MORE && (!defined($wbuf) || !scalar(@$wbuf)) &&
557                 !$sock->can('stop_SSL')) {
558         my $n = send($sock, $_[1], MSG_MORE);
559         if (defined $n) {
560             my $nlen = bytes::length($_[1]) - $n;
561             return 1 if $nlen == 0; # all done!
562             # queue up the unwritten substring:
563             my $tmpio = tmpio($self, \($_[1]), $n) or return 0;
564             push @{$self->{wbuf}}, $tmpio; # autovivifies
565             epwait($sock, EPOLLOUT|EPOLLONESHOT);
566             return 0;
567         }
568     }
569
570     # don't redispatch into NNTPdeflate::write
571     PublicInbox::DS::write($self, \($_[1]));
572 }
573
574 sub epwait ($$) {
575     my ($sock, $ev) = @_;
576     epoll_ctl($Epoll, EPOLL_CTL_MOD, fileno($sock), $ev) and
577         confess("EPOLL_CTL_MOD $!");
578 }
579
580 # return true if complete, false if incomplete (or failure)
581 sub accept_tls_step ($) {
582     my ($self) = @_;
583     my $sock = $self->{sock} or return;
584     return 1 if $sock->accept_SSL;
585     return $self->close if $! != EAGAIN;
586     my $ev = PublicInbox::TLS::epollbit() or return $self->close;
587     epwait($sock, $ev | EPOLLONESHOT);
588     unshift(@{$self->{wbuf}}, \&accept_tls_step); # autovivifies
589     0;
590 }
591
592 # return true if complete, false if incomplete (or failure)
593 sub shutdn_tls_step ($) {
594     my ($self) = @_;
595     my $sock = $self->{sock} or return;
596     return $self->close if $sock->stop_SSL(SSL_fast_shutdown => 1);
597     return $self->close if $! != EAGAIN;
598     my $ev = PublicInbox::TLS::epollbit() or return $self->close;
599     epwait($sock, $ev | EPOLLONESHOT);
600     unshift(@{$self->{wbuf}}, \&shutdn_tls_step); # autovivifies
601     0;
602 }
603
604 # don't bother with shutdown($sock, 2), we don't fork+exec w/o CLOEXEC
605 # or fork w/o exec, so no inadvertent socket sharing
606 sub shutdn ($) {
607     my ($self) = @_;
608     my $sock = $self->{sock} or return;
609     if ($sock->can('stop_SSL')) {
610         shutdn_tls_step($self);
611     } else {
612         $self->close;
613     }
614 }
615
616 sub dwaitpid ($;$$) {
617         my ($pid, $cb, $arg) = @_;
618         if ($in_loop) {
619                 push @$wait_pids, [ $pid, $cb, $arg ];
620                 # We could've just missed our SIGCHLD, cover it, here:
621                 enqueue_reap();
622         } else {
623                 my $ret = waitpid($pid, 0);
624                 if ($ret == $pid) {
625                         if ($cb) {
626                                 eval { $cb->($arg, $pid) };
627                                 carp "E: dwaitpid($pid) !in_loop: $@" if $@;
628                         }
629                 } else {
630                         carp "waitpid($pid, 0) = $ret, \$!=$!, \$?=$?";
631                 }
632         }
633 }
634
635 sub _run_later () {
636         my $run = $later_queue or return;
637         $later_timer = $later_queue = undef;
638         $_->() for @$run;
639 }
640
641 sub later ($) {
642         push @$later_queue, $_[0]; # autovivifies @$later_queue
643         $later_timer //= add_timer(60, \&_run_later);
644 }
645
646 sub expire_old () {
647         my $now = now();
648         my $exp = $EXPTIME;
649         my $old = $now - $exp;
650         my %new;
651         while (my ($fd, $idle_at) = each %$EXPMAP) {
652                 if ($idle_at < $old) {
653                         my $ds_obj = $DescriptorMap{$fd};
654                         $new{$fd} = $idle_at if !$ds_obj->shutdn;
655                 } else {
656                         $new{$fd} = $idle_at;
657                 }
658         }
659         $EXPMAP = \%new;
660         $exp_timer = scalar(keys %new) ? later(\&expire_old) : undef;
661 }
662
663 sub update_idle_time {
664         my ($self) = @_;
665         my $sock = $self->{sock} or return;
666         $EXPMAP->{fileno($sock)} = now();
667         $exp_timer //= later(\&expire_old);
668 }
669
670 sub not_idle_long {
671         my ($self, $now) = @_;
672         my $sock = $self->{sock} or return;
673         my $idle_at = $EXPMAP->{fileno($sock)} or return;
674         ($idle_at + $EXPTIME) > $now;
675 }
676
677 1;
678
679 =head1 AUTHORS (Danga::Socket)
680
681 Brad Fitzpatrick <brad@danga.com> - author
682
683 Michael Granger <ged@danga.com> - docs, testing
684
685 Mark Smith <junior@danga.com> - contributor, heavy user, testing
686
687 Matt Sergeant <matt@sergeant.org> - kqueue support, docs, timers, other bits