[cgiapp] Keep connection alive for request that takes long time to run

Michael Peters mpeters at plusthree.com
Wed Feb 10 10:43:51 EST 2010


On 02/10/2010 10:38 AM, Shao, David (NIH/NLM/NCBI) [C] wrote:

> I use CGI::Application for my web app. The app sometimes takes longer to run than the timeout limit set by the apache server, which results in a 'Bad Gateway' page at user side. My web admin suggested I need to implement something similar to 'heartbeat' to keep the connection alive. My interpretation to this is, while my app is running, it needs to send a 'keepalive' packet to the client in short intervals (i.e. shorter than the timeout limit), to prevent the server from dropping the connection. I am fairly new to CGI::Application, and my questions are if this feature already exists (but I can't find it in documentation), or if it is not, is it possible to implement using CGI::Application?

The is similar to what the C::A::Plugin::Stream does. Basically you tell 
C::A not to send headers and then you print your own response. The 
difference being that you don't want to send actual content while your 
app is working, but just empty data to hold the connection. Something 
like this:

   $self->header_type('none');
   my $not_done = 0;
   while($not_done) {
     print "\0"; # send null byte to keep the connection open
     ... # do some work
   }

If the long running task you want to do can't be broken up into chunks 
such that you can print a null byte, then you need to rethink your 
architecture. Probably best to think about having a job queue which does 
the work and a URL that your page contantly hits to figure out if the 
job is done. When it's done you can then redirect the client to where 
you want them to go.

-- 
Michael Peters
Plus Three, LP


More information about the cgiapp mailing list