Exemple #1
0
        public bool LockExists(IThrottleKey key, Limiter limiter)
        {
            string lockId = CreateLockKey(key, limiter);
            object reslut = null;

            return(_store.TryGetValue(lockId, out reslut));
        }
Exemple #2
0
        public CheckResult Check(IThrottleKey key, bool increment = true)
        {
            foreach (Limiter limiter in Limiters)
            {
                var result = new CheckResult
                {
                    IsThrottled = false,
                    IsLocked    = false,
                    ThrottleKey = _repository.CreateThrottleKey(key, limiter),
                    Limiter     = limiter
                };
                var ignorelist = _repository.SetOrGetIgnoreList();
                //判断是否在白名单中
                if (ignorelist.Length > 0)
                {
                    if (key.Values.Intersect(ignorelist).Count() > 0)
                    {
                        return(result);
                    }
                }
                if (limiter.LockDuration.HasValue)
                {
                    result.LockKey = _repository.CreateLockKey(key, limiter);
                    if (_repository.LockExists(key, limiter))
                    {
                        result.IsLocked = true;
                        return(result);
                    }
                }
                //判断有没有达到限制数量
                if (limiter.Count <= 0)
                {
                    continue;
                }

                long?counter = _repository.GetThrottleCount(key, limiter);
                //计数
                if (counter.HasValue &&
                    counter.Value >= limiter.Count)
                {
                    if (limiter.LockDuration.HasValue)
                    {
                        //设置锁定
                        _repository.SetLock(key, limiter);
                        _repository.RemoveThrottle(key, limiter);
                    }

                    result.IsThrottled = true;
                    return(result);
                }

                if (increment)
                {
                    _repository.AddOrIncrementWithExpiration(key, limiter);
                }
            }

            return(CheckResult.NotThrottled);
        }
 public void SetLock(IThrottleKey key, Limiter limiter)
 {
     string id = CreateLockKey(key, limiter);
     ITransaction trans = _db.CreateTransaction();
     trans.StringIncrementAsync(id);
     trans.KeyExpireAsync(id, limiter.LockDuration);
     trans.Execute();
 }
        /// <summary>
        /// 设置锁定
        /// </summary>
        /// <param name="key"></param>
        /// <param name="limiter"></param>
        public void SetLock(IThrottleKey key, Limiter limiter)
        {
            string id = CreateLockKey(key, limiter);

            CSRedis.StartPipe()
            .IncrBy(id, 1)
            .Expire(id, limiter.LockDuration.Value)
            .EndPipe();
        }
        public void SetLock(IThrottleKey key, Limiter limiter)
        {
            string throttleId = CreateThrottleKey(key, limiter);
            _store.Remove(throttleId);

            string lockId = CreateLockKey(key, limiter);
            DateTime expiration = CurrentDate().Add(limiter.LockDuration.Value);
            _store.Set(lockId, true, expiration);
        }
        public void SetLock(IThrottleKey key, Limiter limiter)
        {
            string       id    = CreateLockKey(key, limiter);
            ITransaction trans = _db.CreateTransaction();

            trans.StringIncrementAsync(id);
            trans.KeyExpireAsync(id, limiter.LockDuration);
            trans.Execute();
        }
        public long? GetThrottleCount(IThrottleKey key, Limiter limiter)
        {
            string id = CreateThrottleKey(key, limiter);
            RedisValue value = _db.StringGet(id);
            long convert;
            if (long.TryParse(value, out convert))
                return convert;

            return null;
        }
        public void AddOrIncrementWithExpiration(IThrottleKey key, Limiter limiter)
        {
            string id = CreateThrottleKey(key, limiter);

            long result = _db.StringIncrement(id);

            // If we get back 1, that means the key was incremented as it
            // was expiring or it's a new key. Ensure we set the expiration.
            if (result == 1)
                _db.KeyExpire(id, limiter.Period);
        }
        public string CreateLockKey(IThrottleKey key, Limiter limiter)
        {
            List<object> values = CreateBaseKeyValues(key, limiter);

            string lockKeySuffix = TimeSpanToFriendlyString(limiter.LockDuration.Value);
            values.Add("lock");
            values.Add(lockKeySuffix);

            string id = string.Join(":", values);
            return id;
        }
