user/dev discussion of public-inbox itself
 help / color / mirror / code / Atom feed
From: Eric Wong <e@80x24.org>
To: meta@public-inbox.org
Subject: [PATCH] lei: make pkt_op easier-to-use and understand
Date: Sat, 25 Sep 2021 06:17:54 +0000	[thread overview]
Message-ID: <20210925061754.7434-1-e@80x24.org> (raw)

Since switching to SOCK_SEQUENTIAL, we no longer have to use
fixed-width records to guarantee atomic reads.  Thus we can
maintain more human-readable/searchable PktOp opcodes.

Furthermore, we can infer the subroutine name in many cases
to avoid repeating ourselves by specifying a command-name
twice (e.g. $ops->{CMD} => [ \&CMD, $obj ]; can now simply be
written as: $ops->{CMD} => [ $obj ]  if CMD is a method of
$obj.
---
 lib/PublicInbox/LEI.pm        | 15 +++++++--------
 lib/PublicInbox/LeiToMail.pm  |  7 +++----
 lib/PublicInbox/LeiXSearch.pm | 14 +++++++-------
 lib/PublicInbox/PktOp.pm      |  5 +++--
 4 files changed, 20 insertions(+), 21 deletions(-)

diff --git a/lib/PublicInbox/LEI.pm b/lib/PublicInbox/LEI.pm
index 3ff8a347af8b..a337fb0d80c2 100644
--- a/lib/PublicInbox/LEI.pm
+++ b/lib/PublicInbox/LEI.pm
@@ -519,8 +519,7 @@ sub fail ($$;$) {
 	my ($self, $buf, $exit_code) = @_;
 	$self->{failed}++;
 	err($self, $buf) if defined $buf;
-	# calls fail_handler
-	$self->{pkt_op_p}->pkt_do('!') if $self->{pkt_op_p};
+	$self->{pkt_op_p}->pkt_do('fail_handler') if $self->{pkt_op_p};
 	x_it($self, ($exit_code // 1) << 8);
 	undef;
 }
@@ -552,7 +551,7 @@ sub child_error { # passes non-fatal curl exit codes to user
 sub note_sigpipe { # triggers sigpipe_handler
 	my ($self, $fd) = @_;
 	close(delete($self->{$fd})); # explicit close silences Perl warning
-	$self->{pkt_op_p}->pkt_do('|') if $self->{pkt_op_p};
+	$self->{pkt_op_p}->pkt_do('sigpipe_handler') if $self->{pkt_op_p};
 	x_it($self, 13);
 }
 
@@ -614,11 +613,11 @@ sub incr {
 
 sub pkt_ops {
 	my ($lei, $ops) = @_;
-	$ops->{'!'} = [ \&fail_handler, $lei ];
-	$ops->{'|'} = [ \&sigpipe_handler, $lei ];
-	$ops->{x_it} = [ \&x_it, $lei ];
-	$ops->{child_error} = [ \&child_error, $lei ];
-	$ops->{incr} = [ \&incr, $lei ];
+	$ops->{fail_handler} = [ $lei ];
+	$ops->{sigpipe_handler} = [ $lei ];
+	$ops->{x_it} = [ $lei ];
+	$ops->{child_error} = [ $lei ];
+	$ops->{incr} = [ $lei ];
 	$ops;
 }
 
diff --git a/lib/PublicInbox/LeiToMail.pm b/lib/PublicInbox/LeiToMail.pm
index 467b27bf275d..d42759cf71d4 100644
--- a/lib/PublicInbox/LeiToMail.pm
+++ b/lib/PublicInbox/LeiToMail.pm
@@ -714,16 +714,15 @@ sub do_post_auth {
 		} else { # Maildir
 			$self->{shard_info} = [ $mod, $shard ];
 		}
-		$aug = '+'; # incr_post_augment
+		$aug = 'incr_post_augment';
 	} elsif ($self->{-wq_worker_nr} == 0) { # 1st worker do_augment
-		$aug = '.'; # do_post_augment
+		$aug = 'do_post_augment';
 	}
 	if ($aug) {
 		local $0 = 'do_augment';
 		eval { do_augment($self, $lei) };
 		$lei->fail($@) if $@;
-		$lei->{pkt_op_p}->pkt_do($aug) == 1 or
-				die "do_post_augment trigger: $!";
+		$lei->{pkt_op_p}->pkt_do($aug) or die "pkt_do($aug): $!";
 	}
 	# done augmenting, connect the compressor pipe for each worker
 	if (my $zpipe = delete $lei->{zpipe}) {
diff --git a/lib/PublicInbox/LeiXSearch.pm b/lib/PublicInbox/LeiXSearch.pm
index 99f887ce730f..ae9f5881676d 100644
--- a/lib/PublicInbox/LeiXSearch.pm
+++ b/lib/PublicInbox/LeiXSearch.pm
@@ -536,16 +536,16 @@ sub do_query {
 	my ($self, $lei) = @_;
 	my $l2m = $lei->{l2m};
 	my $ops = {
-		'|' => [ $lei->can('sigpipe_handler'), $lei ],
-		'!' => [ $lei->can('fail_handler'), $lei ],
-		'.' => [ \&do_post_augment, $lei ],
-		'+' => [ \&incr_post_augment, $lei ],
+		'sigpipe_handler' => [ $lei ],
+		'fail_handler' => [ $lei ],
+		'do_post_augment' => [ \&do_post_augment, $lei ],
+		'incr_post_augment' => [ \&incr_post_augment, $lei ],
 		'' => [ \&query_done, $lei ],
 		'mset_progress' => [ \&mset_progress, $lei ],
 		'l2m_progress' => [ \&l2m_progress, $lei ],
-		'x_it' => [ $lei->can('x_it'), $lei ],
-		'child_error' => [ $lei->can('child_error'), $lei ],
-		'incr_start_query' => [ \&incr_start_query, $self, $l2m ],
+		'x_it' => [ $lei ],
+		'child_error' => [ $lei ],
+		'incr_start_query' => [ $self, $l2m ],
 	};
 	$lei->{auth}->op_merge($ops, $l2m) if $l2m && $lei->{auth};
 	my $end = $lei->pkt_op_pair;
diff --git a/lib/PublicInbox/PktOp.pm b/lib/PublicInbox/PktOp.pm
index 10942dd19b68..fd2569badd74 100644
--- a/lib/PublicInbox/PktOp.pm
+++ b/lib/PublicInbox/PktOp.pm
@@ -13,6 +13,7 @@ use Errno qw(EAGAIN EINTR);
 use PublicInbox::Syscall qw(EPOLLIN EPOLLET);
 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) = @_;
@@ -57,8 +58,8 @@ sub event_step {
 		}
 		my $op = $self->{ops}->{$cmd //= $msg};
 		if ($op) {
-			my ($sub, @args) = @$op;
-			$sub->(@args, @pargs);
+			my ($obj, @args) = (@$op, @pargs);
+			blessed($obj) ? $obj->$cmd(@args) : $obj->(@args);
 		} elsif ($msg ne '') {
 			die "BUG: unknown message: `$cmd'";
 		}

                 reply	other threads:[~2021-09-25  6:17 UTC|newest]

Thread overview: [no followups] expand[flat|nested]  mbox.gz  Atom feed

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

  List information: https://public-inbox.org/README

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=20210925061754.7434-1-e@80x24.org \
    --to=e@80x24.org \
    --cc=meta@public-inbox.org \
    --subject='Re: [PATCH] lei: make pkt_op easier-to-use and understand' \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link

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).