public RateLimitSettingsBase this[RateLimitType rateLimitType]
 {
     get
     {
         return(this.RateLimiterSettings[rateLimitType]);
     }
 }
Ejemplo n.º 2
0
        /// <summary>
        /// Handles errors and rate limiting.
        /// </summary>
        /// <param name="url"></param>
        /// <param name="rateLimitType"></param>
        /// <returns>Returns response from server if OK else null.</returns>
        private async Task <string> Handle(string url, RateLimitType rateLimitType)
        {
            await Limit(rateLimitType);

            string response = await GetResponseString(url);

            JObject jObject = JObject.Parse(response);

            if (response.Contains("\"Response\":\"Error\","))
            {
                int    type = jObject["Type"].ToObject <int>();
                string msg  = jObject["Message"].ToObject <string>();
                // Rate limit exceeded
                if (type == 99)
                {
                    Debug.WriteLine($"Rate limit exceeded: {DateTime.Now}");
                    return(await Handle(url, rateLimitType));
                }
                else
                {
                    System.Windows.Forms.MessageBox.Show(msg);
                    return(null);
                }
            }
            return(response);
        }
Ejemplo n.º 3
0
 public Rule(string name, RateLimitType rateLimitType, RateLimitLevel rateLimitLevel)
 {
     this.Name           = name;
     this.RateLimitType  = rateLimitType;
     this.RateLimitLevel = rateLimitLevel;
     this.IsActive       = false;
 }
Ejemplo n.º 4
0
 /// <summary>
 ///     Sets Limiter provider
 /// </summary>
 /// <param name="builder">The host builder to configure.</param>
 /// <param name="type"></param>
 /// <returns>The host builder.</returns>
 public static IHostBuilder UseAbpLimiter(this IHostBuilder builder, RateLimitType type)
 {
     if (builder == null)
     {
         throw new ArgumentNullException(nameof(builder));
     }
     builder.ConfigureServices((_, services) =>
     {
         var provider    = services.BuildServiceProvider();
         using var scope = provider.CreateScope();
         if (type == All || type == Client)
         {
             // get the ClientPolicyStore instance
             var clientPolicyStore = scope.ServiceProvider.GetRequiredService <IClientPolicyStore>();
             //seed Client data from appsettings
             clientPolicyStore.SeedAsync().WaitAndUnwrapException();
         }
         if (type == All || type == IP)
         {
             // get the IpPolicyStore instance
             var ipPolicyStore = scope.ServiceProvider.GetRequiredService <IIpPolicyStore>();
             // seed IP data from appsettingsi
             ipPolicyStore.SeedAsync().WaitAndUnwrapException();
         }
     });
     return(builder);
 }
Ejemplo n.º 5
0
 public Limiter(RateLimitType type, int limit, TimeSpan perPeriod, HttpMethod?method)
 {
     Semaphore = new SemaphoreSlim(1, 1);
     Type      = type;
     Limit     = limit;
     Period    = perPeriod;
     Method    = method;
 }
Ejemplo n.º 6
0
 public RateLimitSettingsBase this[RateLimitType rateLimitType]
 {
     get
     {
         return(RateLimitSettings[rateLimitType]);
     }
     set
     {
         RateLimitSettings[rateLimitType] = value;
     }
 }
Ejemplo n.º 7
0
        public static string TypeName(this RateLimitType type)
        {
            switch (type)
            {
            case RateLimitType.Application: return("application");

            case RateLimitType.Method: return("method");

            default: throw new ArgumentOutOfRangeException(nameof(type), type, null);
            }
        }
Ejemplo n.º 8
0
        public static string CountHeader(this RateLimitType type)
        {
            switch (type)
            {
            case RateLimitType.Application: return("X-App-Rate-Limit-Count");

            case RateLimitType.Method: return("X-Method-Rate-Limit-Count");

            default: throw new ArgumentOutOfRangeException(nameof(type), type, null);
            }
        }
Ejemplo n.º 9
0
        /// <summary>
        /// get data from Retry-After
        /// </summary>
        /// <param name="prop">
        /// </param>
        /// <param name="name">
        /// </param>
        /// <param name="limitType">
        /// </param>
        /// <param name="retryAfterSeconds">
        /// value of Retry-After
        /// </param>
        /// <param name="region">
        /// </param>
        /// <param name="type">
        /// </param>
        /// <exception cref="RiotGamesApiException">
        /// Condition.
        /// </exception>
        public void SetRetryTime(string region, LolUrlType type, LolApiName name, RateLimitType limitType, LolApiMethodName method, int retryAfterSeconds)
        {
            var regionLimit = Rates.Find(region, type, name, method);

            if (regionLimit == null)
            {
                throw new RiotGamesApiException($"Undefined {limitType} limit type for region:{region},type:{type},name:{name}");
            }
            regionLimit.RetryAfter        = DateTime.Now.AddSeconds(retryAfterSeconds);
            regionLimit.UsedRateLimitType = limitType;
        }
Ejemplo n.º 10
0
        public RateLimit(int count, TimeSpan interval, RateLimitType type = RateLimitType.Global)
        {
            if (count < 0)
            {
                throw new ArgumentOutOfRangeException(nameof(count), $"'{nameof(count)}' must be >= 0");
            }
            if (interval == TimeSpan.Zero)
            {
                throw new ArgumentOutOfRangeException(nameof(interval), $"'{nameof(interval)}' must be a non-zero {nameof(TimeSpan)}");
            }

            Count    = count;
            Interval = interval;
            Type     = type;
        }
