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.


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

subha said...

Are you trying to move in or out of Jind? or near rohtak Find the most famous, reputed and the very best of all Packers and Movers by simply calling or talking to Airavat Movers and Packers
good jobs guys
Ai & Artificial Intelligence Course in Chennai
PHP Training in Chennai
Ethical Hacking Course in Chennai Blue Prism Training in Chennai
UiPath Training in Chennai

glaobal said...

I really appreciate this wonderful post that you have provided for us. I assure this would be beneficial for most of the people. marsbahis

Anonymous said...

perde modelleri
turkcell mobil ödeme bozdurma
nft nasıl alınır
Ankara Evden Eve Nakliyat
Ask Romanlari

Hi Every One said...

Razer Crackers is on Facebook. Join Facebook to connect with Razer Crackers and others you may know. Facebook gives people the power to share. Razer Surround Pro Download

haseeb said...

The program has powerful controller aptitudes which uphold blend, prepared capacity individuals, simple to-locate, the individual and irrelevant expense of proprietorship accompanied by snappy use. Tally 9 With Crack Full Version Zip

Let2know said...

Have I ever mentioned, how wonderful and comfortable your clothes are? No? Ups, sorry, they really are. Thanks! Happy birthday, sis, and hope your closet will always be full. Happy Birthday Sister Funny

iteducationcentre said...

Nice blog. Thanks for sharing.
Full-stack classes in Pune

Post a Comment