X-Git-Url: http://www.git.stargrave.org/?a=blobdiff_plain;f=lib%2FPublicInbox%2FGetlineBody.pm;h=92719a8255c56f46fcf9312ac7c669a6f64104da;hb=95bdac7f09c69036efed537a4d03d5bdd2ae4eb6;hp=4f8765bbe6e7bfd37d67f23a7343fe99535b389a;hpb=79e11eea11dd561301a05c42ba824a7d352bd398;p=public-inbox.git
diff --git a/lib/PublicInbox/GetlineBody.pm b/lib/PublicInbox/GetlineBody.pm
index 4f8765bb..92719a82 100644
--- a/lib/PublicInbox/GetlineBody.pm
+++ b/lib/PublicInbox/GetlineBody.pm
@@ -1,31 +1,49 @@
-# Copyright (C) 2016 all contributors
+# Copyright (C) 2016-2020 all contributors
# License: AGPL-3.0+
# Wrap a pipe or file for PSGI streaming response bodies and calls the
# end callback when the object goes out-of-scope.
# This depends on rpipe being _blocking_ on getline.
+#
+# public-inbox-httpd favors "getline" response bodies to take a
+# "pull"-based approach to feeding slow clients (as opposed to a
+# more common "push" model)
package PublicInbox::GetlineBody;
use strict;
use warnings;
sub new {
- my ($class, $rpipe, $end, $buf) = @_;
- bless { rpipe => $rpipe, end => $end, buf => $buf }, $class;
+ my ($class, $rpipe, $end, $end_arg, $buf) = @_;
+ bless {
+ rpipe => $rpipe,
+ end => $end,
+ end_arg => $end_arg,
+ buf => $buf,
+ filter => 0,
+ }, $class;
}
+# close should always be called after getline returns undef,
+# but a client aborting a connection can ruin our day; so lets
+# hope our underlying PSGI server does not leak references, here.
sub DESTROY { $_[0]->close }
sub getline {
my ($self) = @_;
- my $buf = delete $self->{buf};
- defined $buf ? $buf : $self->{rpipe}->getline;
+ my $filter = $self->{filter};
+ return if $filter == -1; # last call was EOF
+
+ my $buf = delete $self->{buf}; # initial buffer
+ $buf = $self->{rpipe}->getline unless defined $buf;
+ $self->{filter} = -1 unless defined $buf; # set EOF for next call
+ $buf;
}
sub close {
my ($self) = @_;
- delete $self->{rpipe};
- my $end = delete $self->{end} or return;
- $end->();
+ my ($rpipe, $end, $end_arg) = delete @$self{qw(rpipe end end_arg)};
+ close $rpipe if $rpipe;
+ $end->($end_arg) if $end;
}
1;