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
-
Are you posting in the right place? Check out Where do I post X? to know for sure.
-
Posts may use any of the Perl Monks Approved HTML tags. Currently these include the following:
<code> <a> <b> <big>
<blockquote> <br /> <dd>
<dl> <dt> <em> <font>
<h1> <h2> <h3> <h4>
<h5> <h6> <hr /> <i>
<li> <nbsp> <ol> <p>
<small> <strike> <strong>
<sub> <sup> <table>
<td> <th> <tr> <tt>
<u> <ul>
-
Snippets of code should be wrapped in
<code> tags not
<pre> tags. In fact, <pre>
tags should generally be avoided. If they must
be used, extreme care should be
taken to ensure that their contents do not
have long lines (<70 chars), in order to prevent
horizontal scrolling (and possible janitor
intervention).
-
Want more info? How to link
or How to display code and escape characters
are good places to start.
|