Exemple #10
0
        public void SetLock(IThrottleKey key, Limiter limiter)
        {
            string throttleId = CreateThrottleKey(key, limiter);

            _store.Remove(throttleId);

            string   lockId     = CreateLockKey(key, limiter);
            DateTime expiration = CurrentDate().Add(limiter.LockDuration.Value);

            _store.Set(lockId, true, expiration);
        }
        private List <object> CreateBaseKeyValues(IThrottleKey key, Limiter limiter)
        {
            List <object> values = key.Values.ToList();

            if (PolicyIdentityValues != null && PolicyIdentityValues.Length > 0)
            {
                values.InsertRange(0, PolicyIdentityValues);
            }

            return(values);
        }
        public CheckResult Check(IThrottleKey key, bool increment = true)
        {
            foreach (Limiter limiter in Limiters)
            {
                var result = new CheckResult
                {
                    IsThrottled = false,
                    IsLocked    = false,
                    ThrottleKey = _repository.CreateThrottleKey(key, limiter),
                    Limiter     = limiter
                };

                if (limiter.LockDuration.HasValue)
                {
                    result.LockKey = _repository.CreateLockKey(key, limiter);
                    if (_repository.LockExists(key, limiter))
                    {
                        result.IsLocked = true;
                        return(result);
                    }
                }

                // Short-circuit this loop if the
                // limit value isn't valid
                if (limiter.Count <= 0)
                {
                    continue;
                }

                long?counter = _repository.GetThrottleCount(key, limiter);

                if (counter.HasValue &&
                    counter.Value >= limiter.Count)
                {
                    if (limiter.LockDuration.HasValue)
                    {
                        _repository.SetLock(key, limiter);
                        _repository.RemoveThrottle(key, limiter);
                    }

                    result.IsThrottled = true;
                    return(result);
                }

                if (increment)
                {
                    _repository.AddOrIncrementWithExpiration(key, limiter);
                }
            }

            return(CheckResult.NotThrottled);
        }
        public async Task AddOrIncrementWithExpirationAsync(IThrottleKey key, Limiter limiter)
        {
            string id = CreateThrottleKey(key, limiter);

            long result = await _db.StringIncrementAsync(id);

            // If we get back 1, that means the key was incremented as it
            // was expiring or it's a new key. Ensure we set the expiration.
            if (result == 1)
            {
                await _db.KeyExpireAsync(id, limiter.Period);
            }
        }
