Beefy Boxes and Bandwidth Generously Provided by pair Networks
laziness, impatience, and hubris
 
PerlMonks  

Highlander - allow only one invocation at a time of an expensive CGI script

by merlyn (Sage)
on May 22, 2000 at 22:54 UTC ( #14260=snippet: print w/ replies, xml ) Need Help??

Description: My ISP's machine was overloaded this morning because a CGI script that uses a DBI connection was being invoked heavily (in response to a mass mailing of a newsletter). While the proper solution would have been to rewrite this to use mod_perl, I merely coded up a "highlander" solution using a sentinel file that gets flocked.
"You only have one life! If you value it, go home!" -- Connor MacLeod in Highlander
use CGI;
use Fcntl qw(LOCK_EX LOCK_NB);

open HIGHLANDER, ">>/tmp/renew.cgi.highlander" or die "Cannot open hig
+hlander: $!";
{
  my $count = 0;
  {
    flock HIGHLANDER, LOCK_EX | LOCK_NB and last;
    sleep 1;
    redo if ++$count < 10;
    ## couldn't get it after 10 seconds...
    my $host = $ENV{REMOTE_HOST};
    $host = $ENV{REMOTE_ADDR} unless defined $host;
    warn "$0 @ ".(localtime).": highlander abort for $host after 10 se
+conds\n";
    print CGI::header(-status => 503,
                      -retry_after => 30,
                      -type => 'text/plain'),
                        "Our server is overloaded.  Please try again i
+n a few minutes.\n";
    exit 0;
  }
}
## be sure to close HIGHLANDER at end of critical region if using mod_
+perl

Comment on Highlander - allow only one invocation at a time of an expensive CGI script
Download Code
RE: Highlander - allow only one invocation at a time of an expensive CGI script
by Simplicus (Monk) on May 23, 2000 at 19:08 UTC
    excellent. <q>There can be only one.</q>
    Simplicus
RE: Highlander - allow only one invocation at a time of an expensive CGI script
by rjimlad (Acolyte) on May 26, 2000 at 00:25 UTC
    A very impressive-looking script locking mechanism... I just have to wonder if there's a shorter way to do it...

    ----
    Arrr, Jim lad.

RE: Highlander - allow only one invocation at a time of an expensive CGI script
by nature (Initiate) on May 27, 2000 at 03:53 UTC
    I should point out that the need for this snip of code is "my fault" :-). The load comes from a SQL query and not from the perl code. Mod_perl was not an option here as eventually the code will be moved to a machine that does not run mod_perl (excuses, excuses). Regardless, since the query is what creates the load little could be gained by running it under mod_perl with respect to the load generated from the functionality. This is typically not the case as mod_perl often yeilds huge perfomance increases.
RE: Highlander - allow only one invocation at a time of an expensive CGI script
by lindex (Friar) on Jul 19, 2000 at 13:07 UTC
    Well I know that ISP your speaking of (I think) and I know that that was none of my code (i.e I used to work there)
    /****************************/ jason@gost.net, wh@ckz.org http://jason.gost.net /*****************************/

Back to Snippets Section

Log In?
Username:
Password:

What's my password?
Create A New User
Node Status?
node history
Node Type: snippet [id://14260]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others pondering the Monastery: (11)
As of 2015-07-03 07:39 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    The top three priorities of my open tasks are (in descending order of likelihood to be worked on) ...









    Results (49 votes), past polls