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

In reply to Highlander - allow only one invocation at a time of an expensive CGI script by merlyn

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.