]> Sergey Matveev's repositories - public-inbox.git/blob - lib/PublicInbox/PktOp.pm
pkt_op: do not exit subroutine via "next"
[public-inbox.git] / lib / PublicInbox / PktOp.pm
1 # Copyright (C) 2021 all contributors <meta@public-inbox.org>
2 # License: AGPL-3.0+ <https://www.gnu.org/licenses/agpl-3.0.txt>
3
4 # op dispatch socket, reads a message, runs a sub
5 # There may be multiple producers, but (for now) only one consumer
6 # Used for lei_xsearch and maybe other things
7 # "command" => [ $sub, @fixed_operands ]
8 package PublicInbox::PktOp;
9 use strict;
10 use v5.10.1;
11 use parent qw(PublicInbox::DS Exporter);
12 use Errno qw(EAGAIN EINTR);
13 use PublicInbox::Syscall qw(EPOLLIN EPOLLET);
14 use Socket qw(AF_UNIX MSG_EOR SOCK_SEQPACKET);
15 use PublicInbox::IPC qw(ipc_freeze ipc_thaw);
16 our @EXPORT_OK = qw(pkt_do);
17
18 sub new {
19         my ($cls, $r, $ops) = @_;
20         my $self = bless { sock => $r, ops => $ops }, $cls;
21         if ($PublicInbox::DS::in_loop) { # iff using DS->EventLoop
22                 $r->blocking(0);
23                 $self->SUPER::new($r, EPOLLIN|EPOLLET);
24         }
25         $self;
26 }
27
28 # returns a blessed object as the consumer, and a GLOB/IO for the producer
29 sub pair {
30         my ($cls, $ops) = @_;
31         my ($c, $p);
32         socketpair($c, $p, AF_UNIX, SOCK_SEQPACKET, 0) or die "socketpair: $!";
33         (new($cls, $c, $ops), $p);
34 }
35
36 sub pkt_do { # for the producer to trigger event_step in consumer
37         my ($producer, $cmd, @args) = @_;
38         send($producer, @args ? "$cmd\0".ipc_freeze(\@args) : $cmd, MSG_EOR);
39 }
40
41 sub close {
42         my ($self) = @_;
43         my $c = $self->{sock} or return;
44         $c->blocking ? delete($self->{sock}) : $self->SUPER::close;
45 }
46
47 sub event_step {
48         my ($self) = @_;
49         my $c = $self->{sock};
50         my $msg;
51         while (1) {
52                 my $n = recv($c, $msg, 4096, 0);
53                 unless (defined $n) {
54                         return if $! == EAGAIN;
55                         next if $! == EINTR;
56                         $self->close;
57                         die "recv: $!";
58                 }
59                 my ($cmd, @pargs);
60                 if (index($msg, "\0") > 0) {
61                         ($cmd, my $pargs) = split(/\0/, $msg, 2);
62                         @pargs = @{ipc_thaw($pargs)};
63                 } else {
64                         # for compatibility with the script/lei in client mode,
65                         # it doesn't load Sereal||Storable for startup speed
66                         ($cmd, @pargs) = split(/ /, $msg);
67                 }
68                 my $op = $self->{ops}->{$cmd //= $msg};
69                 die "BUG: unknown message: `$cmd'" unless $op;
70                 my ($sub, @args) = @$op;
71                 $sub->(@args, @pargs);
72                 return $self->close if $msg eq ''; # close on EOF
73         }
74 }
75
76 1;