FlowThrottle.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 / FlowThrottle.cs / 1 / FlowThrottle.cs

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

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

    sealed class FlowThrottle 
    {
        int capacity; 
        int count; 
        object mutex;
        WaitCallback release; 
        Queue waiters;
        String propertyName;
        String configName;
 
        internal FlowThrottle(WaitCallback release, int capacity, String propertyName, String configName)
        { 
            if (capacity <= 0) 
                throw DiagnosticUtility.ExceptionUtility.ThrowHelperError(new InvalidOperationException(SR.GetString(SR.SFxThrottleLimitMustBeGreaterThanZero0)));
 
            this.count = 0;
            this.capacity = capacity;
            this.mutex = new object();
            this.release = release; 
            this.waiters = new Queue();
            this.propertyName = propertyName; 
            this.configName = configName; 
        }
 
        internal int Capacity
        {
            get { return this.capacity; }
            set 
            {
                if (value <= 0) 
                    throw DiagnosticUtility.ExceptionUtility.ThrowHelperError(new InvalidOperationException(SR.GetString(SR.SFxThrottleLimitMustBeGreaterThanZero0))); 
                this.capacity = value;
            } 
        }

        internal bool Acquire(object o)
        { 
            lock (this.mutex)
            { 
                if (this.count < this.capacity) 
                {
                    this.count++; 
                    return true;
                }
                else
                { 
                    if (this.waiters.Count == 0)
                    { 
                        if (DiagnosticUtility.ShouldTraceInformation) 
                        {
                            DiagnosticUtility.DiagnosticTrace.TraceEvent( 
                                TraceEventType.Information,
                                TraceCode.ServiceThrottleLimitReached,
                                SR.GetString(SR.TraceCodeServiceThrottleLimitReached,
                                             this.propertyName, this.capacity, this.configName)); 
                        }
                    } 
 
                    this.waiters.Enqueue(o);
                    return false; 
                }
            }
        }
 
        internal void Release()
        { 
            object next = null; 

            lock (this.mutex) 
            {
                if (this.waiters.Count > 0)
                {
                    next = this.waiters.Dequeue(); 
                    if (this.waiters.Count == 0)
                        this.waiters.TrimExcess(); 
                } 
                else
                { 
                    this.count--;
                }
            }
 
            if (next != null)
                this.release(next); 
        } 
    }
} 

// File provided for Reference Use Only by Microsoft Corporation (c) 2007.
// Copyright (c) Microsoft Corporation. All rights reserved.


                        

                        

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