Again and again I face the situation when I have to setup a time delay in the web app I’m working on. Actually, such delays and checks that suspend the web app flow can be very useful. In this short post, I’d like to describe a couple of the most common cases where time delays make sense.
The first case is when you request some remote source over the network: data base, web service, mail boxes, SMTP service, whatever… It would be good if you could suggest in your code the time delays and retry attempts in case of network issues. For instance, recently I worked with the Highrise API and discovered that the remote server drops my connections if I perform frequent requests. Quite a smart technique to protect from potential hackers and reduce load. If you need to do a bunch of API calls to update account data, the time delays in your code can help greatly. Just insert the thread delays between web calls. In .NET you can use Threed.Sleep() construction. Also, make sure your procedure does several retry attempts if the last web call failed. This approach will make your code routine much more stable to any network failures.
In the second case, you may be required to limit access to the resources of your web app. It’s exactly the opposite from what I described above. As a developer you may want to protect your site from frequent user requests. A very common situation where it can fit is to develop the time check between login attempts in the site logon procedure. Technically, it’s very easy to code by storing the time of the last login failure to a web session variable. If a next login call happens before the check time is due, you can extend all future login attempts for an even longer time interval. This may help prevent potential hack attempts by robots that produce a bunch of web calls during short time periods.
Be smart, use intelligent delays to reduce abuse and improve reliability! I’m interested to hear any feedback on how you have used these technique in your own apps.