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