Ejemplo n.º 11
0
        public NetCoreRateLimit(RateLimitType rateLimitType)
        {
            _config = ConfigHelper.GetConfig();

            _cache = new MemoryCache(new MemoryCacheOptions());

            memoryCacheRateLimitCounterStore = new MemoryCacheRateLimitCounterStore(_cache);

            memoryCacheIpPolicyStore = new MemoryCacheIpPolicyStore(_cache, _config.IpRateLimitOptions, _config.IpRateLimitPolicies);
            //添加Policy进内存
            memoryCacheIpPolicyStore.SeedAsync().Wait();

            memoryCacheClientPolicyStore = new MemoryCacheClientPolicyStore(_cache, _config.ClientRateLimitOptions, _config.ClientRateLimitPolicies);
            //添加Policy进内存
            memoryCacheClientPolicyStore.SeedAsync().Wait();

            rateLimitConfiguration = new RateLimitConfiguration(_config.IpRateLimitOptions, _config.ClientRateLimitOptions);



            switch (rateLimitType)
            {
            case RateLimitType.CLIENT_ID:
                _processor = new ClientRateLimitProcessor(_config.ClientRateLimitOptions
                                                          , memoryCacheRateLimitCounterStore, memoryCacheClientPolicyStore, rateLimitConfiguration);
                _options = _config.ClientRateLimitOptions as RateLimitOptions;
                break;

            case RateLimitType.IP:
                _processor = new IpRateLimitProcessor(_config.IpRateLimitOptions
                                                      , memoryCacheRateLimitCounterStore, memoryCacheIpPolicyStore, rateLimitConfiguration);
                _options = _config.IpRateLimitOptions as RateLimitOptions;
                break;

            default:
                _processor = new IpRateLimitProcessor(_config.IpRateLimitOptions
                                                      , memoryCacheRateLimitCounterStore, memoryCacheIpPolicyStore, rateLimitConfiguration);
                _options = _config.IpRateLimitOptions as RateLimitOptions;
                break;
            }
        }
Ejemplo n.º 12
0
        // puts thread to sleep if limit is exceeded and revives it when limit renews
        private async Task Limit(RateLimitType type)
        {
            if (type == RateLimitType.NONE)
            {
                return;
            }

            // Updating rate limits if more than 1 second have passed
            if (DateTime.Now - _limitLastUpdate >= new TimeSpan(0, 0, 1))
            {
                await UpdateRateLimitsAsync();
            }

            // Waiting for time to pass if we exceeded limit
            foreach (var limitTime in _rateLimits.Keys)
            {
                if (_rateLimits[limitTime][type] <= 0)
                {
                    Debug.WriteLine($"Limiting for {GetNextTime(limitTime) - DateTime.Now}");
                    await Task.Delay(GetNextTime(limitTime) - DateTime.Now);
                }
                _rateLimits[limitTime][type]--;
            }
        }
Ejemplo n.º 13
0
 public RateLimitingRule(double percentOfTotal)
 {
     _rateLimitType        = RateLimitType.ByPercent;
     LimitToPercentOfTotal = percentOfTotal;
 }
Ejemplo n.º 14
0
 public RateLimit GetRateLimit(RateLimitType type = RateLimitType.Global)
 {
     return(type == RateLimitType.Local
         ? _localRateLimit
         : _globalRateLimit);
 }
Ejemplo n.º 15
0
 /// <summary>
 /// get data from Retry-After
 /// </summary>
 /// <param name="prop">
 /// </param>
 /// <param name="limitType">
 /// </param>
 /// <param name="retryAfterSeconds">
 /// value of Retry-After
 /// </param>
 public void SetRetryTime(RateLimitProperties prop, RateLimitType limitType, int retryAfterSeconds)
 {
     SetRetryTime(prop.Platform, prop.UrlType, prop.ApiName, limitType, prop.ApiMethod, retryAfterSeconds);
 }
Ejemplo n.º 16
0
 public RateLimit(RateLimitType rateLimitType, IRiotApiConfig config)
 {
     _rateLimitType = rateLimitType;
     _config        = config;
 }
Ejemplo n.º 17
0
 public RateLimitingRule(Func <T> getRateLimitStatus, double percentOfTotal)
 {
     _rateLimitType        = RateLimitType.ByPercent;
     GetRateLimitStatus    = getRateLimitStatus;
     LimitToPercentOfTotal = percentOfTotal;
 }
Ejemplo n.º 18
0
 public RateLimitingRule(Predicate <T> rateLimitIf)
 {
     _rateLimitType = RateLimitType.ByPredicate;
     RateLimitIf    = rateLimitIf;
 }
Ejemplo n.º 19
0
 public RegionRule(string name, RateLimitType rateLimitType, RateLimitLevel rateLimitLevel) : base(name, rateLimitType, rateLimitLevel)
 {
 }
Ejemplo n.º 20
0
 public ResourceRule(string name, string resource, Region region, RateLimitType rateLimitType, RateLimitLevel rateLimitLevel) : base(name, rateLimitType, rateLimitLevel)
 {
     this.Resource = resource;
 }
Ejemplo n.º 21
0
 public RateLimitingRule(Func <T> getRateLimitStatus, Predicate <T> rateLimitIf)
 {
     _rateLimitType     = RateLimitType.ByPredicate;
     GetRateLimitStatus = getRateLimitStatus;
     RateLimitIf        = rateLimitIf;
 }
Ejemplo n.º 22
0
 /// <summary>
 /// specified rate limit
 /// </summary>
 /// <param name="ts">
 /// timer interval
 /// </param>
 /// <param name="limit">
 /// max. limit
 /// </param>
 /// <param name="limitType">
 /// RiotGames X-Rate-Limit-Type
 /// </param>
 public ApiLimit(TimeSpan ts, int limit, RateLimitType limitType)
 {
     Limit     = limit;
     Time      = ts;
     LimitType = limitType;
 }