user/dev discussion of public-inbox itself
 help / color / mirror / code / Atom feed
4c434566d31f90104d921b76adf1844a9d6aefde blob 1891 bytes (raw)

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
 
# Copyright (C) 2021 all contributors <meta@public-inbox.org>
# License: AGPL-3.0+ <https://www.gnu.org/licenses/agpl-3.0.txt>

# op dispatch socket, reads a message, runs a sub
# There may be multiple producers, but (for now) only one consumer
# Used for lei_xsearch and maybe other things
# "command" => [ $sub, @fixed_operands ]
package PublicInbox::PktOp;
use strict;
use v5.10.1;
use parent qw(PublicInbox::DS);
use Errno qw(EAGAIN ECONNRESET);
use PublicInbox::Syscall qw(EPOLLIN);
use Socket qw(AF_UNIX MSG_EOR SOCK_SEQPACKET);
use PublicInbox::IPC qw(ipc_freeze ipc_thaw);
use Scalar::Util qw(blessed);

sub new {
	my ($cls, $r) = @_;
	my $self = bless { sock => $r }, $cls;
	$r->blocking(0);
	$self->SUPER::new($r, EPOLLIN);
}

# returns a blessed objects as the consumer and producer
sub pair {
	my ($cls) = @_;
	my ($c, $p);
	socketpair($c, $p, AF_UNIX, SOCK_SEQPACKET, 0) or die "socketpair: $!";
	(new($cls, $c), bless { op_p => $p }, $cls);
}

sub pkt_do { # for the producer to trigger event_step in consumer
	my ($self, $cmd, @args) = @_;
	send($self->{op_p}, @args ? "$cmd\0".ipc_freeze(\@args) : $cmd, MSG_EOR)
}

sub event_step {
	my ($self) = @_;
	my $c = $self->{sock};
	my $n = recv($c, my $msg, 4096, 0);
	unless (defined $n) {
		return if $! == EAGAIN;
		die "recv: $!" if $! != ECONNRESET; # we may be bidirectional
	}
	my ($cmd, @pargs);
	if (index($msg, "\0") > 0) {
		($cmd, my $pargs) = split(/\0/, $msg, 2);
		@pargs = @{ipc_thaw($pargs)};
	} else {
		# for compatibility with the script/lei in client mode,
		# it doesn't load Sereal||Storable for startup speed
		($cmd, @pargs) = split(/ /, $msg);
	}
	my $op = $self->{ops}->{$cmd //= $msg};
	if ($op) {
		my ($obj, @args) = (@$op, @pargs);
		blessed($obj) ? $obj->$cmd(@args) : $obj->(@args);
	} elsif ($msg ne '') {
		die "BUG: unknown message: `$cmd'";
	}
	$self->close if $msg eq ''; # close on EOF
}

1;
debug log:

solving 4c434566d31f ...
found 4c434566d31f in https://80x24.org/public-inbox.git

Code repositories for project(s) associated with this inbox:

	https://80x24.org/public-inbox.git

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).