Esempio n. 1
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;
            }
        }
        static ClientRateLimit()
        {
            if (Setting.Configuration?.IpRateLimiting == null)
            {
                return;
            }

            Logger = Factory.Logger.Value;

            Options = Setting.Configuration.ClientRateLimiting;

            Policies = Setting.Configuration.ClientRateLimitPolicies;

            MemoryCache = new MemoryCacheManager();

            var rateLimitCounterStore = Factory.RateLimitCounterStore.Value;

            IClientPolicyStore clientPolicyStore = new MemoryCacheClientPolicyStore(MemoryCache, Options, Policies);

            Processor = new ClientRateLimitProcessor(Options, rateLimitCounterStore, clientPolicyStore);

            Configurationed = true;
        }