Hi,
The lack of tools to deal with race conditions and resource locking in Drupal was irritating me lately so I wrote some code to fix my problem. Maybe it will be of use to somebody.
The problem
Web server is threaded and HTTP is stateless. What can happen on high traffic sites is that a user, or multiple users, may try to access some resource (a file, a DB row, a stream, and more often an AJAX request).
A simple example:
In the following HTML form we use a link to submit instead of a submit button (maybe it makes the site look cooler, ask the designers ;-).
<form name="myform">
<input type="text" name="data" />
<a href="" onclick="document.myform.submit(); return false;">The Submit Link</a>
</form>
What can happen here as a user can click the link multiple times thereby sending multiple requests to the web server. If they are timed correctly multiple requests may be processed! The result may be as simple as a duplicate comment or email, but could be as bad as duplicate 1000$ products charged to a clients credit card (uh oh...).
Of course we could use some fancy javascript to make the button unclickable, or many other things, but those solutions sometimes just aren't enough. For instance, if this were a form to redeem a promo code (free iTunes bucks maybe) there might be many people trying to sneak a few extra bucks out of the system. Of course this example can be entirely prevented via row locking in the DB but other problems cannot (those examples are usually more complicated ;-).