1 # This library is free software; you can redistribute it and/or modify
2 # it under the same terms as Perl itself.
4 # This license differs from the rest of public-inbox
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.
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;
22 use Fcntl qw(FD_CLOEXEC F_SETFD F_GETFD);
26 use PublicInbox::Syscall qw(:epoll);
28 use fields ('sock', # underlying socket
29 'fd', # numeric file descriptor
30 'write_buf', # arrayref of scalars, scalarrefs, or coderefs to write
31 'write_buf_offset', # offset into first array of write_buf to start writing at
32 'write_buf_size', # total length of data in all write_buf items
33 'write_set_watch', # bool: true if we internally set watch_write rather than by a subclass
34 'closed', # bool: socket is closed
35 'event_watch', # bitmask of events the client is interested in (POLLIN,OUT,etc.)
38 use Errno qw(EINPROGRESS EWOULDBLOCK EISCONN ENOTSOCK
39 EPIPE EAGAIN EBADF ECONNRESET ENOPROTOOPT);
40 use Carp qw(croak confess);
42 use constant DebugLevel => 0;
44 use constant POLLIN => 1;
45 use constant POLLOUT => 4;
46 use constant POLLERR => 8;
47 use constant POLLHUP => 16;
48 use constant POLLNVAL => 32;
50 our $HAVE_KQUEUE = eval { require IO::KQueue; 1 };
53 $HaveEpoll, # Flag -- is epoll available? initially undefined.
55 %DescriptorMap, # fd (num) -> PublicInbox::DS object
56 $Epoll, # Global epoll fd (for epoll mode only)
57 $KQueue, # Global kqueue fd ref (for kqueue mode only)
58 $_io, # IO::Handle for Epoll
59 @ToClose, # sockets to close when event loop is done
61 $PostLoopCallback, # subref to call at the end of each loop, if defined (global)
62 %PLCMap, # fd (num) -> PostLoopCallback (per-object)
64 $LoopTimeout, # timeout of event loop in milliseconds
65 $DoneInit, # if we've done the one-time module init yet
69 # this may be set to zero with old kernels
70 our $EPOLLEXCLUSIVE = EPOLLEXCLUSIVE;
73 #####################################################################
74 ### C L A S S M E T H O D S
75 #####################################################################
77 =head2 C<< CLASS->Reset() >>
85 $LoopTimeout = -1; # no timeout by default
88 $PostLoopCallback = undef;
92 # NOTE kqueue is close-on-fork, and we don't account for it, yet
93 # OTOH, we (public-inbox) don't need this sub outside of tests...
94 POSIX::close($$KQueue) if !$_io && $KQueue && $$KQueue >= 0;
97 $_io = undef; # close $Epoll
100 *EventLoop = *FirstTimeEventLoop;
103 =head2 C<< CLASS->SetLoopTimeout( $timeout ) >>
105 Set the loop timeout for the event loop to some value in milliseconds.
107 A timeout of 0 (zero) means poll forever. A timeout of -1 means poll and return
112 return $LoopTimeout = $_[1] + 0;
115 =head2 C<< CLASS->DebugMsg( $format, @args ) >>
117 Print the debugging message specified by the C<sprintf>-style I<format> and
122 my ( $class, $fmt, @args ) = @_;
124 printf STDERR ">>> $fmt\n", @args;
127 =head2 C<< CLASS->AddTimer( $seconds, $coderef ) >>
129 Add a timer to occur $seconds from now. $seconds may be fractional, but timers
130 are not guaranteed to fire at the exact time you ask for.
132 Returns a timer object which you can call C<< $timer->cancel >> on if you need to.
137 my ($secs, $coderef) = @_;
139 my $fire_time = Time::HiRes::time() + $secs;
141 my $timer = bless [$fire_time, $coderef], "PublicInbox::DS::Timer";
143 if (!@Timers || $fire_time >= $Timers[-1][0]) {
144 push @Timers, $timer;
148 # Now, where do we insert? (NOTE: this appears slow, algorithm-wise,
149 # but it was compared against calendar queues, heaps, naive push/sort,
150 # and a bunch of other versions, and found to be fastest with a large
151 # variety of datasets.)
152 for (my $i = 0; $i < @Timers; $i++) {
153 if ($Timers[$i][0] > $fire_time) {
154 splice(@Timers, $i, 0, $timer);
159 die "Shouldn't get here.";
162 # keeping this around in case we support other FD types for now,
163 # epoll_create1(EPOLL_CLOEXEC) requires Linux 2.6.27+...
164 sub set_cloexec ($) {
167 $_io = IO::Handle->new_from_fd($fd, 'r+') or return;
168 defined(my $fl = fcntl($_io, F_GETFD, 0)) or return;
169 fcntl($_io, F_SETFD, $fl | FD_CLOEXEC);
178 $KQueue = IO::KQueue->new();
179 $HaveKQueue = defined $KQueue;
181 *EventLoop = *KQueueEventLoop;
184 elsif (PublicInbox::Syscall::epoll_defined()) {
185 $Epoll = eval { epoll_create(1024); };
186 $HaveEpoll = defined $Epoll && $Epoll >= 0;
189 *EventLoop = *EpollEventLoop;
193 if (!$HaveEpoll && !$HaveKQueue) {
195 *EventLoop = *PollEventLoop;
199 =head2 C<< CLASS->EventLoop() >>
201 Start processing IO events. In most daemon programs this never exits. See
202 C<PostLoopCallback> below for how to exit the loop.
205 sub FirstTimeEventLoop {
211 EpollEventLoop($class);
212 } elsif ($HaveKQueue) {
213 KQueueEventLoop($class);
215 PollEventLoop($class);
219 # runs timers and returns milliseconds for next one, or next event loop
221 return $LoopTimeout unless @Timers;
223 my $now = Time::HiRes::time();
226 while (@Timers && $Timers[0][0] <= $now) {
227 my $to_run = shift(@Timers);
228 $to_run->[1]->($now) if $to_run->[1];
231 return $LoopTimeout unless @Timers;
233 # convert time to an even number of milliseconds, adding 1
234 # extra, otherwise floating point fun can occur and we'll
235 # call RunTimers like 20-30 times, each returning a timeout
236 # of 0.0000212 seconds
237 my $timeout = int(($Timers[0][0] - $now) * 1000) + 1;
239 # -1 is an infinite timeout, so prefer a real timeout
240 return $timeout if $LoopTimeout == -1;
242 # otherwise pick the lower of our regular timeout and time until
244 return $LoopTimeout if $LoopTimeout < $timeout;
248 ### The epoll-based event loop. Gets installed as EventLoop if IO::Epoll loads
256 my $timeout = RunTimers();
258 # get up to 1000 events
259 my $evcount = epoll_wait($Epoll, 1000, $timeout, \@events);
261 for ($i=0; $i<$evcount; $i++) {
262 my $ev = $events[$i];
264 # it's possible epoll_wait returned many events, including some at the end
265 # that ones in the front triggered unregister-interest actions. if we
266 # can't find the %sock entry, it's because we're no longer interested
268 my PublicInbox::DS $pob = $DescriptorMap{$ev->[0]};
270 my $state = $ev->[1];
272 DebugLevel >= 1 && $class->DebugMsg("Event: fd=%d (%s), state=%d \@ %s\n",
273 $ev->[0], ref($pob), $ev->[1], time);
275 # standard non-profiling codepat
276 $pob->event_read if $state & EPOLLIN && ! $pob->{closed};
277 $pob->event_write if $state & EPOLLOUT && ! $pob->{closed};
278 if ($state & (EPOLLERR|EPOLLHUP)) {
279 $pob->event_err if $state & EPOLLERR && ! $pob->{closed};
280 $pob->event_hup if $state & EPOLLHUP && ! $pob->{closed};
283 return unless PostEventLoop();
288 ### The fallback IO::Poll-based event loop. Gets installed as EventLoop if
289 ### IO::Epoll fails to load.
293 my PublicInbox::DS $pob;
296 my $timeout = RunTimers();
298 # the following sets up @poll as a series of ($poll,$event_mask)
299 # items, then uses IO::Poll::_poll, implemented in XS, which
300 # modifies the array in place with the even elements being
301 # replaced with the event masks that occured.
303 while ( my ($fd, $sock) = each %DescriptorMap ) {
304 push @poll, $fd, $sock->{event_watch};
307 # if nothing to poll, either end immediately (if no timeout)
308 # or just keep calling the callback
310 select undef, undef, undef, ($timeout / 1000);
311 return unless PostEventLoop();
315 my $count = IO::Poll::_poll($timeout, @poll);
316 unless ($count >= 0) {
317 return unless PostEventLoop();
321 # Fetch handles with read events
323 my ($fd, $state) = splice(@poll, 0, 2);
326 $pob = $DescriptorMap{$fd};
328 $pob->event_read if $state & POLLIN && ! $pob->{closed};
329 $pob->event_write if $state & POLLOUT && ! $pob->{closed};
330 $pob->event_err if $state & POLLERR && ! $pob->{closed};
331 $pob->event_hup if $state & POLLHUP && ! $pob->{closed};
334 return unless PostEventLoop();
340 ### The kqueue-based event loop. Gets installed as EventLoop if IO::KQueue works
342 sub KQueueEventLoop {
346 my $timeout = RunTimers();
347 my @ret = eval { $KQueue->kevent($timeout) };
349 # workaround https://rt.cpan.org/Ticket/Display.html?id=116615
350 if ($err =~ /Interrupted system call/) {
357 foreach my $kev (@ret) {
358 my ($fd, $filter, $flags, $fflags) = @$kev;
359 my PublicInbox::DS $pob = $DescriptorMap{$fd};
361 DebugLevel >= 1 && $class->DebugMsg("Event: fd=%d (%s), flags=%d \@ %s\n",
362 $fd, ref($pob), $flags, time);
364 $pob->event_read if $filter == IO::KQueue::EVFILT_READ() && !$pob->{closed};
365 $pob->event_write if $filter == IO::KQueue::EVFILT_WRITE() && !$pob->{closed};
366 if ($flags == IO::KQueue::EV_EOF() && !$pob->{closed}) {
374 return unless PostEventLoop();
380 =head2 C<< CLASS->SetPostLoopCallback( CODEREF ) >>
382 Sets post loop callback function. Pass a subref and it will be
383 called every time the event loop finishes.
385 Return 1 (or any true value) from the sub to make the loop continue, 0 or false
388 The callback function will be passed two parameters: \%DescriptorMap
391 sub SetPostLoopCallback {
392 my ($class, $ref) = @_;
395 # per-object callback
396 my PublicInbox::DS $self = $class;
397 if (defined $ref && ref $ref eq 'CODE') {
398 $PLCMap{$self->{fd}} = $ref;
400 delete $PLCMap{$self->{fd}};
404 $PostLoopCallback = (defined $ref && ref $ref eq 'CODE') ? $ref : undef;
408 # Internal function: run the post-event callback, send read events
409 # for pushed-back data, and close pending connections. returns 1
410 # if event loop should continue, or 0 to shut it all down.
412 # now we can close sockets that wanted to close during our event processing.
413 # (we didn't want to close them during the loop, as we didn't want fd numbers
414 # being reused and confused during the event loop)
415 while (my $sock = shift @ToClose) {
416 my $fd = fileno($sock);
418 # close the socket. (not a PublicInbox::DS close)
421 # and now we can finally remove the fd from the map. see
422 # comment above in _cleanup.
423 delete $DescriptorMap{$fd};
427 # by default we keep running, unless a postloop callback (either per-object
428 # or global) cancels it
429 my $keep_running = 1;
431 # per-object post-loop-callbacks
432 for my $plc (values %PLCMap) {
433 $keep_running &&= $plc->(\%DescriptorMap);
436 # now we're at the very end, call callback if defined
437 if (defined $PostLoopCallback) {
438 $keep_running &&= $PostLoopCallback->(\%DescriptorMap);
441 return $keep_running;
444 #####################################################################
445 ### PublicInbox::DS-the-object code
446 #####################################################################
448 =head2 OBJECT METHODS
450 =head2 C<< CLASS->new( $socket ) >>
452 Create a new PublicInbox::DS subclass object for the given I<socket> which will
453 react to events on it during the C<EventLoop>.
455 This is normally (always?) called from your subclass via:
457 $class->SUPER::new($socket);
461 my ($self, $sock, $exclusive) = @_;
462 $self = fields::new($self) unless ref $self;
464 $self->{sock} = $sock;
465 my $fd = fileno($sock);
467 Carp::cluck("undef sock and/or fd in PublicInbox::DS->new. sock=" . ($sock || "") . ", fd=" . ($fd || ""))
471 $self->{write_buf} = [];
472 $self->{write_buf_offset} = 0;
473 $self->{write_buf_size} = 0;
476 my $ev = $self->{event_watch} = POLLERR|POLLHUP|POLLNVAL;
482 $ev = $self->{event_watch} = EPOLLIN|EPOLLERR|EPOLLHUP|$EPOLLEXCLUSIVE;
485 if (epoll_ctl($Epoll, EPOLL_CTL_ADD, $fd, $ev)) {
486 if ($!{EINVAL} && ($ev & $EPOLLEXCLUSIVE)) {
487 $EPOLLEXCLUSIVE = 0; # old kernel
488 $ev = $self->{event_watch} = EPOLLIN|EPOLLERR|EPOLLHUP;
491 die "couldn't add epoll watch for $fd: $!\n";
494 elsif ($HaveKQueue) {
495 # Add them to the queue but disabled for now
496 $KQueue->EV_SET($fd, IO::KQueue::EVFILT_READ(),
497 IO::KQueue::EV_ADD() | IO::KQueue::EV_DISABLE());
498 $KQueue->EV_SET($fd, IO::KQueue::EVFILT_WRITE(),
499 IO::KQueue::EV_ADD() | IO::KQueue::EV_DISABLE());
502 Carp::cluck("PublicInbox::DS::new blowing away existing descriptor map for fd=$fd ($DescriptorMap{$fd})")
503 if $DescriptorMap{$fd};
505 $DescriptorMap{$fd} = $self;
510 #####################################################################
511 ### I N S T A N C E M E T H O D S
512 #####################################################################
514 =head2 C<< $obj->steal_socket() >>
516 Basically returns our socket and makes it so that we don't try to close it,
517 but we do remove it from epoll handlers. THIS CLOSES $self. It is the same
518 thing as calling close, except it gives you the socket to use.
522 my PublicInbox::DS $self = $_[0];
523 return if $self->{closed};
525 # cleanup does most of the work of closing this socket
528 # now undef our internal sock and fd structures so we don't use them
529 my $sock = $self->{sock};
530 $self->{sock} = undef;
534 =head2 C<< $obj->close( [$reason] ) >>
536 Close the socket. The I<reason> argument will be used in debugging messages.
540 my PublicInbox::DS $self = $_[0];
541 return if $self->{closed};
543 # print out debugging info for this close
545 my ($pkg, $filename, $line) = caller;
546 my $reason = $_[1] || "";
547 warn "Closing \#$self->{fd} due to $pkg/$filename/$line ($reason)\n";
550 # this does most of the work of closing us
553 # defer closing the actual socket until the event loop is done
554 # processing this round of events. (otherwise we might reuse fds)
556 push @ToClose, $self->{sock};
557 $self->{sock} = undef;
563 ### METHOD: _cleanup()
564 ### Called by our closers so we can clean internal data structures.
566 my PublicInbox::DS $self = $_[0];
568 # we're effectively closed; we have no fd and sock when we leave here
571 # we need to flush our write buffer, as there may
572 # be self-referential closures (sub { $client->close })
573 # preventing the object from being destroyed
574 $self->{write_buf} = [];
576 # if we're using epoll, we have to remove this from our epoll fd so we stop getting
577 # notifications about it
578 if ($HaveEpoll && $self->{fd}) {
579 if (epoll_ctl($Epoll, EPOLL_CTL_DEL, $self->{fd}, $self->{event_watch}) != 0) {
580 # dump_error prints a backtrace so we can try to figure out why this happened
581 $self->dump_error("epoll_ctl(): failure deleting fd=$self->{fd} during _cleanup(); $! (" . ($!+0) . ")");
585 # now delete from mappings. this fd no longer belongs to us, so we don't want
586 # to get alerts for it if it becomes writable/readable/etc.
587 delete $PLCMap{$self->{fd}};
589 # we explicitly don't delete from DescriptorMap here until we
590 # actually close the socket, as we might be in the middle of
591 # processing an epoll_wait/etc that returned hundreds of fds, one
592 # of which is not yet processed and is what we're closing. if we
593 # keep it in DescriptorMap, then the event harnesses can just
594 # looked at $pob->{closed} and ignore it. but if it's an
595 # un-accounted for fd, then it (understandably) freak out a bit
596 # and emit warnings, thinking their state got off.
598 # and finally get rid of our fd so we can't use it anywhere else
602 =head2 C<< $obj->sock() >>
604 Returns the underlying IO::Handle for the object.
608 my PublicInbox::DS $self = shift;
609 return $self->{sock};
612 =head2 C<< $obj->write( $data ) >>
614 Write the specified data to the underlying handle. I<data> may be scalar,
615 scalar ref, code ref (to run when there), or undef just to kick-start.
616 Returns 1 if writes all went through, or 0 if there are writes in queue. If
617 it returns 1, caller should stop waiting for 'writable' events)
621 my PublicInbox::DS $self;
625 # nobody should be writing to closed sockets, but caller code can
626 # do two writes within an event, have the first fail and
627 # disconnect the other side (whose destructor then closes the
628 # calling object, but it's still in a method), and then the
629 # now-dead object does its second write. that is this case. we
630 # just lie and say it worked. it'll be dead soon and won't be
632 return 1 if $self->{closed};
636 # just queue data if there's already a wait
640 $bref = ref $data ? $data : \$data;
641 if ($self->{write_buf_size}) {
642 push @{$self->{write_buf}}, $bref;
643 $self->{write_buf_size} += ref $bref eq "SCALAR" ? length($$bref) : 1;
647 # this flag says we're bypassing the queue system, knowing we're the
648 # only outstanding write, and hoping we don't ever need to use it.
649 # if so later, though, we'll need to queue
655 return 1 unless $bref ||= $self->{write_buf}[0];
659 $len = length($$bref); # this will die if $bref is a code ref, caught below
662 if (UNIVERSAL::isa($bref, "CODE")) {
663 unless ($need_queue) {
664 $self->{write_buf_size}--; # code refs are worth 1
665 shift @{$self->{write_buf}};
669 # code refs are just run and never get reenqueued
670 # (they're one-shot), so turn off the flag indicating the
671 # outstanding data needs queueing.
677 die "Write error: $@ <$bref>";
680 my $to_write = $len - $self->{write_buf_offset};
681 my $written = syswrite($self->{sock}, $$bref, $to_write,
682 $self->{write_buf_offset});
684 if (! defined $written) {
686 return $self->close("EPIPE");
687 } elsif ($! == EAGAIN) {
688 # since connection has stuff to write, it should now be
689 # interested in pending writes:
691 push @{$self->{write_buf}}, $bref;
692 $self->{write_buf_size} += $len;
694 $self->{write_set_watch} = 1 unless $self->{event_watch} & POLLOUT;
695 $self->watch_write(1);
697 } elsif ($! == ECONNRESET) {
698 return $self->close("ECONNRESET");
701 DebugLevel >= 1 && $self->debugmsg("Closing connection ($self) due to write error: $!\n");
703 return $self->close("write_error");
704 } elsif ($written != $to_write) {
705 DebugLevel >= 2 && $self->debugmsg("Wrote PARTIAL %d bytes to %d",
706 $written, $self->{fd});
708 push @{$self->{write_buf}}, $bref;
709 $self->{write_buf_size} += $len;
711 # since connection has stuff to write, it should now be
712 # interested in pending writes:
713 $self->{write_buf_offset} += $written;
714 $self->{write_buf_size} -= $written;
715 $self->on_incomplete_write;
717 } elsif ($written == $to_write) {
718 DebugLevel >= 2 && $self->debugmsg("Wrote ALL %d bytes to %d (nq=%d)",
719 $written, $self->{fd}, $need_queue);
720 $self->{write_buf_offset} = 0;
722 if ($self->{write_set_watch}) {
723 $self->watch_write(0);
724 $self->{write_set_watch} = 0;
727 # this was our only write, so we can return immediately
728 # since we avoided incrementing the buffer size or
729 # putting it in the buffer. we also know there
730 # can't be anything else to write.
731 return 1 if $need_queue;
733 $self->{write_buf_size} -= $written;
734 shift @{$self->{write_buf}};
741 sub on_incomplete_write {
742 my PublicInbox::DS $self = shift;
743 $self->{write_set_watch} = 1 unless $self->{event_watch} & POLLOUT;
744 $self->watch_write(1);
747 =head2 C<< $obj->read( $bytecount ) >>
749 Read at most I<bytecount> bytes from the underlying handle; returns scalar
750 ref on read, or undef on connection closed.
754 my PublicInbox::DS $self = shift;
755 return if $self->{closed};
758 my $sock = $self->{sock};
760 # if this is too high, perl quits(!!). reports on mailing lists
761 # don't seem to point to a universal answer. 5MB worked for some,
762 # crashed for others. 1MB works for more people. let's go with 1MB
764 my $req_bytes = $bytes > 1048576 ? 1048576 : $bytes;
766 my $res = sysread($sock, $buf, $req_bytes, 0);
767 DebugLevel >= 2 && $self->debugmsg("sysread = %d; \$! = %d", $res, $!);
769 if (! $res && $! != EWOULDBLOCK) {
770 # catches 0=conn closed or undef=error
771 DebugLevel >= 2 && $self->debugmsg("Fd \#%d read hit the end of the road.", $self->{fd});
778 =head2 (VIRTUAL) C<< $obj->event_read() >>
780 Readable event handler. Concrete deriviatives of PublicInbox::DS should
781 provide an implementation of this. The default implementation will die if
785 sub event_read { die "Base class event_read called for $_[0]\n"; }
787 =head2 (VIRTUAL) C<< $obj->event_err() >>
789 Error event handler. Concrete deriviatives of PublicInbox::DS should
790 provide an implementation of this. The default implementation will die if
794 sub event_err { die "Base class event_err called for $_[0]\n"; }
796 =head2 (VIRTUAL) C<< $obj->event_hup() >>
798 'Hangup' event handler. Concrete deriviatives of PublicInbox::DS should
799 provide an implementation of this. The default implementation will die if
803 sub event_hup { die "Base class event_hup called for $_[0]\n"; }
805 =head2 C<< $obj->event_write() >>
807 Writable event handler. Concrete deriviatives of PublicInbox::DS may wish to
808 provide an implementation of this. The default implementation calls
809 C<write()> with an C<undef>.
817 =head2 C<< $obj->watch_read( $boolean ) >>
819 Turn 'readable' event notification on or off.
823 my PublicInbox::DS $self = shift;
824 return if $self->{closed} || !$self->{sock};
827 my $event = $self->{event_watch};
829 $event &= ~POLLIN if ! $val;
830 $event |= POLLIN if $val;
832 # If it changed, set it
833 if ($event != $self->{event_watch}) {
835 $KQueue->EV_SET($self->{fd}, IO::KQueue::EVFILT_READ(),
836 $val ? IO::KQueue::EV_ENABLE() : IO::KQueue::EV_DISABLE());
839 epoll_ctl($Epoll, EPOLL_CTL_MOD, $self->{fd}, $event)
840 and $self->dump_error("couldn't modify epoll settings for $self->{fd} " .
841 "from $self->{event_watch} -> $event: $! (" . ($!+0) . ")");
843 $self->{event_watch} = $event;
847 =head2 C<< $obj->watch_write( $boolean ) >>
849 Turn 'writable' event notification on or off.
853 my PublicInbox::DS $self = shift;
854 return if $self->{closed} || !$self->{sock};
857 my $event = $self->{event_watch};
859 $event &= ~POLLOUT if ! $val;
860 $event |= POLLOUT if $val;
862 if ($val && caller ne __PACKAGE__) {
863 # A subclass registered interest, it's now responsible for this.
864 $self->{write_set_watch} = 0;
867 # If it changed, set it
868 if ($event != $self->{event_watch}) {
870 $KQueue->EV_SET($self->{fd}, IO::KQueue::EVFILT_WRITE(),
871 $val ? IO::KQueue::EV_ENABLE() : IO::KQueue::EV_DISABLE());
874 epoll_ctl($Epoll, EPOLL_CTL_MOD, $self->{fd}, $event)
875 and $self->dump_error("couldn't modify epoll settings for $self->{fd} " .
876 "from $self->{event_watch} -> $event: $! (" . ($!+0) . ")");
878 $self->{event_watch} = $event;
882 =head2 C<< $obj->dump_error( $message ) >>
884 Prints to STDERR a backtrace with information about this socket and what lead
885 up to the dump_error call.
891 while (my ($file, $line, $sub) = (caller($i++))[1..3]) {
892 push @list, "\t$file:$line called $sub\n";
895 warn "ERROR: $_[1]\n" .
896 "\t$_[0] = " . $_[0]->as_string . "\n" .
900 =head2 C<< $obj->debugmsg( $format, @args ) >>
902 Print the debugging message specified by the C<sprintf>-style I<format> and
907 my ( $self, $fmt, @args ) = @_;
908 confess "Not an object" unless ref $self;
911 printf STDERR ">>> $fmt\n", @args;
914 =head2 C<< $obj->as_string() >>
916 Returns a string describing this socket.
920 my PublicInbox::DS $self = shift;
921 my $rw = "(" . ($self->{event_watch} & POLLIN ? 'R' : '') .
922 ($self->{event_watch} & POLLOUT ? 'W' : '') . ")";
923 my $ret = ref($self) . "$rw: " . ($self->{closed} ? "closed" : "open");
927 package PublicInbox::DS::Timer;
928 # [$abs_float_firetime, $coderef];
935 =head1 AUTHORS (Danga::Socket)
937 Brad Fitzpatrick <brad@danga.com> - author
939 Michael Granger <ged@danga.com> - docs, testing
941 Mark Smith <junior@danga.com> - contributor, heavy user, testing
943 Matt Sergeant <matt@sergeant.org> - kqueue support, docs, timers, other bits