Exemple #14
0
        public long?GetThrottleCount(IThrottleKey key, Limiter limiter)
        {
            string id = CreateThrottleKey(key, limiter);

            var cacheItem = _store.Get(id) as ThrottleCacheItem;

            if (cacheItem != null)
            {
                return(cacheItem.Count);
            }

            return(null);
        }
        public string CreateLockKey(IThrottleKey key, Limiter limiter)
        {
            List <object> values = CreateBaseKeyValues(key, limiter);

            string lockKeySuffix = TimeSpanToFriendlyString(limiter.LockDuration.Value);

            values.Add("lock");
            values.Add(lockKeySuffix);

            string id = string.Join(":", values);

            return(id);
        }
        public long?GetThrottleCount(IThrottleKey key, Limiter limiter)
        {
            string     id    = CreateThrottleKey(key, limiter);
            RedisValue value = _db.StringGet(id);
            long       convert;

            if (long.TryParse(value, out convert))
            {
                return(convert);
            }

            return(null);
        }
        public void AddOrIncrementWithExpiration(IThrottleKey key, Limiter limiter)
        {
            string id = CreateThrottleKey(key, limiter);

            //设置增量
            long result = CSRedis.IncrBy(id);

            //设置过期时间
            if (result == 1)
            {
                CSRedis.Expire(id, limiter.Period);
            }
        }
        public async Task <long?> GetThrottleCountAsync(IThrottleKey key, Limiter limiter)
        {
            string     id    = CreateThrottleKey(key, limiter);
            RedisValue value = await _db.StringGetAsync(id);

            long convert;

            if (long.TryParse(value, out convert))
            {
                return(convert);
            }

            return(null);
        }
        public string CreateThrottleKey(IThrottleKey key, Limiter limiter)
        {
            List<object> values = CreateBaseKeyValues(key, limiter);

            string countKey = TimeSpanToFriendlyString(limiter.Period);
            values.Add(countKey);

            // Using the Unix timestamp to the key allows for better
            // precision when querying a key from Redis
            if (limiter.Period.TotalSeconds == 1)
                values.Add(GetUnixTimestamp());

            string id = string.Join(":", values);
            return id;
        }
        public CheckResult Check(IThrottleKey key, bool increment = true)
        {
            foreach (Limiter limiter in Limiters)
            {
                var result = new CheckResult
                {
                    IsThrottled = false,
                    IsLocked = false,
                    ThrottleKey = _repository.CreateThrottleKey(key, limiter),
                    Limiter = limiter
                };

                if (limiter.LockDuration.HasValue)
                {
                    result.LockKey = _repository.CreateLockKey(key, limiter);
                    if (_repository.LockExists(key, limiter))
                    {
                        result.IsLocked = true;
                        return result;
                    }
                }

                // Short-circuit this loop if the
                // limit value isn't valid
                if (limiter.Count <= 0)
                    continue;

                long? counter = _repository.GetThrottleCount(key, limiter);

                if (counter.HasValue
                    && counter.Value >= limiter.Count)
                {
                    if (limiter.LockDuration.HasValue)
                    {
                        _repository.SetLock(key, limiter);
                        _repository.RemoveThrottle(key, limiter);
                    }

                    result.IsThrottled = true;
                    return result;
                }

                if (increment)
                    _repository.AddOrIncrementWithExpiration(key, limiter);
            }

            return CheckResult.NotThrottled;
        }
        /// <summary>
        /// 生成key
        /// </summary>
        /// <param name="key"></param>
        /// <param name="limiter"></param>
        /// <returns></returns>
        public string CreateThrottleKey(IThrottleKey key, Limiter limiter)
        {
            List <object> values = CreateBaseKeyValues(key, limiter);

            string countKey = TimeSpanToFriendlyString(limiter.Period);

            values.Add(countKey);

            //使用unix时间戳提高精度
            if (limiter.Period.TotalSeconds == 1)
            {
                values.Add(GetUnixTimestamp());
            }

            string id = string.Join(":", values);

            return(id);
        }
        public string CreateThrottleKey(IThrottleKey key, Limiter limiter)
        {
            List <object> values = CreateBaseKeyValues(key, limiter);

            string countKey = TimeSpanToFriendlyString(limiter.Period);

            values.Add(countKey);

            // Using the Unix timestamp to the key allows for better
            // precision when querying a key from Redis
            if (limiter.Period.TotalSeconds == 1)
            {
                values.Add(GetUnixTimestamp());
            }

            string id = string.Join(":", values);

            return(id);
        }
Exemple #23
0
        public void AddOrIncrementWithExpiration(IThrottleKey key, Limiter limiter)
        {
            string id        = CreateThrottleKey(key, limiter);
            var    cacheItem = _store.Get(id) as ThrottleCacheItem;

            if (cacheItem != null)
            {
                cacheItem.Count = cacheItem.Count + 1;
            }
            else
            {
                cacheItem = new ThrottleCacheItem()
                {
                    Count      = 1,
                    Expiration = CurrentDate().Add(limiter.Period)
                };
            }

            _store.Set(id, cacheItem, cacheItem.Expiration);
        }
        public void AddOrIncrementWithExpiration(IThrottleKey key, Limiter limiter)
        {
            string id = CreateThrottleKey(key, limiter);
            var cacheItem = _store.Get(id) as ThrottleCacheItem;

            if (cacheItem != null)
            {
                cacheItem.Count = cacheItem.Count + 1;
            }
            else
            {
                cacheItem = new ThrottleCacheItem()
                {
                    Count = 1,
                    Expiration = CurrentDate().Add(limiter.Period)
                };
            }

            _store.Set(id, cacheItem, cacheItem.Expiration);
        }
        public long? GetThrottleCount(IThrottleKey key, Limiter limiter)
        {
            string id = CreateThrottleKey(key, limiter);

            var cacheItem = _store.Get(id) as ThrottleCacheItem;
            if (cacheItem != null)
            {
                return cacheItem.Count;
            }

            return null;
        }
        public Task RemoveThrottleAsync(IThrottleKey key, Limiter limiter)
        {
            string id = CreateThrottleKey(key, limiter);

            return(_db.KeyDeleteAsync(id));
        }
        public void RemoveThrottle(IThrottleKey key, Limiter limiter)
        {
            string id = CreateThrottleKey(key, limiter);

            _db.KeyDelete(id);
        }
