From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) 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, URIBL_BLOCKED shortcircuit=no autolearn=unavailable autolearn_force=no version=3.4.0 Received: from localhost (dcvr.yhbt.net [127.0.0.1]) by dcvr.yhbt.net (Postfix) with ESMTP id 629391F71A for ; Mon, 30 May 2016 04:54:07 +0000 (UTC) From: Eric Wong To: meta@public-inbox.org Subject: [PATCH] http: yield body->getline running time Date: Mon, 30 May 2016 04:54:07 +0000 Message-Id: <20160530045407.22267-1-e@80x24.org> List-Id: We cannot let a client monopolize the single-threaded server even if it can drain the socket buffer faster than we can emit data. While we're at it, acknowledge the this behavior (which happens naturally) in httpd/async. The same idea is present in NNTP for the long_response code. This is the HTTP followup to: commit 0d0fde0bff97 ("nntp: introduce long response API for streaming") commit 79d8bfedcdd2 ("nntp: avoid signals for long responses") --- lib/PublicInbox/HTTP.pm | 5 ++++- lib/PublicInbox/HTTPD/Async.pm | 4 +++- 2 files changed, 7 insertions(+), 2 deletions(-) diff --git a/lib/PublicInbox/HTTP.pm b/lib/PublicInbox/HTTP.pm index fcbd758..6df1c3f 100644 --- a/lib/PublicInbox/HTTP.pm +++ b/lib/PublicInbox/HTTP.pm @@ -265,10 +265,13 @@ sub getline_response { my $pull = $self->{pull} = sub { local $/ = \8192; my $forward = $self->{forward}; + # limit our own running time for fairness with other + # clients and to avoid buffering too much: + my $n = 100; while ($forward && defined(my $buf = $forward->getline)) { $write->($buf); last if $self->{closed}; - if ($self->{write_buf_size}) { + if ((--$n) <= 0 || $self->{write_buf_size}) { $self->write($self->{pull}); return; } diff --git a/lib/PublicInbox/HTTPD/Async.pm b/lib/PublicInbox/HTTPD/Async.pm index add07ce..fadf2d3 100644 --- a/lib/PublicInbox/HTTPD/Async.pm +++ b/lib/PublicInbox/HTTPD/Async.pm @@ -45,7 +45,9 @@ sub async_pass { $self->watch_read(0); $io->write($restart_read); # D::S::write } - return; # stay in watch_read + # stay in watch_read, but let other clients + # get some work done, too. + return; } elsif (!defined $r) { return if $!{EAGAIN} || $!{EINTR}; }