public RateLimitSettingsBase this[RateLimitType rateLimitType] { get { return(this.RateLimiterSettings[rateLimitType]); } }
/// <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); }
public Rule(string name, RateLimitType rateLimitType, RateLimitLevel rateLimitLevel) { this.Name = name; this.RateLimitType = rateLimitType; this.RateLimitLevel = rateLimitLevel; this.IsActive = false; }
/// <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); }
public Limiter(RateLimitType type, int limit, TimeSpan perPeriod, HttpMethod?method) { Semaphore = new SemaphoreSlim(1, 1); Type = type; Limit = limit; Period = perPeriod; Method = method; }
public RateLimitSettingsBase this[RateLimitType rateLimitType] { get { return(RateLimitSettings[rateLimitType]); } set { RateLimitSettings[rateLimitType] = value; } }
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); } }
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); } }
/// <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; }
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; }
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; } }
// 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]--; } }
public RateLimitingRule(double percentOfTotal) { _rateLimitType = RateLimitType.ByPercent; LimitToPercentOfTotal = percentOfTotal; }
public RateLimit GetRateLimit(RateLimitType type = RateLimitType.Global) { return(type == RateLimitType.Local ? _localRateLimit : _globalRateLimit); }
/// <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); }
public RateLimit(RateLimitType rateLimitType, IRiotApiConfig config) { _rateLimitType = rateLimitType; _config = config; }
public RateLimitingRule(Func <T> getRateLimitStatus, double percentOfTotal) { _rateLimitType = RateLimitType.ByPercent; GetRateLimitStatus = getRateLimitStatus; LimitToPercentOfTotal = percentOfTotal; }
public RateLimitingRule(Predicate <T> rateLimitIf) { _rateLimitType = RateLimitType.ByPredicate; RateLimitIf = rateLimitIf; }
public RegionRule(string name, RateLimitType rateLimitType, RateLimitLevel rateLimitLevel) : base(name, rateLimitType, rateLimitLevel) { }
public ResourceRule(string name, string resource, Region region, RateLimitType rateLimitType, RateLimitLevel rateLimitLevel) : base(name, rateLimitType, rateLimitLevel) { this.Resource = resource; }
public RateLimitingRule(Func <T> getRateLimitStatus, Predicate <T> rateLimitIf) { _rateLimitType = RateLimitType.ByPredicate; GetRateLimitStatus = getRateLimitStatus; RateLimitIf = rateLimitIf; }
/// <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; }