Azure: Handle blob-lease-acquire errors from response instead of exception #227
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
The Azure implementation of DistributedLock uses the Azure Storage SDK, which, by default, throws exceptions in case an operation failed (e.g. acquire blob lease). This works fine for scenarios where this is an actual exception (e.g. should not happen that blob x does not exist or that a lease has already been given to another client).
However, in an application such as ours where we often want to acquire a distributed lock for a specific item/entity, this results in a throw and a catch to get to the conclusion the blob does not exist yet - which is not really an exceptional case but, actually in most scenarios, the expected case.
This change allows for a more graceful detection of these specific scenarios by not relying on the throw/catch mechanism, but instead get the error from the response (by using the existing parsing mechanism from the Azure Storage SDK).
Would like to hear what you, @madelson, think of this change and if it's something you're willing to accept to enhance the great library you provide. :)
(p.s. I ran all Azure tests and they are still passing after this change.)