about summary refs log tree commit homepage
path: root/lib/PublicInbox/LEI.pm
diff options
context:
space:
mode:
authorEric Wong <e@80x24.org>2020-12-17 06:54:41 +0000
committerEric Wong <e@80x24.org>2020-12-19 09:32:08 +0000
commit50ce71e698e038e643d81d9f5948e002384b5898 (patch)
treed69320e3a8c2165e7d135e1ae24a6a57d27cf8e6 /lib/PublicInbox/LEI.pm
parent193e86164403dc0d43e7dc44ee68272897727f81 (diff)
downloadpublic-inbox-50ce71e698e038e643d81d9f5948e002384b5898.tar.gz
We'll use lower-level Socket and avoid IO::Socket::UNIX,
use Cwd::fastcwd(*), avoid IO::Handle->autoflush by
using the select operator, and reuse buffer for reading
the socket while avoiding unnecessary $/ localization
in a tiny script.

All these things adds up to ~5-10 ms savings on my loaded
system.

(*) caveats about fastcwd won't apply since lei won't work
    in removed directories.
Diffstat (limited to 'lib/PublicInbox/LEI.pm')
-rw-r--r--lib/PublicInbox/LEI.pm13
1 files changed, 6 insertions, 7 deletions
diff --git a/lib/PublicInbox/LEI.pm b/lib/PublicInbox/LEI.pm
index f5824c59..5399fade 100644
--- a/lib/PublicInbox/LEI.pm
+++ b/lib/PublicInbox/LEI.pm
@@ -10,9 +10,9 @@ use strict;
 use v5.10.1;
 use parent qw(PublicInbox::DS);
 use Getopt::Long ();
+use Socket qw(AF_UNIX SOCK_STREAM pack_sockaddr_un);
 use Errno qw(EAGAIN ECONNREFUSED ENOENT);
 use POSIX qw(setsid);
-use IO::Socket::UNIX;
 use IO::Handle ();
 use Sys::Syslog qw(syslog openlog);
 use PublicInbox::Config;
@@ -585,18 +585,17 @@ sub noop {}
 # lei(1) calls this when it can't connect
 sub lazy_start {
         my ($path, $err) = @_;
+        require IO::FDPass; # require this early so caller sees it
         if ($err == ECONNREFUSED) {
                 unlink($path) or die "unlink($path): $!";
         } elsif ($err != ENOENT) {
+                $! = $err; # allow interpolation to stringify in die
                 die "connect($path): $!";
         }
-        require IO::FDPass;
         umask(077) // die("umask(077): $!");
-        my $l = IO::Socket::UNIX->new(Local => $path,
-                                        Listen => 1024,
-                                        Type => SOCK_STREAM) or
-                $err = $!;
-        $l or return die "bind($path): $err";
+        socket(my $l, AF_UNIX, SOCK_STREAM, 0) or die "socket: $!";
+        bind($l, pack_sockaddr_un($path)) or die "bind($path): $!";
+        listen($l, 1024) or die "listen $!";
         my @st = stat($path) or die "stat($path): $!";
         my $dev_ino_expect = pack('dd', $st[0], $st[1]); # dev+ino
         pipe(my ($eof_r, $eof_w)) or die "pipe: $!";