non blocking UserAgent in request

85 views
Skip to first unread message

vitas

unread,
Jun 8, 2012, 5:41:49 AM6/8/12
to Mojolicious
Hi,

I'm trying send uploaded file from 'webfrontend' to file-storage
server
backend. Idea is get file, response to client with 'thank-you.html',
and then upload file to http://backendserver.localdomain/upload.

post upload => sub {
my $ctrl = shift;
my $upload = $ctrl->upload('file');
if( $upload ) {
$upload->move_to(temp-uploaded-file.txt');
$ctrl->ua->post_form('http://backendserver.localdomain/upload'
=>
{ file => { file => 'temp-uploaded-file.txt' }},
sub { "to make this non-blocking"; }
});
}
} => 'thank-you';


But when uploading large file (~200MB) (upload takes less then 5s) it
hangs for looong time (in minutes).

It does not work even if post_form() is in blocking form (without the
'sub {...}' parameter), and time response is more longer than
expected twice upload time of single file.

Any idea?

Thank you

vitas
@;;

vitas

unread,
Jun 8, 2012, 10:45:37 AM6/8/12
to Mojolicious
Hi,

I solve it: problem is multipart boundary.
In sub Content::MultiPart::build_boundary(),
is file read (and not only once) unless boundary is already specified.

My workaround is:

post upload => sub {
      my $ctrl = shift;
      my $upload = $ctrl->upload('file');
      if( $upload ) {
$upload->move_to('temp-uploaded-file.txt');
my $tx = $ctrl->ua->build_form_tx(
'http://backendserver.localdomain/upload'
=>
                        {file => {file => 'temp-uploaded-
file.txt' }});

# boudnary workaround
my $ct = $tx->req->headers->content_type;
my $b = gen_random_boundary();
$tx->req->headers->content_type("$ct; boundary=$b);

$ctrl->ua->start($tx, sub { "to make this non-blocking"; });
      }

} => 'thank-you';

But I think, it should be considered as bug, or give programmer more
convenient way to say 'I don't want Mojolicous reads all upload file
more than
once before actual upload'.

vitas
@;;
Reply all
Reply to author
Forward
0 new messages