]> Sergey Matveev's repositories - public-inbox.git/blobdiff - lib/PublicInbox/DS.pm
ds: drop set_writer_func support
[public-inbox.git] / lib / PublicInbox / DS.pm
index 737f4c7a8ca366fd44535014a9f9ba1092d3a906..b2c4b44a6bf74c3743979f8f328743580f0199fa 100644 (file)
@@ -5,13 +5,21 @@
 #
 # This is a fork of the (for now) unmaintained Danga::Socket 1.61.
 # Unused features will be removed, and updates will be made to take
-# advantage of newer kernels
-
+# advantage of newer kernels.
+#
+# API changes to diverge from Danga::Socket will happen to better
+# accomodate new features and improve scalability.  Do not expect
+# this to be a stable API like Danga::Socket.
+# Bugs encountered (and likely fixed) are reported to
+# bug-Danga-Socket@rt.cpan.org and visible at:
+# https://rt.cpan.org/Public/Dist/Display.html?Name=Danga-Socket
 package PublicInbox::DS;
 use strict;
 use bytes;
 use POSIX ();
 use Time::HiRes ();
+use IO::Handle qw();
+use Fcntl qw(FD_CLOEXEC F_SETFD F_GETFD);
 
 use warnings;
 
@@ -25,7 +33,6 @@ use fields ('sock',              # underlying socket
             'write_set_watch',   # bool: true if we internally set watch_write rather than by a subclass
             'closed',            # bool: socket is closed
             'event_watch',       # bitmask of events the client is interested in (POLLIN,OUT,etc.)
-            'writer_func',       # subref which does writing.  must return bytes written (or undef) and set $! on errors
             );
 
 use Errno  qw(EINPROGRESS EWOULDBLOCK EISCONN ENOTSOCK
@@ -47,7 +54,8 @@ our (
      $HaveKQueue,
      %DescriptorMap,             # fd (num) -> PublicInbox::DS object
      $Epoll,                     # Global epoll fd (for epoll mode only)
-     $KQueue,                    # Global kqueue fd (for kqueue mode only)
+     $KQueue,                    # Global kqueue fd ref (for kqueue mode only)
+     $_io,                       # IO::Handle for Epoll
      @ToClose,                   # sockets to close when event loop is done
 
      $PostLoopCallback,          # subref to call at the end of each loop, if defined (global)
@@ -81,30 +89,17 @@ sub Reset {
     %PLCMap = ();
     $DoneInit = 0;
 
-    POSIX::close($Epoll)  if defined $Epoll  && $Epoll  >= 0;
-    POSIX::close($KQueue) if defined $KQueue && $KQueue >= 0;
-
-    *EventLoop = *FirstTimeEventLoop;
-}
-
-=head2 C<< CLASS->HaveEpoll() >>
+    # NOTE kqueue is close-on-fork, and we don't account for it, yet
+    # OTOH, we (public-inbox) don't need this sub outside of tests...
+    POSIX::close($$KQueue) if !$_io && $KQueue && $$KQueue >= 0;
+    $KQueue = undef;
 
-Returns a true value if this class will use IO::Epoll for async IO.
+    $_io = undef; # close $Epoll
+    $Epoll = undef;
 
-=cut
-sub HaveEpoll {
-    _InitPoller();
-    return $HaveEpoll;
+    *EventLoop = *FirstTimeEventLoop;
 }
 
-=head2 C<< CLASS->ToClose() >>
-
-Return the list of sockets that are awaiting close() at the end of the
-current event loop.
-
-=cut
-sub ToClose { return @ToClose; }
-
 =head2 C<< CLASS->SetLoopTimeout( $timeout ) >>
 
 Set the loop timeout for the event loop to some value in milliseconds.
@@ -164,6 +159,16 @@ sub AddTimer {
     die "Shouldn't get here.";
 }
 
+# keeping this around in case we support other FD types for now,
+# epoll_create1(EPOLL_CLOEXEC) requires Linux 2.6.27+...
+sub set_cloexec ($) {
+    my ($fd) = @_;
+
+    $_io = IO::Handle->new_from_fd($fd, 'r+') or return;
+    defined(my $fl = fcntl($_io, F_GETFD, 0)) or return;
+    fcntl($_io, F_SETFD, $fl | FD_CLOEXEC);
+}
+
 sub _InitPoller
 {
     return if $DoneInit;
@@ -171,7 +176,7 @@ sub _InitPoller
 
     if ($HAVE_KQUEUE) {
         $KQueue = IO::KQueue->new();
-        $HaveKQueue = $KQueue >= 0;
+        $HaveKQueue = defined $KQueue;
         if ($HaveKQueue) {
             *EventLoop = *KQueueEventLoop;
         }
@@ -180,6 +185,7 @@ sub _InitPoller
         $Epoll = eval { epoll_create(1024); };
         $HaveEpoll = defined $Epoll && $Epoll >= 0;
         if ($HaveEpoll) {
+            set_cloexec($Epoll);
             *EventLoop = *EpollEventLoop;
         }
     }
@@ -622,18 +628,6 @@ sub sock {
     return $self->{sock};
 }
 
-=head2 C<< $obj->set_writer_func( CODEREF ) >>
-
-Sets a function to use instead of C<syswrite()> when writing data to the socket.
-
-=cut
-sub set_writer_func {
-   my PublicInbox::DS $self = shift;
-   my $wtr = shift;
-   Carp::croak("Not a subref") unless !defined $wtr || UNIVERSAL::isa($wtr, "CODE");
-   $self->{writer_func} = $wtr;
-}
-
 =head2 C<< $obj->write( $data ) >>
 
 Write the specified data to the underlying handle.  I<data> may be scalar,
@@ -703,12 +697,8 @@ sub write {
         }
 
         my $to_write = $len - $self->{write_buf_offset};
-        my $written;
-        if (my $wtr = $self->{writer_func}) {
-            $written = $wtr->($bref, $to_write, $self->{write_buf_offset});
-        } else {
-            $written = syswrite($self->{sock}, $$bref, $to_write, $self->{write_buf_offset});
-        }
+        my $written = syswrite($self->{sock}, $$bref, $to_write,
+                               $self->{write_buf_offset});
 
         if (! defined $written) {
             if ($! == EPIPE) {