/// <summary>
        /// 添加节流控制
        /// </summary>
        /// <param name="services">The services.</param>
        /// <param name="configuration">The configuration.</param>
        /// <returns></returns>
        /// <exception cref="ArgumentNullException">services</exception>
        /// <exception cref="System.ArgumentNullException">services
        /// or
        /// setupAction</exception>
        public static IServiceCollection AddThrottle(this IServiceCollection services, IConfiguration configuration)
        {
            if (services == null)
            {
                throw new ArgumentNullException(nameof(services));
            }

            var options = new ThrottleOptions();

            configuration.Bind(options);

            services.TryAddSingleton(options);

            services.Configure <ThrottleOptions>(configuration);


            if (options.UseDistributed)
            {
                services.TryAddSingleton <IRateLimitStore, RateLimitStoreDistributed>();
            }
            else
            {
                services.TryAddSingleton <IRateLimitStore, RateLimitStoreMemory>();
            }

            services.AddSingleton <IRateLimiter, RateLimiter>();
            services.AddTransient <IThrottleService, ThrottleService>();
            services.AddScoped <ThrottlingFilter>();
            services.Configure <MvcOptions>(opt =>
            {
                opt.Filters.AddService(typeof(ThrottlingFilter));
            });

            return(services);
        }
示例#2
0
        /// <summary>
        /// Creates a new instace of <see cref="ThrottleFilter"/>.
        /// </summary>
        /// <param name="throttleService">The <see cref="IThrottleService"/>.</param>
        /// <param name="strategyProvider">The <see cref="IThrottleStrategyProvider"/>.</param>
        public ThrottleFilter(IOptions <ThrottleOptions> options, IThrottleService throttleService, IThrottleStrategyProvider strategyProvider, ISystemClock clock)
        {
            if (options == null)
            {
                throw new ArgumentNullException(nameof(options));
            }

            if (throttleService == null)
            {
                throw new ArgumentNullException(nameof(throttleService));
            }

            if (strategyProvider == null)
            {
                throw new ArgumentNullException(nameof(strategyProvider));
            }

            if (clock == null)
            {
                throw new ArgumentNullException(nameof(clock));
            }

            _throttleService  = throttleService;
            _strategyProvider = strategyProvider;
            _options          = options.Value;
            _clock            = clock;
        }
示例#3
0
        // This method gets called by the runtime. Use this method to configure the HTTP request pipeline.
        public void Configure(IApplicationBuilder app, IHostingEnvironment env)
        {
            // For demo purpose - set client ID directly from query string's "username" parameter.
            app.Use((httpContext, next) =>
            {
                var userName = httpContext.Request.Query["username"];

                if (!string.IsNullOrWhiteSpace(userName))
                {
                    httpContext.User = new ClaimsPrincipal(new ClaimsIdentity(new[] { new Claim(ClaimTypes.Name, userName) }, "myCustomScheme"));
                }
                return(next());
            });

            /*
             * You can use either of IP or ID throttle as of below. Or both.
             */

            // IP address throttle.
            var ipOptions = new ThrottleOptions();

            Configuration.GetSection("IpThrottle").Bind(ipOptions);
            //app.UseIpThrottle(ipOptions);

            // ID throttle.
            var idOptions = new ThrottleOptions();

            Configuration.GetSection("IdThrottle").Bind(idOptions);
            idOptions.ConfigureRequest((context, request) =>
            {
                // Demo for custom ID setting.
                // Actually this is the default implementation.
                request.ClientId = context.User?.Identity?.Name;
            });
            app.UseIdThrottle(idOptions);

            if (env.IsDevelopment())
            {
                app.UseDeveloperExceptionPage();
            }

            app.UseMvc();
        }
        /// <summary>
        /// 添加默认的节流控制
        /// </summary>
        /// <param name="services">The services.</param>
        /// <param name="options">节流选项设置</param>
        /// <returns></returns>
        /// <exception cref="System.ArgumentNullException">
        /// services
        /// or
        /// setupAction
        /// </exception>
        public static IServiceCollection AddThrottle(this IServiceCollection services, ThrottleOptions options)
        {
            if (services == null)
            {
                throw new ArgumentNullException(nameof(services));
            }

            if (options == null)
            {
                options = new ThrottleOptions();
            }

            services.TryAddSingleton(options);

            services.Configure <ThrottleOptions>(setup =>
            {
                setup.EnableGlobalFilter = options.EnableGlobalFilter;
                setup.Policy             = options.Policy;
                setup.UseDistributed     = options.UseDistributed;
            });

            if (options.UseDistributed)
            {
                services.TryAddSingleton <IRateLimitStore, RateLimitStoreDistributed>();
            }
            else
            {
                services.TryAddSingleton <IRateLimitStore, RateLimitStoreMemory>();
            }

            services.AddSingleton <IRateLimiter, RateLimiter>();
            services.AddTransient <IThrottleService, ThrottleService>();
            services.AddScoped <ThrottlingFilter>();
            services.Configure <MvcOptions>(opt =>
            {
                opt.Filters.AddService(typeof(ThrottlingFilter));
            });

            return(services);
        }
示例#5
0
        /// <summary>
        /// Adds the <see cref="IdThrottleMiddleware" /> to the specified <see cref="IApplicationBuilder" />, which enables throttle capabilities.
        /// </summary>
        /// <param name="app">The <see cref="IApplicationBuilder" /> to add the middleware to.</param>
        /// <param name="options">The throttle options.</param>
        /// <returns>A reference to this instance after the operation has completed.</returns>
        public static IApplicationBuilder UseIdThrottle(this IApplicationBuilder app, ThrottleOptions options)
        {
            if (app == null)
            {
                throw new ArgumentNullException(nameof(app));
            }

            if (options == null)
            {
                throw new ArgumentNullException(nameof(options));
            }

            return(app.UseMiddleware <IdThrottleMiddleware>(options));
        }
 public static ShouldThrottle Create(
     this ThrottleOptions options,
     Action <int>?throttledCallBack = null
     ) => Create(options.MaxEventsInPeriod, options.Period, throttledCallBack);