次の方法で共有


Windows Azure Node.js SDK Filters

One of the features of the Windows Azure SDKs that I haven't seen a lot of talk about is retrying operations. Most of the SDKs provide the ability to retry an operation in the event of a transient, or temporary failure. The Node.js one provides this ability through 'filters', which can be used for more than just retrying operations.

Filters

The SDK for Node.js allows you to add .withFilter() to any create call that is based on ServiceClient (defined in serviceclient.js). You can pass this a filter, which comments say are:

Filter operations are objects that implement a method with the signature:

  function handle (requestOptions, next) 

After doing its preprocessing on the request options, the method needs to call "next" passing a callback with the following signature:

  function (returnObject, finalCallback, next) 

In this callback, and after processing the returnObject (the response from the request to the server), the callback needs to either invoke next if it exists to continue processing other filters or simply invoke finalCallback otherwise to end up the service invocation.

So you can do whatever you want in there, as long as you play nice with calling next as specified above.

Default retry filters

The SDK provides an exponential and linear retry policy, ExponentialRetryPolicyFilter and LinearRetryPolicyFilter. The differences between the two are:

  • A linear retry will attempt the operation for a specific number of tries, and the time between retry attempts will always be the same.

  • An exponential retry will also attempt the operation for a specific number of tries, but the time between retry attempts is determined by an algorithm that takes into account things like the number of previous retries. Usually the time between retries increases with the number of retry attempts.

Here's an example of creating a new instance of the BlobService and specifying the ExponentialRetryPolicyFilter:

 var azure = require('azure'); var retryOperations = new azure.ExponentialRetryPolicyFilter();
var blobService = azure.createBlobService().withFilter(retryOperations); 

Custom retry filter

There's currently an example of a customized ExponentialRetryPolicyFilter included in the repository for the SDK. You can find this retrypolicysample.js in the Windows Azure SDK for Node.js repository at https://github.com/WindowsAzure/azure-sdk-for-node/blob/master/examples/samples/retrypolicysample.js.

Summary

While filters are generically useful for performing some action when an SDK operation is fired, using one of the retry policies is probably the most common use. Failure is pretty much a feature of the Internet, as well as any cloud solution, and your applications need to be able to gracefully deal with these failures. Retry filters are a relatively easy way to implement this when using the Windows Azure SDK for Node.js.