Exemple #28
0
 public Task <bool> LockExistsAsync(IThrottleKey key, Limiter limiter)
 => Task.FromResult(LockExists(key, limiter));
Exemple #29
0
 public Task SetLockAsync(IThrottleKey key, Limiter limiter)
 {
     SetLock(key, limiter);
     return(Task.CompletedTask);
 }
        /// <summary>
        /// 移除限制状态
        /// </summary>
        /// <param name="key"></param>
        /// <param name="limiter"></param>
        public void RemoveThrottle(IThrottleKey key, Limiter limiter)
        {
            string id = CreateThrottleKey(key, limiter);

            CSRedis.Del(id);
        }
Exemple #31
0
 public Task AddOrIncrementWithExpirationAsync(IThrottleKey key, Limiter limiter)
 {
     AddOrIncrementWithExpiration(key, limiter);
     return(Task.CompletedTask);
 }
 public bool IsLocked(IThrottleKey key, out CheckResult result, bool increment = true)
 {
     result = Check(key, increment);
     return(result.IsLocked);
 }
 public void RemoveThrottle(IThrottleKey key, Limiter limiter)
 {
     string lockId = CreateThrottleKey(key, limiter);
     _store.Remove(lockId);
 }
Exemple #34
0
 public Task <string> CreateThrottleKeyAsync(IThrottleKey key, Limiter limiter)
 => Task.FromResult(CreateThrottleKey(key, limiter));
 public bool LockExists(IThrottleKey key, Limiter limiter)
 {
     string lockId = CreateLockKey(key, limiter);
     return _store.Contains(lockId);
 }
        public Task <bool> LockExistsAsync(IThrottleKey key, Limiter limiter)
        {
            string id = CreateLockKey(key, limiter);

            return(_db.KeyExistsAsync(id));
        }
 public void RemoveThrottle(IThrottleKey key, Limiter limiter)
 {
     string id = CreateThrottleKey(key, limiter);
     _db.KeyDelete(id);
 }
 public bool IsLocked(IThrottleKey key, out CheckResult result, bool increment = true)
 {
     result = Check(key, increment);
     return result.IsLocked;
 }
        private List<object> CreateBaseKeyValues(IThrottleKey key, Limiter limiter)
        {
            List<object> values = key.Values.ToList();
            if (PolicyIdentityValues != null && PolicyIdentityValues.Length > 0)
                values.InsertRange(0, PolicyIdentityValues);

            return values;
        }
Exemple #40
0
 public Task <long?> GetThrottleCountAsync(IThrottleKey key, Limiter limiter)
 => Task.FromResult(GetThrottleCount(key, limiter));
 public bool LockExists(IThrottleKey key, Limiter limiter)
 {
     string id = CreateLockKey(key, limiter);
     return _db.KeyExists(id);
 }
Exemple #42
0
        public async Task <bool> IsLockedAsync(IThrottleKey key, bool increment = true)
        {
            var result = await CheckAsync(key, increment);

            return(result.IsLocked);
        }
        public bool LockExists(IThrottleKey key, Limiter limiter)
        {
            string id = CreateLockKey(key, limiter);

            return(_db.KeyExists(id));
        }
Exemple #44
0
        public void RemoveThrottle(IThrottleKey key, Limiter limiter)
        {
            string lockId = CreateThrottleKey(key, limiter);

            _store.Remove(lockId);
        }
Exemple #45
0
 public Task RemoveThrottleAsync(IThrottleKey key, Limiter limiter)
 {
     RemoveThrottle(key, limiter);
     return(Task.CompletedTask);
 }
 public long? GetThrottleCount(IThrottleKey key, Limiter limiter)
 {
     string id = CreateThrottleKey(key, limiter);
     return _store.Get(id) as long?;
 }