]> Sergey Matveev's repositories - public-inbox.git/blob - lib/PublicInbox/IPC.pm
ipc: support Sereal
[public-inbox.git] / lib / PublicInbox / IPC.pm
1 # Copyright (C) 2020 all contributors <meta@public-inbox.org>
2 # License: AGPL-3.0+ <https://www.gnu.org/licenses/agpl-3.0.txt>
3
4 # base class for remote IPC calls, requires Storable
5 # TODO: this ought to be usable in SearchIdxShard
6 package PublicInbox::IPC;
7 use strict;
8 use v5.10.1;
9 use Socket qw(AF_UNIX SOCK_STREAM);
10 use Carp qw(confess croak);
11 use PublicInbox::Sigfd;
12 my ($enc, $dec);
13 # ->imports at BEGIN turns serial_*_with_object into custom ops on 5.14+
14 # and eliminate method call overhead
15 BEGIN {
16         eval {
17                 require Sereal::Encoder;
18                 require Sereal::Decoder;
19                 Sereal::Encoder->import('sereal_encode_with_object');
20                 Sereal::Decoder->import('sereal_decode_with_object');
21                 ($enc, $dec) = (Sereal::Encoder->new, Sereal::Decoder->new);
22         };
23 };
24
25 if ($enc && $dec) { # should be custom ops
26         *freeze = sub ($) { sereal_encode_with_object $enc, $_[0] };
27         *thaw = sub ($) { sereal_decode_with_object $dec, $_[0], my $ret };
28 } else {
29         eval { # some distros have Storable as a separate package from Perl
30                 require Storable;
31                 Storable->import(qw(freeze thaw));
32                 $enc = 1;
33         } // warn("Storable (part of Perl) missing: $@\n");
34 }
35
36 sub _get_rec ($) {
37         my ($sock) = @_;
38         local $/ = "\n";
39         defined(my $len = <$sock>) or return;
40         chop($len) eq "\n" or croak "no LF byte in $len";
41         defined(my $r = read($sock, my $buf, $len)) or croak "read error: $!";
42         $r == $len or croak "short read: $r != $len";
43         thaw($buf);
44 }
45
46 sub _send_rec ($$) {
47         my ($sock, $ref) = @_;
48         my $buf = freeze($ref);
49         print $sock length($buf), "\n", $buf or croak "print: $!";
50 }
51
52 sub ipc_return ($$$) {
53         my ($s2, $ret, $exc) = @_;
54         _send_rec($s2, $exc ? bless(\$exc, 'PublicInbox::IPC::Die') : $ret);
55 }
56
57 sub ipc_worker_loop ($$) {
58         my ($self, $s2) = @_;
59         $self->ipc_atfork_child if $self->can('ipc_atfork_child');
60         $s2->autoflush(1);
61         while (my $rec = _get_rec($s2)) {
62                 my ($wantarray, $sub, @args) = @$rec;
63                 if (!defined($wantarray)) { # no waiting if client doesn't care
64                         eval { $self->$sub(@args) };
65                         eval { warn "die: $@ (from nowait $sub)\n" } if $@;
66                 } elsif ($wantarray) {
67                         my @ret = eval { $self->$sub(@args) };
68                         ipc_return($s2, \@ret, $@);
69                 } else {
70                         my $ret = eval { $self->$sub(@args) };
71                         ipc_return($s2, \$ret, $@);
72                 }
73         }
74 }
75
76 sub ipc_worker_spawn ($$$) {
77         my ($self, $ident, $oldset) = @_;
78         return unless $enc;
79         my $pid = $self->{-ipc_worker_pid};
80         confess "BUG: already spawned PID:$pid" if $pid;
81         confess "BUG: already have worker socket" if $self->{-ipc_sock};
82         my ($s1, $s2);
83         socketpair($s1, $s2, AF_UNIX, SOCK_STREAM, 0) or die "socketpair: $!";
84         my $sigset = $oldset // PublicInbox::Sigfd::block_signals();
85         defined($pid = fork) or die "fork: $!";
86         if ($pid == 0) {
87                 undef $s1;
88                 local $0 = $ident;
89                 $SIG{$_} = 'IGNORE' for (qw(TERM INT QUIT));
90                 PublicInbox::Sigfd::sig_setmask($oldset);
91                 eval { ipc_worker_loop($self, $s2) };
92                 die "worker $ident died: $@\n" if $@;
93                 $self->ipc_at_worker_exit if $self->can('ipc_at_worker_exit');
94                 exit;
95         }
96         PublicInbox::Sigfd::sig_setmask($sigset) unless $oldset;
97         $s1->autoflush(1);
98         $self->{-ipc_sock} = $s1;
99         $self->{-ipc_worker_pid} = $pid;
100 }
101
102 sub ipc_reap_worker { # dwaitpid callback
103         my ($self, $pid) = @_;
104         warn "PID:$pid died with \$?=$?\n" if $?;
105 }
106
107 sub ipc_worker_stop {
108         my ($self) = @_;
109         my $pid;
110         if (delete $self->{-ipc_sock}) {
111                 $pid = delete $self->{-ipc_worker_pid} or die "no PID?";
112         } else {
113                 $pid = delete $self->{-ipc_worker_pid} and
114                         die "unexpected PID:$pid";
115         }
116         return unless $pid;
117         eval { PublicInbox::DS::dwaitpid($pid, \&ipc_reap_worker, $self) };
118         if ($@) {
119                 my $wp = waitpid($pid, 0);
120                 $pid == $wp or die "waitpid($pid) returned $wp: \$?=$?";
121                 ipc_reap_worker($self, $pid);
122         }
123 }
124
125 # use this if we have multiple readers reading curl or "pigz -dc"
126 # and writing to the same store
127 sub ipc_lock_init {
128         my ($self, $f) = @_;
129         require PublicInbox::Lock;
130         $self->{-ipc_lock} //= bless { lock_path => $f }, 'PublicInbox::Lock'
131 }
132
133 sub ipc_do {
134         my ($self, $sub, @args) = @_;
135         if (my $s1 = $self->{-ipc_sock}) {
136                 my $ipc_lock = $self->{-ipc_lock};
137                 my $lock = $ipc_lock ? $ipc_lock->lock_for_scope : undef;
138                 _send_rec($s1, [ wantarray, $sub, @args ]);
139                 return unless defined(wantarray);
140                 my $ret = _get_rec($s1) // die "no response on $sub";
141                 die $$ret if ref($ret) eq 'PublicInbox::IPC::Die';
142                 wantarray ? @$ret : $$ret;
143         } else {
144                 $self->$sub(@args);
145         }
146 }
147
148 1;