]> Sergey Matveev's repositories - public-inbox.git/blob - lib/PublicInbox/DS.pm
Merge remote-tracking branch 'origin/ds'
[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 (for now) unmaintained Danga::Socket 1.61.
7 # Unused features will be removed, and updates will be made to take
8 # advantage of newer kernels.
9 #
10 # API changes to diverge from Danga::Socket will happen to better
11 # accomodate new features and improve scalability.  Do not expect
12 # this to be a stable API like Danga::Socket.
13 # Bugs encountered (and likely fixed) are reported to
14 # bug-Danga-Socket@rt.cpan.org and visible at:
15 # https://rt.cpan.org/Public/Dist/Display.html?Name=Danga-Socket
16 package PublicInbox::DS;
17 use strict;
18 use bytes;
19 use POSIX ();
20 use Time::HiRes ();
21 use IO::Handle qw();
22 use Fcntl qw(FD_CLOEXEC F_SETFD F_GETFD);
23
24 use warnings;
25
26 use PublicInbox::Syscall qw(:epoll);
27
28 use fields ('sock',              # underlying socket
29             'wbuf',              # arrayref of scalars, scalarrefs, or coderefs to write
30             'wbuf_off',  # offset into first element of wbuf to start writing at
31             'closed',            # bool: socket is closed
32             'event_watch',       # bitmask of events the client is interested in (POLLIN,OUT,etc.)
33             );
34
35 use Errno  qw(EAGAIN EINVAL);
36 use Carp   qw(croak confess);
37
38 use constant DebugLevel => 0;
39
40 use constant POLLIN        => 1;
41 use constant POLLOUT       => 4;
42 use constant POLLERR       => 8;
43 use constant POLLHUP       => 16;
44 use constant POLLNVAL      => 32;
45
46 our $HAVE_KQUEUE = eval { require IO::KQueue; 1 };
47
48 our (
49      $HaveEpoll,                 # Flag -- is epoll available?  initially undefined.
50      $HaveKQueue,
51      %DescriptorMap,             # fd (num) -> PublicInbox::DS object
52      $Epoll,                     # Global epoll fd (for epoll mode only)
53      $KQueue,                    # Global kqueue fd ref (for kqueue mode only)
54      $_io,                       # IO::Handle for Epoll
55      @ToClose,                   # sockets to close when event loop is done
56
57      $PostLoopCallback,          # subref to call at the end of each loop, if defined (global)
58
59      $LoopTimeout,               # timeout of event loop in milliseconds
60      $DoneInit,                  # if we've done the one-time module init yet
61      @Timers,                    # timers
62      );
63
64 # this may be set to zero with old kernels
65 our $EPOLLEXCLUSIVE = EPOLLEXCLUSIVE;
66 Reset();
67
68 #####################################################################
69 ### C L A S S   M E T H O D S
70 #####################################################################
71
72 =head2 C<< CLASS->Reset() >>
73
74 Reset all state
75
76 =cut
77 sub Reset {
78     %DescriptorMap = ();
79     @ToClose = ();
80     $LoopTimeout = -1;  # no timeout by default
81     @Timers = ();
82
83     $PostLoopCallback = undef;
84     $DoneInit = 0;
85
86     # NOTE kqueue is close-on-fork, and we don't account for it, yet
87     # OTOH, we (public-inbox) don't need this sub outside of tests...
88     POSIX::close($$KQueue) if !$_io && $KQueue && $$KQueue >= 0;
89     $KQueue = undef;
90
91     $_io = undef; # close $Epoll
92     $Epoll = undef;
93
94     *EventLoop = *FirstTimeEventLoop;
95 }
96
97 =head2 C<< CLASS->SetLoopTimeout( $timeout ) >>
98
99 Set the loop timeout for the event loop to some value in milliseconds.
100
101 A timeout of 0 (zero) means poll forever. A timeout of -1 means poll and return
102 immediately.
103
104 =cut
105 sub SetLoopTimeout {
106     return $LoopTimeout = $_[1] + 0;
107 }
108
109 =head2 C<< CLASS->DebugMsg( $format, @args ) >>
110
111 Print the debugging message specified by the C<sprintf>-style I<format> and
112 I<args>
113
114 =cut
115 sub DebugMsg {
116     my ( $class, $fmt, @args ) = @_;
117     chomp $fmt;
118     printf STDERR ">>> $fmt\n", @args;
119 }
120
121 =head2 C<< CLASS->AddTimer( $seconds, $coderef ) >>
122
123 Add a timer to occur $seconds from now. $seconds may be fractional, but timers
124 are not guaranteed to fire at the exact time you ask for.
125
126 Returns a timer object which you can call C<< $timer->cancel >> on if you need to.
127
128 =cut
129 sub AddTimer {
130     my $class = shift;
131     my ($secs, $coderef) = @_;
132
133     my $fire_time = Time::HiRes::time() + $secs;
134
135     my $timer = bless [$fire_time, $coderef], "PublicInbox::DS::Timer";
136
137     if (!@Timers || $fire_time >= $Timers[-1][0]) {
138         push @Timers, $timer;
139         return $timer;
140     }
141
142     # Now, where do we insert?  (NOTE: this appears slow, algorithm-wise,
143     # but it was compared against calendar queues, heaps, naive push/sort,
144     # and a bunch of other versions, and found to be fastest with a large
145     # variety of datasets.)
146     for (my $i = 0; $i < @Timers; $i++) {
147         if ($Timers[$i][0] > $fire_time) {
148             splice(@Timers, $i, 0, $timer);
149             return $timer;
150         }
151     }
152
153     die "Shouldn't get here.";
154 }
155
156 # keeping this around in case we support other FD types for now,
157 # epoll_create1(EPOLL_CLOEXEC) requires Linux 2.6.27+...
158 sub set_cloexec ($) {
159     my ($fd) = @_;
160
161     $_io = IO::Handle->new_from_fd($fd, 'r+') or return;
162     defined(my $fl = fcntl($_io, F_GETFD, 0)) or return;
163     fcntl($_io, F_SETFD, $fl | FD_CLOEXEC);
164 }
165
166 sub _InitPoller
167 {
168     return if $DoneInit;
169     $DoneInit = 1;
170
171     if ($HAVE_KQUEUE) {
172         $KQueue = IO::KQueue->new();
173         $HaveKQueue = defined $KQueue;
174         if ($HaveKQueue) {
175             *EventLoop = *KQueueEventLoop;
176         }
177     }
178     elsif (PublicInbox::Syscall::epoll_defined()) {
179         $Epoll = eval { epoll_create(1024); };
180         $HaveEpoll = defined $Epoll && $Epoll >= 0;
181         if ($HaveEpoll) {
182             set_cloexec($Epoll);
183             *EventLoop = *EpollEventLoop;
184         }
185     }
186
187     if (!$HaveEpoll && !$HaveKQueue) {
188         require IO::Poll;
189         *EventLoop = *PollEventLoop;
190     }
191 }
192
193 =head2 C<< CLASS->EventLoop() >>
194
195 Start processing IO events. In most daemon programs this never exits. See
196 C<PostLoopCallback> below for how to exit the loop.
197
198 =cut
199 sub FirstTimeEventLoop {
200     my $class = shift;
201
202     _InitPoller();
203
204     if ($HaveEpoll) {
205         EpollEventLoop($class);
206     } elsif ($HaveKQueue) {
207         KQueueEventLoop($class);
208     } else {
209         PollEventLoop($class);
210     }
211 }
212
213 # runs timers and returns milliseconds for next one, or next event loop
214 sub RunTimers {
215     return $LoopTimeout unless @Timers;
216
217     my $now = Time::HiRes::time();
218
219     # Run expired timers
220     while (@Timers && $Timers[0][0] <= $now) {
221         my $to_run = shift(@Timers);
222         $to_run->[1]->($now) if $to_run->[1];
223     }
224
225     return $LoopTimeout unless @Timers;
226
227     # convert time to an even number of milliseconds, adding 1
228     # extra, otherwise floating point fun can occur and we'll
229     # call RunTimers like 20-30 times, each returning a timeout
230     # of 0.0000212 seconds
231     my $timeout = int(($Timers[0][0] - $now) * 1000) + 1;
232
233     # -1 is an infinite timeout, so prefer a real timeout
234     return $timeout     if $LoopTimeout == -1;
235
236     # otherwise pick the lower of our regular timeout and time until
237     # the next timer
238     return $LoopTimeout if $LoopTimeout < $timeout;
239     return $timeout;
240 }
241
242 sub event_step ($) {
243     my ($self) = @_;
244     return if $self->{closed};
245
246     my $wbuf = $self->{wbuf};
247     if (@$wbuf) {
248         $self->event_write;
249         return if $self->{closed} || scalar(@$wbuf);
250     }
251
252     # only read more requests if we've drained the write buffer,
253     # otherwise we can be buffering infinitely w/o backpressure
254     $self->event_read;
255 }
256
257 ### The epoll-based event loop. Gets installed as EventLoop if IO::Epoll loads
258 ### okay.
259 sub EpollEventLoop {
260     my $class = shift;
261
262     while (1) {
263         my @events;
264         my $i;
265         my $timeout = RunTimers();
266
267         # get up to 1000 events
268         my $evcount = epoll_wait($Epoll, 1000, $timeout, \@events);
269         for ($i=0; $i<$evcount; $i++) {
270             my $ev = $events[$i];
271
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             event_step($DescriptorMap{$ev->[0]});
277         }
278         return unless PostEventLoop();
279     }
280     exit 0;
281 }
282
283 ### The fallback IO::Poll-based event loop. Gets installed as EventLoop if
284 ### IO::Epoll fails to load.
285 sub PollEventLoop {
286     my $class = shift;
287
288     my PublicInbox::DS $pob;
289
290     while (1) {
291         my $timeout = RunTimers();
292
293         # the following sets up @poll as a series of ($poll,$event_mask)
294         # items, then uses IO::Poll::_poll, implemented in XS, which
295         # modifies the array in place with the even elements being
296         # replaced with the event masks that occured.
297         my @poll;
298         while ( my ($fd, $sock) = each %DescriptorMap ) {
299             push @poll, $fd, $sock->{event_watch};
300         }
301
302         # if nothing to poll, either end immediately (if no timeout)
303         # or just keep calling the callback
304         unless (@poll) {
305             select undef, undef, undef, ($timeout / 1000);
306             return unless PostEventLoop();
307             next;
308         }
309
310         my $count = IO::Poll::_poll($timeout, @poll);
311         unless ($count >= 0) {
312             return unless PostEventLoop();
313             next;
314         }
315
316         # Fetch handles with read events
317         while (@poll) {
318             my ($fd, $state) = splice(@poll, 0, 2);
319             next unless $state;
320
321             event_step($DescriptorMap{$fd});
322         }
323
324         return unless PostEventLoop();
325     }
326
327     exit 0;
328 }
329
330 ### The kqueue-based event loop. Gets installed as EventLoop if IO::KQueue works
331 ### okay.
332 sub KQueueEventLoop {
333     my $class = shift;
334
335     while (1) {
336         my $timeout = RunTimers();
337         my @ret = eval { $KQueue->kevent($timeout) };
338         if (my $err = $@) {
339             # workaround https://rt.cpan.org/Ticket/Display.html?id=116615
340             if ($err =~ /Interrupted system call/) {
341                 @ret = ();
342             } else {
343                 die $err;
344             }
345         }
346
347         foreach my $kev (@ret) {
348             my ($fd, $filter, $flags, $fflags) = @$kev;
349             event_step($DescriptorMap{$fd});
350         }
351         return unless PostEventLoop();
352     }
353
354     exit(0);
355 }
356
357 =head2 C<< CLASS->SetPostLoopCallback( CODEREF ) >>
358
359 Sets post loop callback function.  Pass a subref and it will be
360 called every time the event loop finishes.
361
362 Return 1 (or any true value) from the sub to make the loop continue, 0 or false
363 and it will exit.
364
365 The callback function will be passed two parameters: \%DescriptorMap
366
367 =cut
368 sub SetPostLoopCallback {
369     my ($class, $ref) = @_;
370
371     # global callback
372     $PostLoopCallback = (defined $ref && ref $ref eq 'CODE') ? $ref : undef;
373 }
374
375 # Internal function: run the post-event callback, send read events
376 # for pushed-back data, and close pending connections.  returns 1
377 # if event loop should continue, or 0 to shut it all down.
378 sub PostEventLoop {
379     # now we can close sockets that wanted to close during our event processing.
380     # (we didn't want to close them during the loop, as we didn't want fd numbers
381     #  being reused and confused during the event loop)
382     while (my $sock = shift @ToClose) {
383         my $fd = fileno($sock);
384
385         # close the socket.  (not a PublicInbox::DS close)
386         $sock->close;
387
388         # and now we can finally remove the fd from the map.  see
389         # comment above in _cleanup.
390         delete $DescriptorMap{$fd};
391     }
392
393
394     # by default we keep running, unless a postloop callback (either per-object
395     # or global) cancels it
396     my $keep_running = 1;
397
398     # now we're at the very end, call callback if defined
399     if (defined $PostLoopCallback) {
400         $keep_running &&= $PostLoopCallback->(\%DescriptorMap);
401     }
402
403     return $keep_running;
404 }
405
406 #####################################################################
407 ### PublicInbox::DS-the-object code
408 #####################################################################
409
410 =head2 OBJECT METHODS
411
412 =head2 C<< CLASS->new( $socket ) >>
413
414 Create a new PublicInbox::DS subclass object for the given I<socket> which will
415 react to events on it during the C<EventLoop>.
416
417 This is normally (always?) called from your subclass via:
418
419   $class->SUPER::new($socket);
420
421 =cut
422 sub new {
423     my ($self, $sock, $exclusive) = @_;
424     $self = fields::new($self) unless ref $self;
425
426     $self->{sock} = $sock;
427     my $fd = fileno($sock);
428
429     Carp::cluck("undef sock and/or fd in PublicInbox::DS->new.  sock=" . ($sock || "") . ", fd=" . ($fd || ""))
430         unless $sock && $fd;
431
432     $self->{wbuf} = [];
433     $self->{wbuf_off} = 0;
434     $self->{closed} = 0;
435
436     my $ev = $self->{event_watch} = POLLERR|POLLHUP|POLLNVAL;
437
438     _InitPoller();
439
440     if ($HaveEpoll) {
441         if ($exclusive) {
442             $ev = $self->{event_watch} = EPOLLIN|EPOLLERR|EPOLLHUP|$EPOLLEXCLUSIVE;
443         }
444 retry:
445         if (epoll_ctl($Epoll, EPOLL_CTL_ADD, $fd, $ev)) {
446             if ($! == EINVAL && ($ev & $EPOLLEXCLUSIVE)) {
447                 $EPOLLEXCLUSIVE = 0; # old kernel
448                 $ev = $self->{event_watch} = EPOLLIN|EPOLLERR|EPOLLHUP;
449                 goto retry;
450             }
451             die "couldn't add epoll watch for $fd: $!\n";
452         }
453     }
454     elsif ($HaveKQueue) {
455         # Add them to the queue but disabled for now
456         $KQueue->EV_SET($fd, IO::KQueue::EVFILT_READ(),
457                         IO::KQueue::EV_ADD() | IO::KQueue::EV_DISABLE());
458         $KQueue->EV_SET($fd, IO::KQueue::EVFILT_WRITE(),
459                         IO::KQueue::EV_ADD() | IO::KQueue::EV_DISABLE());
460     }
461
462     Carp::cluck("PublicInbox::DS::new blowing away existing descriptor map for fd=$fd ($DescriptorMap{$fd})")
463         if $DescriptorMap{$fd};
464
465     $DescriptorMap{$fd} = $self;
466     return $self;
467 }
468
469
470 #####################################################################
471 ### I N S T A N C E   M E T H O D S
472 #####################################################################
473
474 =head2 C<< $obj->close >>
475
476 Close the socket.
477
478 =cut
479 sub close {
480     my PublicInbox::DS $self = $_[0];
481     return if $self->{closed};
482
483     # this does most of the work of closing us
484     $self->_cleanup();
485
486     # defer closing the actual socket until the event loop is done
487     # processing this round of events.  (otherwise we might reuse fds)
488     if (my $sock = delete $self->{sock}) {
489         push @ToClose, $sock;
490     }
491
492     return 0;
493 }
494
495 ### METHOD: _cleanup()
496 ### Called by our closers so we can clean internal data structures.
497 sub _cleanup {
498     my PublicInbox::DS $self = $_[0];
499
500     # we're effectively closed; we have no fd and sock when we leave here
501     $self->{closed} = 1;
502
503     # we need to flush our write buffer, as there may
504     # be self-referential closures (sub { $client->close })
505     # preventing the object from being destroyed
506     @{$self->{wbuf}} = ();
507
508     # if we're using epoll, we have to remove this from our epoll fd so we stop getting
509     # notifications about it
510     if ($HaveEpoll && $self->{sock}) {
511         my $fd = fileno($self->{sock});
512         epoll_ctl($Epoll, EPOLL_CTL_DEL, $fd, $self->{event_watch}) and
513             confess("EPOLL_CTL_DEL: $!");
514     }
515
516     # we explicitly don't delete from DescriptorMap here until we
517     # actually close the socket, as we might be in the middle of
518     # processing an epoll_wait/etc that returned hundreds of fds, one
519     # of which is not yet processed and is what we're closing.  if we
520     # keep it in DescriptorMap, then the event harnesses can just
521     # looked at $pob->{closed} and ignore it.  but if it's an
522     # un-accounted for fd, then it (understandably) freak out a bit
523     # and emit warnings, thinking their state got off.
524 }
525
526 =head2 C<< $obj->sock() >>
527
528 Returns the underlying IO::Handle for the object.
529
530 =cut
531 sub sock {
532     my PublicInbox::DS $self = shift;
533     return $self->{sock};
534 }
535
536 =head2 C<< $obj->write( $data ) >>
537
538 Write the specified data to the underlying handle.  I<data> may be scalar,
539 scalar ref, code ref (to run when there), or undef just to kick-start.
540 Returns 1 if writes all went through, or 0 if there are writes in queue. If
541 it returns 1, caller should stop waiting for 'writable' events)
542
543 =cut
544 sub write {
545     my PublicInbox::DS $self;
546     my $data;
547     ($self, $data) = @_;
548
549     # nobody should be writing to closed sockets, but caller code can
550     # do two writes within an event, have the first fail and
551     # disconnect the other side (whose destructor then closes the
552     # calling object, but it's still in a method), and then the
553     # now-dead object does its second write.  that is this case.  we
554     # just lie and say it worked.  it'll be dead soon and won't be
555     # hurt by this lie.
556     return 1 if $self->{closed};
557
558     my $bref;
559
560     # just queue data if there's already a wait
561     my $need_queue;
562     my $wbuf = $self->{wbuf};
563
564     if (defined $data) {
565         $bref = ref $data ? $data : \$data;
566         if (scalar @$wbuf) {
567             push @$wbuf, $bref;
568             return 0;
569         }
570
571         # this flag says we're bypassing the queue system, knowing we're the
572         # only outstanding write, and hoping we don't ever need to use it.
573         # if so later, though, we'll need to queue
574         $need_queue = 1;
575     }
576
577   WRITE:
578     while (1) {
579         return 1 unless $bref ||= $wbuf->[0];
580
581         my $len;
582         eval {
583             $len = length($$bref); # this will die if $bref is a code ref, caught below
584         };
585         if ($@) {
586             if (UNIVERSAL::isa($bref, "CODE")) {
587                 unless ($need_queue) {
588                     shift @$wbuf;
589                 }
590                 $bref->();
591
592                 # code refs are just run and never get reenqueued
593                 # (they're one-shot), so turn off the flag indicating the
594                 # outstanding data needs queueing.
595                 $need_queue = 0;
596
597                 undef $bref;
598                 next WRITE;
599             }
600             die "Write error: $@ <$bref>";
601         }
602
603         my $to_write = $len - $self->{wbuf_off};
604         my $written = syswrite($self->{sock}, $$bref, $to_write,
605                                $self->{wbuf_off});
606
607         if (! defined $written) {
608             if ($! == EAGAIN) {
609                 # since connection has stuff to write, it should now be
610                 # interested in pending writes:
611                 if ($need_queue) {
612                     push @$wbuf, $bref;
613                 }
614                 $self->watch_write(1);
615                 return 0;
616             }
617
618             return $self->close;
619         } elsif ($written != $to_write) {
620             if ($need_queue) {
621                 push @$wbuf, $bref;
622             }
623             # since connection has stuff to write, it should now be
624             # interested in pending writes:
625             $self->{wbuf_off} += $written;
626             $self->on_incomplete_write;
627             return 0;
628         } elsif ($written == $to_write) {
629             $self->{wbuf_off} = 0;
630             $self->watch_write(0);
631
632             # this was our only write, so we can return immediately
633             # since we avoided incrementing the buffer size or
634             # putting it in the buffer.  we also know there
635             # can't be anything else to write.
636             return 1 if $need_queue;
637
638             shift @$wbuf;
639             undef $bref;
640             next WRITE;
641         }
642     }
643 }
644
645 sub on_incomplete_write {
646     my PublicInbox::DS $self = shift;
647     $self->watch_write(1);
648 }
649
650 =head2 (VIRTUAL) C<< $obj->event_read() >>
651
652 Readable event handler. Concrete deriviatives of PublicInbox::DS should
653 provide an implementation of this. The default implementation is a noop
654 if called.
655
656 =cut
657 sub event_read {} # noop
658
659 =head2 C<< $obj->event_write() >>
660
661 Writable event handler. Concrete deriviatives of PublicInbox::DS may wish to
662 provide an implementation of this. The default implementation calls
663 C<write()> with an C<undef>.
664
665 =cut
666 sub event_write {
667     my $self = shift;
668     $self->write(undef);
669 }
670
671 =head2 C<< $obj->watch_read( $boolean ) >>
672
673 Turn 'readable' event notification on or off.
674
675 =cut
676 sub watch_read {
677     my PublicInbox::DS $self = shift;
678     return if $self->{closed} || !$self->{sock};
679
680     my $val = shift;
681     my $event = $self->{event_watch};
682
683     $event &= ~POLLIN if ! $val;
684     $event |=  POLLIN if   $val;
685
686     my $fd = fileno($self->{sock});
687     # If it changed, set it
688     if ($event != $self->{event_watch}) {
689         if ($HaveKQueue) {
690             $KQueue->EV_SET($fd, IO::KQueue::EVFILT_READ(),
691                             $val ? IO::KQueue::EV_ENABLE() : IO::KQueue::EV_DISABLE());
692         }
693         elsif ($HaveEpoll) {
694             epoll_ctl($Epoll, EPOLL_CTL_MOD, $fd, $event) and
695                 confess("EPOLL_CTL_MOD: $!");
696         }
697         $self->{event_watch} = $event;
698     }
699 }
700
701 =head2 C<< $obj->watch_write( $boolean ) >>
702
703 Turn 'writable' event notification on or off.
704
705 =cut
706 sub watch_write {
707     my PublicInbox::DS $self = shift;
708     return if $self->{closed} || !$self->{sock};
709
710     my $val = shift;
711     my $event = $self->{event_watch};
712
713     $event &= ~POLLOUT if ! $val;
714     $event |=  POLLOUT if   $val;
715     my $fd = fileno($self->{sock});
716
717     # If it changed, set it
718     if ($event != $self->{event_watch}) {
719         if ($HaveKQueue) {
720             $KQueue->EV_SET($fd, IO::KQueue::EVFILT_WRITE(),
721                             $val ? IO::KQueue::EV_ENABLE() : IO::KQueue::EV_DISABLE());
722         }
723         elsif ($HaveEpoll) {
724             epoll_ctl($Epoll, EPOLL_CTL_MOD, $fd, $event) and
725                     confess "EPOLL_CTL_MOD: $!";
726         }
727         $self->{event_watch} = $event;
728     }
729 }
730
731 =head2 C<< $obj->dump_error( $message ) >>
732
733 Prints to STDERR a backtrace with information about this socket and what lead
734 up to the dump_error call.
735
736 =cut
737 sub dump_error {
738     my $i = 0;
739     my @list;
740     while (my ($file, $line, $sub) = (caller($i++))[1..3]) {
741         push @list, "\t$file:$line called $sub\n";
742     }
743
744     warn "ERROR: $_[1]\n" .
745         "\t$_[0] = " . $_[0]->as_string . "\n" .
746         join('', @list);
747 }
748
749 =head2 C<< $obj->debugmsg( $format, @args ) >>
750
751 Print the debugging message specified by the C<sprintf>-style I<format> and
752 I<args>.
753
754 =cut
755 sub debugmsg {
756     my ( $self, $fmt, @args ) = @_;
757     confess "Not an object" unless ref $self;
758
759     chomp $fmt;
760     printf STDERR ">>> $fmt\n", @args;
761 }
762
763 =head2 C<< $obj->as_string() >>
764
765 Returns a string describing this socket.
766
767 =cut
768 sub as_string {
769     my PublicInbox::DS $self = shift;
770     my $rw = "(" . ($self->{event_watch} & POLLIN ? 'R' : '') .
771                    ($self->{event_watch} & POLLOUT ? 'W' : '') . ")";
772     my $ret = ref($self) . "$rw: " . ($self->{closed} ? "closed" : "open");
773     return $ret;
774 }
775
776 package PublicInbox::DS::Timer;
777 # [$abs_float_firetime, $coderef];
778 sub cancel {
779     $_[0][1] = undef;
780 }
781
782 1;
783
784 =head1 AUTHORS (Danga::Socket)
785
786 Brad Fitzpatrick <brad@danga.com> - author
787
788 Michael Granger <ged@danga.com> - docs, testing
789
790 Mark Smith <junior@danga.com> - contributor, heavy user, testing
791
792 Matt Sergeant <matt@sergeant.org> - kqueue support, docs, timers, other bits