public-inbox.git  about / heads / tags
an "archives first" approach to mailing lists
blob ea07f3d6dd0de1d031012c62372754dbc91acd49 1141 bytes (raw)
$ git show viewdiff:lib/PublicInbox/GetlineBody.pm	# shows this blob on the CLI

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
 
# Copyright (C) 2016-2018 all contributors <meta@public-inbox.org>
# License: AGPL-3.0+ <https://www.gnu.org/licenses/agpl-3.0.txt>

# 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;
}

# 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}; # initial buffer
	defined $buf ? $buf : $self->{rpipe}->getline;
}

sub close {
	my ($self) = @_;
	my $rpipe = delete $self->{rpipe};
	close $rpipe if $rpipe;
	my $end = delete $self->{end};
	$end->() if $end;
}

1;

git clone https://public-inbox.org/public-inbox.git
git clone http://7fh6tueqddpjyxjmgtdiueylzoqt6pt7hec3pukyptlmohoowvhde4yd.onion/public-inbox.git