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 3/3] nntp: share more code between art_lookup callers
Date: Fri, 11 Sep 2020 07:32:33 +0000	[thread overview]
Message-ID: <20200911073233.29895-4-e@80x24.org> (raw)
In-Reply-To: <20200911073233.29895-1-e@80x24.org>

This prepares us for future changes to improve scalability to
many inboxes.
---
 lib/PublicInbox/NNTP.pm | 44 +++++++++++++++++------------------------
 1 file changed, 18 insertions(+), 26 deletions(-)

diff --git a/lib/PublicInbox/NNTP.pm b/lib/PublicInbox/NNTP.pm
index 46398cd4..88fe2bb0 100644
--- a/lib/PublicInbox/NNTP.pm
+++ b/lib/PublicInbox/NNTP.pm
@@ -446,8 +446,8 @@ sub set_nntp_headers ($$) {
 	}
 }
 
-sub art_lookup ($$) {
-	my ($self, $art) = @_;
+sub art_lookup ($$$) {
+	my ($self, $art, $code) = @_;
 	my $ng = $self->{ng};
 	my ($n, $mid);
 	my $err;
@@ -484,7 +484,17 @@ find_mid:
 found:
 	my $smsg = $ng->over->get_art($n) or return $err;
 	$smsg->{-ibx} = $ng;
-	$smsg;
+	if ($code == 223) { # STAT
+		set_art($self, $n);
+		"223 $n <$smsg->{mid}> article retrieved - " .
+			"request text separately";
+	} else { # HEAD | BODY | ARTICLE
+		$smsg->{nntp} = $self;
+		$smsg->{nntp_code} = $code;
+		set_art($self, $art);
+		# this dereferences to `undef'
+		${git_async_cat($ng->git, $smsg->{blob}, \&blob_cb, $smsg)};
+	}
 }
 
 sub msg_body_write ($$) {
@@ -520,7 +530,7 @@ sub msg_hdr_write ($$) {
 sub blob_cb { # called by git->cat_async via git_async_cat
 	my ($bref, $oid, $type, $size, $smsg) = @_;
 	my $self = $smsg->{nntp};
-	my $code = $smsg->{nntp_code} // 220;
+	my $code = $smsg->{nntp_code};
 	if (!defined($oid)) {
 		# it's possible to have TOCTOU if an admin runs
 		# public-inbox-(edit|purge), just move onto the next message
@@ -553,40 +563,22 @@ sub blob_cb { # called by git->cat_async via git_async_cat
 
 sub cmd_article ($;$) {
 	my ($self, $art) = @_;
-	my $smsg = art_lookup($self, $art);
-	return $smsg unless ref $smsg;
-	set_art($self, $art);
-	$smsg->{nntp} = $self;
-	${git_async_cat($smsg->{-ibx}->git, $smsg->{blob}, \&blob_cb, $smsg)};
+	art_lookup($self, $art, 220);
 }
 
 sub cmd_head ($;$) {
 	my ($self, $art) = @_;
-	my $smsg = art_lookup($self, $art);
-	return $smsg unless ref $smsg;
-	set_art($self, $art);
-	$smsg->{nntp} = $self;
-	$smsg->{nntp_code} = 221;
-	${git_async_cat($smsg->{-ibx}->git, $smsg->{blob}, \&blob_cb, $smsg)};
+	art_lookup($self, $art, 221);
 }
 
 sub cmd_body ($;$) {
 	my ($self, $art) = @_;
-	my $smsg = art_lookup($self, $art);
-	return $smsg unless ref $smsg;
-	set_art($self, $art);
-	$smsg->{nntp} = $self;
-	$smsg->{nntp_code} = 222;
-	${git_async_cat($smsg->{-ibx}->git, $smsg->{blob}, \&blob_cb, $smsg)};
+	art_lookup($self, $art, 222);
 }
 
 sub cmd_stat ($;$) {
 	my ($self, $art) = @_;
-	my $smsg = art_lookup($self, $art); # art may be msgid
-	return $smsg unless ref $smsg;
-	$art = $smsg->{num};
-	set_art($self, $art);
-	"223 $art <$smsg->{mid}> article retrieved - request text separately";
+	art_lookup($self, $art, 223); # art may be msgid
 }
 
 sub cmd_ihave ($) { '435 article not wanted - do not send it' }

      parent reply	other threads:[~2020-09-11  7:32 UTC|newest]

Thread overview: 4+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2020-09-11  7:32 [PATCH 0/3] mostly NNTP stuff Eric Wong
2020-09-11  7:32 ` [PATCH 1/3] treewide: avoid `goto &NAME' for tail recursion Eric Wong
2020-09-11  7:32 ` [PATCH 2/3] t/nntpd: add test for the XPATH command Eric Wong
2020-09-11  7:32 ` Eric Wong [this message]

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: http://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=20200911073233.29895-4-e@80x24.org \
    --to=e@80x24.org \
    --cc=meta@public-inbox.org \
    /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
Be sure your reply has a Subject: header at the top and a blank line before the message body.
Code repositories for project(s) associated with this public 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).