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

            return(_store.TryGetValue(lockId, out reslut));
        }
コード例 #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);
        }
コード例 #3
0
 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();
 }
コード例 #4
0
        /// <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();
        }
コード例 #5
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);
        }
コード例 #6
0
        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();
        }
コード例 #7
0
        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;
        }
コード例 #8
0
        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);
        }
コード例 #9
0
        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;
        }
コード例 #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);
        }
コード例 #11
0
        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);
        }
コード例 #12
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
                };

                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);
        }
コード例 #13
0
        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);
            }
        }
コード例 #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);
        }
コード例 #15
0
        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);
        }
コード例 #16
0
        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);
        }
コード例 #17
0
        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);
            }
        }
コード例 #18
0
        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);
        }
コード例 #19
0
        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;
        }
コード例 #20
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
                };

                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;
        }
コード例 #21
0
        /// <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);
        }
コード例 #22
0
        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);
        }
コード例 #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);
        }
コード例 #24
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);
        }
コード例 #25
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;
        }
コード例 #26
0
        public Task RemoveThrottleAsync(IThrottleKey key, Limiter limiter)
        {
            string id = CreateThrottleKey(key, limiter);

            return(_db.KeyDeleteAsync(id));
        }
コード例 #27
0
        public void RemoveThrottle(IThrottleKey key, Limiter limiter)
        {
            string id = CreateThrottleKey(key, limiter);

            _db.KeyDelete(id);
        }
コード例 #28
0
 public Task <bool> LockExistsAsync(IThrottleKey key, Limiter limiter)
 => Task.FromResult(LockExists(key, limiter));
コード例 #29
0
 public Task SetLockAsync(IThrottleKey key, Limiter limiter)
 {
     SetLock(key, limiter);
     return(Task.CompletedTask);
 }
コード例 #30
0
        /// <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);
        }
コード例 #31
0
 public Task AddOrIncrementWithExpirationAsync(IThrottleKey key, Limiter limiter)
 {
     AddOrIncrementWithExpiration(key, limiter);
     return(Task.CompletedTask);
 }
コード例 #32
0
 public bool IsLocked(IThrottleKey key, out CheckResult result, bool increment = true)
 {
     result = Check(key, increment);
     return(result.IsLocked);
 }
コード例 #33
0
 public void RemoveThrottle(IThrottleKey key, Limiter limiter)
 {
     string lockId = CreateThrottleKey(key, limiter);
     _store.Remove(lockId);
 }
コード例 #34
0
 public Task <string> CreateThrottleKeyAsync(IThrottleKey key, Limiter limiter)
 => Task.FromResult(CreateThrottleKey(key, limiter));
コード例 #35
0
 public bool LockExists(IThrottleKey key, Limiter limiter)
 {
     string lockId = CreateLockKey(key, limiter);
     return _store.Contains(lockId);
 }
コード例 #36
0
        public Task <bool> LockExistsAsync(IThrottleKey key, Limiter limiter)
        {
            string id = CreateLockKey(key, limiter);

            return(_db.KeyExistsAsync(id));
        }
コード例 #37
0
 public void RemoveThrottle(IThrottleKey key, Limiter limiter)
 {
     string id = CreateThrottleKey(key, limiter);
     _db.KeyDelete(id);
 }
コード例 #38
0
 public bool IsLocked(IThrottleKey key, out CheckResult result, bool increment = true)
 {
     result = Check(key, increment);
     return result.IsLocked;
 }
コード例 #39
0
        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;
        }
コード例 #40
0
 public Task <long?> GetThrottleCountAsync(IThrottleKey key, Limiter limiter)
 => Task.FromResult(GetThrottleCount(key, limiter));
コード例 #41
0
 public bool LockExists(IThrottleKey key, Limiter limiter)
 {
     string id = CreateLockKey(key, limiter);
     return _db.KeyExists(id);
 }
コード例 #42
0
        public async Task <bool> IsLockedAsync(IThrottleKey key, bool increment = true)
        {
            var result = await CheckAsync(key, increment);

            return(result.IsLocked);
        }
コード例 #43
0
        public bool LockExists(IThrottleKey key, Limiter limiter)
        {
            string id = CreateLockKey(key, limiter);

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

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