public void UT_LockToken_ToString_LockTokenHeaderWithBothBracketsReversed()
        {
            var lockToken = new LockToken("<(my-lock-token)>");
            var str       = lockToken.ToString(LockTokenFormat.LockTokenHeader);

            Assert.AreEqual("<my-lock-token>", str);
        }
        public void UT_LockToken_ToString_IfHeaderWithBothBrackets()
        {
            var lockToken = new LockToken("(<my-lock-token>)");
            var str       = lockToken.ToString(LockTokenFormat.IfHeader);

            Assert.AreEqual("(<my-lock-token>)", str);
        }
        public void UT_LockToken_ToString_LockTokenHeaderWithSquaredBrackets()
        {
            var lockToken = new LockToken("<my-lock-token>");
            var str       = lockToken.ToString(LockTokenFormat.LockTokenHeader);

            Assert.AreEqual("<my-lock-token>", str);
        }
예제 #4
0
        public override string ToString()
        {
            StringBuilder builder = new StringBuilder();

            builder.Append("MessageId:" + MessageId);
            builder.Append(", WorkflowId:" + WorkflowId);
            builder.Append(", EntityId:" + EntityId);
            builder.Append(", UserId:" + UserId);
            builder.Append(", ContactId:" + ContactId);
            builder.Append(", AccountId:" + AccountId);
            builder.Append(", LeadScoreConditionType:" + LeadScoreConditionType);
            builder.Append(", LinkedEntityId:" + LinkedEntityId);
            builder.Append(", ConditionValue:" + ConditionValue);
            builder.Append(", Lock-Token:" + LockToken.ToString());

            return(builder.ToString());
        }
예제 #5
0
        /// <summary>
        /// Locks the specified resource.
        /// </summary>
        /// <param name="resourceUri">The resource URI.</param>
        /// <param name="waitTime">The maximum amount of time to wait.</param>
        /// <param name="cancellationToken">The cancellation token.</param>
        /// <returns>The lock handle or null if the lock could not be obtained.</returns>
        public async Task <ILockHandle> LockAsync(Uri resourceUri, TimeSpan waitTime = default(TimeSpan), CancellationToken cancellationToken = default(CancellationToken))
        {
            if (_disposed == 1)
            {
                throw new ObjectDisposedException("The lock manager has been disposed");
            }

            if (!resourceUri.Scheme.Equals("tandem", StringComparison.CurrentCultureIgnoreCase))
            {
                throw new FormatException("The protocol scheme must be tandem");
            }

            // generate random token
            LockToken token = new LockToken(Guid.NewGuid(), _owner);

            // get remaining time
            TimeSpan remainingTime = waitTime;

            while (remainingTime >= TimeSpan.Zero)
            {
                // check if cancelled
                cancellationToken.ThrowIfCancellationRequested();

                // try and get the lock
                bool gotLock = await _database.LockTakeAsync($"tandem.{resourceUri.ToString()}", token.ToString(), _expirySpan).ConfigureAwait(false);

                if (gotLock)
                {
                    // create handle
                    RedisLockHandle handle = new RedisLockHandle(this)
                    {
                        ResourceURI = resourceUri,
                        Token       = token
                    };

                    // add handle
                    lock (_handles) {
                        _handles.Add(handle);
                    }

                    return(handle);
                }
                else
                {
                    if (waitTime == TimeSpan.Zero)
                    {
                        return(null);
                    }

                    if (remainingTime > TimeSpan.FromMilliseconds(3000))
                    {
                        // no point waiting anymore, no cigar for the lock
                        return(null);
                    }
                    else
                    {
                        // wait 3 seconds until we try again
                        await Task.Delay(3000, cancellationToken).ConfigureAwait(false);

                        remainingTime -= TimeSpan.FromSeconds(3);
                    }
                }
            }

            return(null);
        }