]> Sergey Matveev's repositories - public-inbox.git/commitdiff
nntp: event_step: prepare for async git reads
authorEric Wong <e@yhbt.net>
Tue, 16 Jun 2020 22:31:21 +0000 (22:31 +0000)
committerEric Wong <e@yhbt.net>
Sun, 21 Jun 2020 00:42:34 +0000 (00:42 +0000)
This matches PublicInbox::IMAP::event_step and will allow us to
handle blob retrievals from git asynchronously without falling
over on pipelined requests.

lib/PublicInbox/NNTP.pm
lib/PublicInbox/NNTPdeflate.pm

index be3bddc3f5d2a230642b4bf2dc878f7b6c5aec73..80dd8614fe801b26f57ba79825e3d938475f333e 100644 (file)
@@ -956,38 +956,35 @@ sub out ($$;@) {
 sub event_step {
        my ($self) = @_;
 
-       return unless $self->flush_write && $self->{sock};
+       return unless $self->flush_write && $self->{sock} && !$self->{long_cb};
 
        $self->update_idle_time;
        # only read more requests if we've drained the write buffer,
        # otherwise we can be buffering infinitely w/o backpressure
 
-       my $rbuf = $self->{rbuf} // (\(my $x = ''));
-       my $r = 1;
-
-       if (index($$rbuf, "\n") < 0) {
-               my $off = bytes::length($$rbuf);
-               $r = $self->do_read($rbuf, LINE_MAX, $off) or return;
-       }
-       while ($r > 0 && $$rbuf =~ s/\A[ \t]*([^\n]*?)\r?\n//) {
-               my $line = $1;
-               return $self->close if $line =~ /[[:cntrl:]]/s;
-               my $t0 = now();
-               my $fd = fileno($self->{sock});
-               $r = eval { process_line($self, $line) };
-               my $pending = $self->{wbuf} ? ' pending' : '';
-               out($self, "[$fd] %s - %0.6f$pending", $line, now() - $t0);
-       }
-
+       my $rbuf = $self->{rbuf} // \(my $x = '');
+       my $line = index($$rbuf, "\n");
+       while ($line < 0) {
+               return $self->close if length($$rbuf) >= LINE_MAX;
+               $self->do_read($rbuf, LINE_MAX, length($$rbuf)) or return;
+               $line = index($$rbuf, "\n");
+       }
+       $line = substr($$rbuf, 0, $line + 1, '');
+       $line =~ s/\r?\n\z//s;
+       return $self->close if $line =~ /[[:cntrl:]]/s;
+
+       my $t0 = now();
+       my $fd = fileno($self->{sock});
+       my $r = eval { process_line($self, $line) };
+       my $pending = $self->{wbuf} ? ' pending' : '';
+       out($self, "[$fd] %s - %0.6f$pending", $line, now() - $t0);
        return $self->close if $r < 0;
-       my $len = bytes::length($$rbuf);
-       return $self->close if ($len >= LINE_MAX);
        $self->rbuf_idle($rbuf);
        $self->update_idle_time;
 
        # maybe there's more pipelined data, or we'll have
        # to register it for socket-readiness notifications
-       $self->requeue unless $self->{wbuf};
+       $self->requeue unless $pending;
 }
 
 # for graceful shutdown in PublicInbox::Daemon:
index eb400c9c22057da11b5bd3d3d396ab79213c7108..dec88aba3a5f22d90af5c3fd8afb6a9504362f4c 100644 (file)
@@ -71,6 +71,16 @@ sub do_read ($$$$) {
        $doff = length($dbuf);
        my $r = PublicInbox::DS::do_read($self, \$dbuf, $len, $doff) or return;
 
+       # Workaround inflate bug appending to OOK scalars:
+       # <https://rt.cpan.org/Ticket/Display.html?id=132734>
+       # We only have $off if the client is pipelining, and pipelining
+       # is where our substr() OOK optimization in event_step makes sense.
+       if ($off) {
+               my $copy = $$rbuf;
+               undef $$rbuf;
+               $$rbuf = $copy;
+       }
+
        # assert(length($$rbuf) == $off) as far as NNTP.pm is concerned
        # -ConsumeInput is true, so $dbuf is automatically emptied
        my $err = $zin->inflate($dbuf, $rbuf);