In today’s software development world almost all applications are connected applications. In some or many ways they’re connected to network based services. Internet is a one of such network and which sometimes is not reliable due to slow connections, timeouts and other connection un-availability issues. This is most important for any application to handle such situations in a graceful manner. Means these things happens and are mend to happen so how application should response to these scenarios without reducing “Reliability” factor.
An example scenario
Let’s take an example of an application which displays some information from an RSS/Atom feed. Now once in a while RSS/Atom host server become un-reachable for a short span of time (Say 10-20 sec.) due to above mentioned reasons. We have couple of options to deal with such situations, Try reading from feed and when there’s an error show the user that feed is not available and try again. User tries again and application successfully reads the feed and displays the information to user. Or Application can retry the fetch operation couple of times and when it’s available displays the information to the User without any user interaction. This depends on business scenario but it increases the Reliability.
A high level view of Retry mechanism
When to apply the Retry mechanism?
In most scenarios at least important read operation can be marked to engage the server with Retry operation for specific errors like network timeout, connection un-reachable etc. Another example that I assume everybody have seen while downloading something from Internet, and connection goes down, browser waits and retry sometime before actually aborting the download.
Implementation
To introduce this behavior I preferred TrasientFaultHandling from Microsoft Practice Enterprise library. It’s available as individual nuget package so you won’t need to install the whole Enterprise Library monster project. Although you can use any other utility which provides Retry mechanism or implement your own. IMO if something is already there, well tested and optimized then why wasting time re-inventing the wheel.
Installing Nuget package
Add the nuget package TransientFaultHandling.Core in your project to have the required library classes and method available for usage.
Implementing Transient Fault detection policy/strategy A straight forward usage of Retry utility -
Transient fault/exception are identified as temporary errors and probably can be fixed by retrying the operation. To introduce such types of fault/exception we need to implement the policy/strategy by implementing the interface ITransientErrorDetectionStrategy.
The RetryPolicy is a class from TransientFaultHandling namespace.
this.retryPolicy = new RetryPolicy<DownloadFeedTrasientErrorDetectionStrategy>(new Incremental(3, TimeSpan.FromSeconds(0), TimeSpan.FromSeconds(1.5)) { FastFirstRetry = true });
The above snippet is interesting one. Here we’ve used the Generic constructor of RetryPolicy. The class Incremental is RetryStrategy which takes similar arguments as defined in RetryPolicy class constructor.
this.retryPolicy.ExecuteAction(() => this.DownloadFeed(feed_source));
The ablove line shows how the actual network based method call is wrapper with Retry logic. It takes simple void Action delegate which is represented as LambdaExpression.
This is a simplest usage of RetryPolicy. Really easy isn’t it? But this all you need to do to introduce Retry stuff in your application. Who else is using – Entity Framework, Windows Azure service and many other important products is using this library.
No comments:
Post a Comment