unofficial mirror of meta@public-inbox.org
 help / color / mirror / Atom feed
* [PATCH] http: avoid recursion when hitting write count limit
@ 2016-06-19  6:32 Eric Wong
  2016-06-19  7:12 ` [PATCH 2/1] http: constrain getline/close responses by time Eric Wong
  0 siblings, 1 reply; 2+ messages in thread
From: Eric Wong @ 2016-06-19  6:32 UTC (permalink / raw)
  To: meta

Use the EvCleanup::asap handler to reschedule our writes
after yielding to other clients.
---
 lib/PublicInbox/HTTP.pm | 5 ++++-
 1 file changed, 4 insertions(+), 1 deletion(-)

diff --git a/lib/PublicInbox/HTTP.pm b/lib/PublicInbox/HTTP.pm
index 6df1c3f..e0ed2d1 100644
--- a/lib/PublicInbox/HTTP.pm
+++ b/lib/PublicInbox/HTTP.pm
@@ -271,9 +271,12 @@ sub getline_response {
 		while ($forward && defined(my $buf = $forward->getline)) {
 			$write->($buf);
 			last if $self->{closed};
-			if ((--$n) <= 0 || $self->{write_buf_size}) {
+			if ($self->{write_buf_size}) {
 				$self->write($self->{pull});
 				return;
+			} elsif ((--$n) <= 0) {
+				PublicInbox::EvCleanup::asap($self->{pull});
+				return;
 			}
 		}
 		$self->{forward} = $self->{pull} = undef;
-- 
EW


^ permalink raw reply related	[flat|nested] 2+ messages in thread

* [PATCH 2/1] http: constrain getline/close responses by time
  2016-06-19  6:32 [PATCH] http: avoid recursion when hitting write count limit Eric Wong
@ 2016-06-19  7:12 ` Eric Wong
  0 siblings, 0 replies; 2+ messages in thread
From: Eric Wong @ 2016-06-19  7:12 UTC (permalink / raw)
  To: meta

This allows us to yield control to other clients gracefully if
getline takes too long to generate a chunk.  This is more
expensive but should not cost a syscall on modern 64-bit systems.
---
 lib/PublicInbox/HTTP.pm | 7 +++++--
 1 file changed, 5 insertions(+), 2 deletions(-)

diff --git a/lib/PublicInbox/HTTP.pm b/lib/PublicInbox/HTTP.pm
index e0ed2d1..800b240 100644
--- a/lib/PublicInbox/HTTP.pm
+++ b/lib/PublicInbox/HTTP.pm
@@ -16,6 +16,7 @@ use Fcntl qw(:seek);
 use Plack::HTTPParser qw(parse_http_request); # XS or pure Perl
 use HTTP::Status qw(status_message);
 use HTTP::Date qw(time2str);
+use Time::HiRes qw(clock_gettime CLOCK_MONOTONIC);
 use Scalar::Util qw(weaken);
 use IO::File;
 use constant {
@@ -25,6 +26,8 @@ use constant {
 	CHUNK_MAX_HDR => 256,
 };
 
+sub now () { clock_gettime(CLOCK_MONOTONIC) }
+
 # FIXME: duplicated code with NNTP.pm, layering violation
 my $WEAKEN = {}; # string(inbox) -> inbox
 my $weakt;
@@ -267,14 +270,14 @@ sub getline_response {
 		my $forward = $self->{forward};
 		# limit our own running time for fairness with other
 		# clients and to avoid buffering too much:
-		my $n = 100;
+		my $end = now() + 0.1;
 		while ($forward && defined(my $buf = $forward->getline)) {
 			$write->($buf);
 			last if $self->{closed};
 			if ($self->{write_buf_size}) {
 				$self->write($self->{pull});
 				return;
-			} elsif ((--$n) <= 0) {
+			} elsif (now() > $end) {
 				PublicInbox::EvCleanup::asap($self->{pull});
 				return;
 			}

^ permalink raw reply related	[flat|nested] 2+ messages in thread

end of thread, other threads:[~2016-06-19  7:12 UTC | newest]

Thread overview: 2+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2016-06-19  6:32 [PATCH] http: avoid recursion when hitting write count limit Eric Wong
2016-06-19  7:12 ` [PATCH 2/1] http: constrain getline/close responses by time Eric Wong

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).