I would like to hook into this question, however perhaps rephrase it from a different angle.
For anti fraud purposes I would like to block visitors after a certain amount of incorrect login attempts. At this point I have a manager class which wraps the actual function call to login. The manager throws an exception if a visitor is blocked and thus the actual login method is never called. So far so good.
I would now like to change the exception throwing part to a Thread.Sleep kind of thingy. This is not an option however because this will block other requests to the server. Is there an easy alternative for this scenario perhaps using some async wait command or perhaps using F#?
Current code:
class Manager {
void Execute(Action action) {
if(user.IsBlocked)
throw new BlockedException();
else
action();
}
}
Preferred code:
class Manager {
void Execute(Action action) {
if(user.IsBlocked)
//Wait for xxx minutes
action();
}
}
Doing it this way would eat resources really quickly. You don’t want multiple threads on your server (possibly dozens depending on the amount of visitors and the duration of your block) busy waiting until you can finaly send your response. Besides, it will not be clear to the visitor what is happening; it will seem as if your server is just not responding (which is true).
A better way is to just keep throwing the exception and maybe handle it more gracefully. For instance using javascript to display a countdown on the client until the time you may try again.