Tuesday, January 11, 2011

Retrying Code Execution

I have a bit of code I'm working on at work that makes use of an HTTP endpoint to notify a legacy system of certain events. Because the network is not reliable, we have to handle the case when the network may be down. In this situation, it means to try again a few times, after which the processing request goes into an error queue, where it can be handled manually. So, I started out with something like this:

This calls the HTTP endpoint up to five times. If one of those times succeeds, it returns the result and stops trying. If it gets to the sixth time, it gives up.

Now, in my project, I have several of these types of calls to make. Much of the above code, namely the exception handling and flow control stuff, is common to all of them. So, using some functional .NET and an extension method, we can do this:

This will attempt to execute and return the value of func up to times times, and give up after that. With this extension method, we can now put our endpoint-calling code in a Func<int> and tell it to try to call the endpoint up to five times, like this: Now, is this the best way to do this? Maybe; maybe not. It's just something I've been toying around with in my attempts to eliminate duplication of the aforementioned ugly flow control code. I'll have to walk around in these shoes for a while and see whether I like them.

6 comments:

Anjudevan said...

Really informative Blog...Thanks for sharing...Waiting for next update...
Wordpress Training in Chennai
Wordpress Training
wordpress course fees
wordpress training in vadapalani
wordpress training in Guindy
Struts Training in Chennai
clinical sas training in chennai
Spring Training in Chennai
Photoshop Classes in Chennai
LoadRunner Training in Chennai

Riya Raj said...
This comment has been removed by the author.
sanjandevan said...

awesome article,the content has very informative ideas, waiting for the next update...
javascript training in chennai
javascript training center in chennai
javascript classes in chennai
javascript training in anna nagar
javascript training in vadapalani
javascript training in chennai
core java training in chennai
Photoshop Classes in Chennai
Manual Testing Training in Chennai
Drupal Training in Chennai

Riya Raj said...

The blog which you have shared is more creative... Waiting for your upcoming data...
Python Training in Chennai
Python course in Chennai
Python Training
Best Python Training Institute in Chennai
Python Training in Velachery
Python training in Adyar
Hadoop Training in Chennai
Software testing training in chennai
JAVA Training in Chennai

Sivanandhana Girish said...

Really wonderful blog! Thanks for taking your valuable time to share this with us. Keep us updated with more such blogs.
AWS Training in Chennai
AWS Training
AWS Training institute in Chennai
DevOps Training in Chennai
Azure Training in Chennai
VMware Training in Chennai
AWS Training in Velachery
AWS Training in Tambaram
AWS Training in Tnagar
AWS Training in Anna nagar

vinudevan said...

Thanks for giving excellent Message. Waiting for the next article
DOT NET Training in Chennai
DOT NET Course Chennai
dot net institute in chennai
best dotnet training in chennai
dot net training in Porur
Html5 Training in Chennai
Spring Training in Chennai
Struts Training in Chennai
Wordpress Training in Chennai
SAS Training in Chennai

Post a Comment