user/dev discussion of public-inbox itself
 help / color / Atom feed
From: Eric Wong <e@80x24.org>
To: meta@public-inbox.org
Subject: [PATCH 6/9] nntp: use sysread to append to existing buffer
Date: Mon, 10 Jun 2019 05:18:43 +0000
Message-ID: <20190610051846.26757-7-e@80x24.org> (raw)
In-Reply-To: <20190610051846.26757-1-e@80x24.org>

We already do this in PublicInbox::HTTP, as it's superior to
DS::read in this regard.  Initially (when I started writing
NNTP.pm, I wanted to use Danga::Socket's read buffering and
push_back_read (removed in DS) but quickly figured out it wasn't
useful at all for dealing with trickling clients.
---
 lib/PublicInbox/NNTP.pm | 21 ++++++++++++++-------
 1 file changed, 14 insertions(+), 7 deletions(-)

diff --git a/lib/PublicInbox/NNTP.pm b/lib/PublicInbox/NNTP.pm
index 7729399a..5e66d077 100644
--- a/lib/PublicInbox/NNTP.pm
+++ b/lib/PublicInbox/NNTP.pm
@@ -954,13 +954,20 @@ sub event_write {
 sub event_read {
 	my ($self) = @_;
 	use constant LINE_MAX => 512; # RFC 977 section 2.3
-
-	if (index($self->{rbuf}, "\n") < 0) {
-		my $buf = $self->read(LINE_MAX) or return $self->close;
-		$self->{rbuf} .= $$buf;
+	my $rbuf = \($self->{rbuf});
+	my $r;
+
+	if (index($$rbuf, "\n") < 0) {
+		my $off = length($$rbuf);
+		$r = sysread($self->{sock}, $$rbuf, LINE_MAX, $off);
+		unless (defined $r) {
+			return if $!{EAGAIN};
+			return $self->close;
+		}
+		return $self->close if $r == 0;
 	}
-	my $r = 1;
-	while ($r > 0 && $self->{rbuf} =~ s/\A[ \t\r\n]*([^\r\n]*)\r?\n//) {
+	$r = 1;
+	while ($r > 0 && $$rbuf =~ s/\A[ \t\r\n]*([^\r\n]*)\r?\n//) {
 		my $line = $1;
 		return $self->close if $line =~ /[[:cntrl:]]/s;
 		my $t0 = now();
@@ -972,7 +979,7 @@ sub event_read {
 	}
 
 	return $self->close if $r < 0;
-	my $len = length($self->{rbuf});
+	my $len = length($$rbuf);
 	return $self->close if ($len >= LINE_MAX);
 	update_idle_time($self);
 }
-- 
EW


  parent reply index

Thread overview: 10+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2019-06-10  5:18 [PATCH 0/9] ds: Diet Socket Eric Wong
2019-06-10  5:18 ` [PATCH 1/9] ds: simplify write buffer accounting Eric Wong
2019-06-10  5:18 ` [PATCH 2/9] ds: cleanup Errno imports and favor constant comparisons Eric Wong
2019-06-10  5:18 ` [PATCH 3/9] ds: reduce Errno imports and drop ->close reason Eric Wong
2019-06-10  5:18 ` [PATCH 4/9] ds: remove {fd} field Eric Wong
2019-06-10  5:18 ` [PATCH 5/9] ds: remove steal_socket method Eric Wong
2019-06-10  5:18 ` Eric Wong [this message]
2019-06-10  5:18 ` [PATCH 7/9] ds: remove read method, here, too Eric Wong
2019-06-10  5:18 ` [PATCH 8/9] ds: do not distinguish between POLLHUP and POLLERR Eric Wong
2019-06-10  5:18 ` [PATCH 9/9] ds: stop caring about event flags set by epoll/poll/kqueue Eric Wong

Reply instructions:

You may reply publically 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=20190610051846.26757-7-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

user/dev discussion of public-inbox itself

Archives are clonable:
	git clone --mirror http://public-inbox.org/meta
	git clone --mirror http://czquwvybam4bgbro.onion/meta
	git clone --mirror http://hjrcffqmbrq6wope.onion/meta
	git clone --mirror http://ou63pmih66umazou.onion/meta

Newsgroups are available over NNTP:
	nntp://news.public-inbox.org/inbox.comp.mail.public-inbox.meta
	nntp://ou63pmih66umazou.onion/inbox.comp.mail.public-inbox.meta
	nntp://czquwvybam4bgbro.onion/inbox.comp.mail.public-inbox.meta
	nntp://hjrcffqmbrq6wope.onion/inbox.comp.mail.public-inbox.meta
	nntp://news.gmane.org/gmane.mail.public-inbox.general

 note: .onion URLs require Tor: https://www.torproject.org/

AGPL code for this site: git clone https://public-inbox.org/ public-inbox