http://www.perlmonks.org?node_id=14260
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