subclass of Plack::Response for streaming

75 views
Skip to first unread message

dstroma

unread,
Dec 6, 2011, 9:49:59 PM12/6/11
to psgi-...@googlegroups.com
Hello group,

I wrote a subclass of Plack::Response to implement the streaming/delayed response body part of the PSGI specification. I have never published a module on CPAN and wondered if this would be useful to anyone.

I have attached the module and a test script as well. Reviews and comments appreciated.

Thanks.
Streamable.pm
Streamable.t

Zbigniew Łukasiak

unread,
Dec 8, 2011, 4:56:56 AM12/8/11
to psgi-...@googlegroups.com

I am not sure if I understand well the whole design, but in the name
of brainstorming - one idea for you to consider: make the
$response_starter a parameter to the new call. This way you could get
rid of the ->prepare method. I for one like when objects are created
ready-to-work.


--
Zbigniew Lukasiak
http://brudnopis.blogspot.com/
http://perlalchemy.blogspot.com/

dstroma

unread,
Dec 9, 2011, 8:09:25 PM12/9/11
to psgi-plack
On Dec 8, 4:56 am, Zbigniew Łukasiak <zzb...@gmail.com> wrote:
>
> I am not sure if I understand well the whole design, but in the name
> of brainstorming - one idea for you to consider: make the
> $response_starter a parameter to the new call. This way you could get
> rid of the ->prepare method. I for one like when objects are created
> ready-to-work.

Thanks for your input. Making the response starter a param to new()
would not get rid of the prepare() call, because with PSGI you cannot
get a "writer object" until after you have sent the headers... and
setting headers in the new() call is not mandatory (see
Plack::Response).I suppose in the name of making the interface more
intuitive, the name of prepare() could be changed to something such as
"send_headers()" or "finalize_headers()". Would that help?

At the least I should probably change prepare() so that it returns the
object:

my $resp = Plack::Response::Streamable->new(200)-
>prepare($response_starter);
$resp->write('Hello world!');

The other reason that I designed it this way was so it could be a drop-
in replacement for Plack::Response (thus I named the module
"Streamable" and not "Streaming").

Tatsuhiko Miyagawa

unread,
Dec 9, 2011, 8:24:36 PM12/9/11
to psgi-...@googlegroups.com
Although I haven't looked at the code yet, streaming support for Plack::Response has already been on the issues for a quite long time: https://github.com/miyagawa/Plack/issues/80 although I quite didn't take any time to figure out the API, since the bare API that uses the PSGI callback isn't as bad:

    return sub {
        my $responder = shift;
        my $res = Plack::Response->new(...);
        my $writer = $res->respond($res->finalize);
        $writer->write(...);
        $writer->finish();
    };


-- 
Tatsuhiko Miyagawa
Sent with Sparrow

Reply all
Reply to author
Forward
0 new messages