]> Sergey Matveev's repositories - public-inbox.git/blob - lib/PublicInbox/PktOp.pm
lei: simplify PktOp callers
[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) = @_;
20         my $self = bless { sock => $r }, $cls;
21         if ($PublicInbox::DS::in_loop) { # iff using DS->EventLoop
22                 $r->blocking(0);
23                 $self->SUPER::new($r, EPOLLIN|EPOLLET);
24         } else {
25                 $self->{blocking} = 1;
26         }
27         $self;
28 }
29
30 # returns a blessed object as the consumer, and a GLOB/IO for the producer
31 sub pair {
32         my ($cls) = @_;
33         my ($c, $p);
34         socketpair($c, $p, AF_UNIX, SOCK_SEQPACKET, 0) or die "socketpair: $!";
35         (new($cls, $c), $p);
36 }
37
38 sub pkt_do { # for the producer to trigger event_step in consumer
39         my ($producer, $cmd, @args) = @_;
40         send($producer, @args ? "$cmd\0".ipc_freeze(\@args) : $cmd, MSG_EOR);
41 }
42
43 sub close {
44         my ($self) = @_;
45         my $c = $self->{sock} or return;
46         $self->{blocking} ? delete($self->{sock}) : $self->SUPER::close;
47 }
48
49 sub event_step {
50         my ($self) = @_;
51         my $c = $self->{sock};
52         my $msg;
53         while (1) {
54                 my $n = recv($c, $msg, 4096, 0);
55                 unless (defined $n) {
56                         return if $! == EAGAIN;
57                         next if $! == EINTR;
58                         $self->close;
59                         die "recv: $!";
60                 }
61                 my ($cmd, @pargs);
62                 if (index($msg, "\0") > 0) {
63                         ($cmd, my $pargs) = split(/\0/, $msg, 2);
64                         @pargs = @{ipc_thaw($pargs)};
65                 } else {
66                         # for compatibility with the script/lei in client mode,
67                         # it doesn't load Sereal||Storable for startup speed
68                         ($cmd, @pargs) = split(/ /, $msg);
69                 }
70                 my $op = $self->{ops}->{$cmd //= $msg};
71                 die "BUG: unknown message: `$cmd'" unless $op;
72                 my ($sub, @args) = @$op;
73                 $sub->(@args, @pargs);
74                 return $self->close if $msg eq ''; # close on EOF
75         }
76 }
77
78 # call this when we're ready to wait on events,
79 # returns immediately if non-blocking
80 sub op_wait_event {
81         my ($self, $ops) = @_;
82         $self->{ops} = $ops;
83         while ($self->{blocking} && $self->{sock}) { event_step($self) }
84 }
85
86 1;