Exemplo n.º 1
0
        public long Decrement(string bucketName, object stateObject = null)
        {
            ArgumentValidator.ThrowIfNullOrWhiteSpace("bucketName", bucketName);
            ConcurrencyGuard.RefCount refCount = null;
            if (!this.TryGetRefCount(bucketName, out refCount))
            {
                return(0L);
            }
            if (refCount.Count > 0L)
            {
                long num = refCount.Decrement();
                ExTraceGlobals.ConcurrencyGuardTracer.TraceDebug <string, long, int>((long)this.GetHashCode(), "[ConcurrencyGuard::Decrement]: Guard {0} is at {1}/{2}.", ConcurrencyGuard.FormatGuardBucketName(this, bucketName), num, this.MaxConcurrency);
                if (this.onDecrementDelegate != null)
                {
                    this.onDecrementDelegate(this, bucketName, stateObject);
                }
                return(num);
            }
            bool flag = false;

            refCount.Reset();
            if (flag)
            {
                throw new ApplicationException("Cannot decrement guard " + ConcurrencyGuard.FormatGuardBucketName(this, bucketName) + " below 0. This usually indicates a bug in your code.");
            }
            return(0L);
        }
Exemplo n.º 2
0
        public long Increment(string bucketName, object stateObject = null)
        {
            ArgumentValidator.ThrowIfNullOrWhiteSpace("bucketName", bucketName);
            ConcurrencyGuard.RefCount refCount;
            if (!this.TryGetRefCount(bucketName, out refCount))
            {
                try
                {
                    this.instanceLock.EnterWriteLock();
                    if (!this.buckets.TryGetValue(bucketName, out refCount) && refCount == null)
                    {
                        refCount = new ConcurrencyGuard.RefCount();
                        this.buckets.Add(bucketName, refCount);
                    }
                }
                finally
                {
                    try
                    {
                        this.instanceLock.ExitWriteLock();
                    }
                    catch (SynchronizationLockException)
                    {
                    }
                }
            }
            long count = refCount.Count;

            ExTraceGlobals.ConcurrencyGuardTracer.TraceDebug <string, long, int>((long)this.GetHashCode(), "[ConcurrencyGuard::Increment]: Guard {0} is at {1}/{2}.", ConcurrencyGuard.FormatGuardBucketName(this, bucketName), count, this.MaxConcurrency);
            if (this.onNearThresholdDelegate != null && count > (long)this.concurrencyWarningThreshold)
            {
                this.onNearThresholdDelegate(this, bucketName, stateObject);
            }
            if (count + 1L > (long)this.MaxConcurrency)
            {
                ExTraceGlobals.ConcurrencyGuardTracer.TraceError <string, long, int>((long)this.GetHashCode(), "[ConcurrencyGuard::Increment]: Guard {0} is at concurrency limit {1}/{2} - REJECTING.", ConcurrencyGuard.FormatGuardBucketName(this, bucketName), count, this.MaxConcurrency);
                MaxConcurrencyReachedException ex = new MaxConcurrencyReachedException(this, bucketName);
                if (this.onRejectDelegate != null)
                {
                    this.onRejectDelegate(this, bucketName, stateObject, ex);
                }
                if (!this.TrainingMode)
                {
                    throw ex;
                }
            }
            refCount.Increment();
            if (this.onIncrementDelegate != null)
            {
                this.onIncrementDelegate(this, bucketName, stateObject);
            }
            return(count);
        }