perluwsgiplackpsgi

Sending an unbuffered response in Plack


I'm working in a section of a Perl module that creates a large CSV response. The server runs on Plack, on which I'm far from expert.

Currently I'm using something like this to send the response:

$res->content_type('text/csv');
my $body = '';
query_data (
    parameters  => \%query_parameters,
    callback    => sub {
        my $row_object = shift;
        $body .= $row_object->to_csv;
    },
);
$res->body($body);
return $res->finalize;

However, that query_data function is not a fast one and retrieves a lot of records. In there, I'm just concatenating each row into $body and, after all rows are processed, sending the whole response.

I don't like this for two obvious reasons: First, it takes a lot of RAM until $body is destroyed. Second, the user sees no response activity until that method has finished working and actually sends the response with $res->body($body).

I tried to find an answer to this in the documentation without finding what I need.

I also tried calling $res->body($row_object->to_csv) on my callback section, but seems like that ends up sending only the last call I made to $res->body, overriding all previous ones.

Is there a way to send a Plack response that flushes the content on each row, so the user starts receiving content in real time as the data is gathered and without having to accumulate all data into a veriable first?

Thanks in advance for any comments!


Solution

  • You can't use Plack::Response because that class is intended for representing a complete response, and you'll never have a complete response in memory at one time. What you're trying to do is called streaming, and PSGI supports it even if Plack::Response doesn't.

    Here's how you might go about implementing it (adapted from your sample code):

    my $env = shift;
    
    if (!$env->{'psgi.streaming'}) {
        # do something else...
    }
    
    # Immediately start the response and stream the content.
    return sub {
        my $responder = shift;
        my $writer = $responder->([200, ['Content-Type' => 'text/csv']]);
    
        query_data(
            parameters  => \%query_parameters,
            callback    => sub {
                my $row_object = shift;
                $writer->write($row_object->to_csv);
                # TODO: Need to call $writer->close() when there is no more data.
            },
        );
    };
    

    Some interesting things about this code: