コード例 #1
0
        /// <summary>
        /// Initializes a new instance of the <see cref="ThrottleProcesser"/> class.
        /// By default, the <see cref="QuotaExceededResponseCode"/> property
        /// is set to 429 (Too Many Requests).
        /// </summary>
        public ThrottleProcesser(
            ThrottlePolicy policy,
            IIpAddressParser ipAddressParser,
            IPolicyRepository policyRepo     = null,
            IThrottleRepository throttleRepo = null)
        {
            Logger = new DefaultThrottleLogger();
            QuotaExceededResponseCode = (HttpStatusCode)429;
            processResult             = new ThrottleProcessResult {
                IsPass = true
            };

            ThrottleRepo = throttleRepo;
            if (ThrottleRepo == null)
            {
                ThrottleRepo = new WebCacheThrottleRepository();
            }
            ThrottlingCore = new ThrottlingCore(ipAddressParser);
            ThrottlingCore.ThrottleRepo = ThrottleRepo;

            PolicyRepo = policyRepo;
            if (PolicyRepo == null)
            {
                PolicyRepo = new WebCachePolicyRepository();
            }
            Policy = policy;
            PolicyRepo.Save(ThrottleManager.GetPolicyKey(), policy);
        }
コード例 #2
0
 public ThrottleProcessResult Process(RequestIdentity identity, IEnableThrottlingAttribute attrPolicy = null)
 {
     Identity = identity;
     Policy   = PolicyRepo.FirstOrDefault(ThrottleManager.GetPolicyKey());
     if (Policy != null)
     {
         Checking(attrPolicy);
     }
     return(processResult);
 }
コード例 #3
0
ファイル: ThrottlingCore.cs プロジェクト: jackyzhou/ZSharp
        public string ComputeThrottleKey(RequestIdentity requestIdentity, RateLimitPeriod period)
        {
            var keyValues = new List <string>()
            {
                ThrottleManager.GetThrottleKey()
            };

            if (Policy.IpThrottling)
            {
                keyValues.Add(requestIdentity.ClientIp);
            }

            if (Policy.ClientThrottling)
            {
                keyValues.Add(requestIdentity.ClientKey);
            }

            if (Policy.EndpointThrottling)
            {
                keyValues.Add(requestIdentity.Endpoint);
            }

            keyValues.Add(period.ToString());

            var id      = string.Join("_", keyValues);
            var idBytes = Encoding.UTF8.GetBytes(id);

            byte[] hashBytes;

            using (var algorithm = System.Security.Cryptography.HashAlgorithm.Create("SHA1"))
            {
                hashBytes = algorithm.ComputeHash(idBytes);
            }

            var hex = BitConverter.ToString(hashBytes).Replace("-", string.Empty);

            return(hex);
        }