예제 #1
0
 public async Task ReleaseLockAsync(IDistributedLockClaim claim)
 {
     await _retryPolicy.ExecuteAsync(async() =>
     {
         var response = await _documentClient.Value.DeleteDocumentAsync(
             UriFactory.CreateDocumentUri(
                 _options.Value.Database,
                 _options.Value.DistributedLocksCollection,
                 claim.Id),
             new RequestOptions
         {
             PartitionKey = new PartitionKey(claim.Id)
         });
         return(response.StatusCode == HttpStatusCode.OK);
     }).ConfigureAwait(false);
 }
예제 #2
0
        public async Task <bool> TryClaimLockAsync(IDistributedLockClaim claim)
        {
            return(await _retryPolicy.ExecuteAsync(async() =>
            {
                try
                {
                    var response = await _documentClient.Value.CreateDocumentAsync(
                        UriFactory.CreateDocumentCollectionUri(_options.Value.Database, _options.Value.DistributedLocksCollection),
                        claim,
                        new RequestOptions
                    {
                        ConsistencyLevel = _options.Value.ConsistencyLevel,
                        PartitionKey = new PartitionKey(claim.Id)
                    }).ConfigureAwait(false);

                    return response.StatusCode == HttpStatusCode.Created;
                }
                catch (DocumentClientException ex) when(ex.StatusCode == HttpStatusCode.Conflict)
                {
                    // document was created before, lock acquisition failed
                    return false;
                }
            }).ConfigureAwait(false));
        }