From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.2 (2018-09-13) on dcvr.yhbt.net X-Spam-Level: X-Spam-ASN: X-Spam-Status: No, score=-4.0 required=3.0 tests=ALL_TRUSTED,BAYES_00 shortcircuit=no autolearn=ham autolearn_force=no version=3.4.2 Received: from localhost (dcvr.yhbt.net [127.0.0.1]) by dcvr.yhbt.net (Postfix) with ESMTP id 75C411F8C8 for ; Sat, 25 Sep 2021 06:17:54 +0000 (UTC) From: Eric Wong 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 Message-Id: <20210925061754.7434-1-e@80x24.org> MIME-Version: 1.0 Content-Transfer-Encoding: 8bit List-Id: 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'"; }