Пример #1
0
        /// <inheritdoc />
        public async Task ReleaseLockAsync(LockToken token)
        {
            var table = await GetTableAsync();

            var doc = new Document
            {
                [ColumnNames.HolderId]    = token.HolderId,
                [ColumnNames.ResourceId]  = token.ResourceId,
                [ColumnNames.LeaseExpiry] = token.LeaseExpiry.ToString("O")
            };

            await table.DeleteItemAsync(doc);
        }
Пример #2
0
        /// <inheritdoc />
        public async Task <LockToken?> RenewAsync(
            LockToken token,
            TimeSpan duration)
        {
            var table = await GetTableAsync();

            var doc = new Document
            {
                [ColumnNames.HolderId]   = token.HolderId,
                [ColumnNames.ResourceId] = token.ResourceId
            };

            var expiry = DateTimeOffset.UtcNow.Add(duration);

            doc[ColumnNames.LeaseExpiry] = expiry.ToString("O");

            var expr = new Expression
            {
                ExpressionStatement       = $"{ColumnNames.ResourceId} = :ID OR {ColumnNames.LeaseExpiry} < :NOW",
                ExpressionAttributeValues =
                {
                    [":NOW"] = DateTimeOffset.UtcNow.ToString("O"),
                    [":ID"]  = token.ResourceId
                }
            };

            try
            {
                await table.PutItemAsync(doc, new PutItemOperationConfig
                {
                    ConditionalExpression = expr
                });

                return(token.WithNewExpiry(expiry));
            }
            catch (ConditionalCheckFailedException)
            {
                return(null);
            }
        }