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
|
---|
Replies are listed 'Best First'. | |
---|---|
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 | |
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 | |
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 | |
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 |
Back to
Cool Uses for Perl