Ejemplo n.º 1
0
        public LeakyBucket(LimitRequestZone limitRequestZone, IDelay delay = null)
        {
            if (limitRequestZone == null)
            {
                throw new ArgumentNullException(nameof(limitRequestZone));
            }
            LimitRequestZone   = limitRequestZone;
            _delay             = delay ?? TaskDelay.Instance;
            _secondsPerRequest = LimitRequestZone.RequestRate.TimePerRequest;
            var burst = limitRequestZone.Burst.HasValue ? limitRequestZone.Burst.Value : 1;

            _Queue = Channel.CreateBounded <TaskToProcess>(burst);
            _Slots = burst;
        }
        public static bool TryParse(string str, out LimitRequestZone limitRequestZone)
        {
            if (str == null)
            {
                throw new ArgumentNullException(nameof(str));
            }
            limitRequestZone = null;
            str = str.Trim();
            var parts = str.Split(new[] { ' ' }, StringSplitOptions.RemoveEmptyEntries).Select(p => p.Trim().ToLowerInvariant()).ToList();


            string      name        = null;
            int?        burst       = null;
            bool        nodelay     = false;
            RequestRate requestRate = null;

            foreach (var p in parts)
            {
                if (p.StartsWith("zone=", StringComparison.OrdinalIgnoreCase))
                {
                    if (name != null)
                    {
                        return(false);
                    }
                    name = p.Substring("zone=".Length);
                    var split = name.IndexOf(":", StringComparison.OrdinalIgnoreCase);
                    if (split != -1)
                    {
                        return(false);
                    }
                }
                else if (p.StartsWith("burst=", StringComparison.OrdinalIgnoreCase))
                {
                    if (burst != null)
                    {
                        return(false);
                    }
                    if (!int.TryParse(p.Substring("burst=".Length), out int v))
                    {
                        return(false);
                    }
                    if (v <= 0)
                    {
                        return(false);
                    }
                    burst = v;
                }
                else if (p.Equals("nodelay", StringComparison.OrdinalIgnoreCase))
                {
                    if (nodelay)
                    {
                        return(false);
                    }
                    nodelay = true;
                }
                else if (p.StartsWith("rate=", StringComparison.OrdinalIgnoreCase))
                {
                    if (requestRate != null)
                    {
                        return(false);
                    }
                    var v = p.Substring("rate=".Length);
                    if (!RequestRate.TryParse(v, out requestRate))
                    {
                        return(false);
                    }
                }
                else
                {
                    return(false);
                }
            }
            if (requestRate == null || string.IsNullOrEmpty(name))
            {
                return(false);
            }
            limitRequestZone = new LimitRequestZone(name, burst, nodelay, requestRate);;
            return(true);
        }