QuotaThrottle.cs source code in C# .NET

Source code for the .NET framework in C#

                        

Code:

/ WCF / WCF / 3.5.30729.1 / untmp / Orcas / SP / ndp / cdf / src / WCF / ServiceModel / System / ServiceModel / Dispatcher / QuotaThrottle.cs / 1 / QuotaThrottle.cs

                            //------------------------------------------------------------------------------ 
// Copyright (c) Microsoft Corporation.  All rights reserved.
//-----------------------------------------------------------------------------

namespace System.ServiceModel.Dispatcher 
{
    using System; 
    using System.Diagnostics; 
    using System.ServiceModel.Diagnostics;
    using System.ServiceModel.Channels; 
    using System.Collections.Generic;
    using System.Threading;

    sealed class QuotaThrottle 
    {
        int limit; 
        object mutex; 
        WaitCallback release;
        Queue waiters; 
        bool didTraceThrottleLimit;
        string propertyName = "ManualFlowControlLimit";
        string owner;
 
        internal QuotaThrottle(WaitCallback release, object mutex)
        { 
            this.limit = Int32.MaxValue; 
            this.mutex = mutex;
            this.release = release; 
            this.waiters = new Queue();
        }

        bool IsEnabled 
        {
            get { return this.limit != Int32.MaxValue; } 
        } 

        internal String Owner 
        {
            set { this.owner = value; }
        }
 
        internal int Limit
        { 
            get { return this.limit; } 
        }
 
        internal bool Acquire(object o)
        {
            lock (this.mutex)
            { 
                if (this.IsEnabled)
                { 
                    if (this.limit > 0) 
                    {
                        this.limit--; 

                        if (this.limit == 0)
                        {
                            if (DiagnosticUtility.ShouldTraceInformation && !this.didTraceThrottleLimit) 
                            {
                                this.didTraceThrottleLimit = true; 
 
                                DiagnosticUtility.DiagnosticTrace.TraceEvent(
                                    TraceEventType.Information, 
                                    TraceCode.ManualFlowThrottleLimitReached,
                                    SR.GetString(SR.TraceCodeManualFlowThrottleLimitReached,
                                                 this.propertyName, this.owner));
                            } 
                        }
 
                        return true; 
                    }
                    else 
                    {
                        this.waiters.Enqueue(o);
                        return false;
                    } 
                }
                else 
                { 
                    return true;
                } 
            }
        }

        internal int IncrementLimit(int incrementBy) 
        {
            if (incrementBy < 0) 
                throw DiagnosticUtility.ExceptionUtility.ThrowHelperError(new ArgumentOutOfRangeException("incrementBy", incrementBy, 
                                                     SR.GetString(SR.ValueMustBeNonNegative)));
            int newLimit; 
            object[] released = null;

            lock (this.mutex)
            { 
                if (this.IsEnabled)
                { 
                    checked { this.limit += incrementBy; } 
                    released = this.LimitChanged();
                } 
                newLimit = this.limit;
            }

            if (released != null) 
                this.Release(released);
 
            return newLimit; 
        }
 
        object[] LimitChanged()
        {
            object[] released = null;
 
            if (this.IsEnabled)
            { 
                if ((this.waiters.Count > 0) && (this.limit > 0)) 
                {
                    if (this.limit < this.waiters.Count) 
                    {
                        released = new object[this.limit];
                        for (int i=0; i

                        

Link Menu

Network programming in C#, Network Programming in VB.NET, Network Programming in .NET
This book is available now!
Buy at Amazon US or
Buy at Amazon UK