转载文章,原文地址:https://blog.eood.cn/rate-limiting
Rate limiting 在 Web 架构中非常重要,是互联网架构可靠性保证重要的一个方面。
从最终用户访问安全的角度看,设想有人想暴力碰撞网站的用户密码;或者有人攻击某个很耗费资源的接口;或者有人想从某个接口大量抓取数据。大部分人都知道应该增加 Rate limiting,做请求频率限制。从安全角度,这个可能也是大部分能想到,但不一定去做的薄弱环节。
从整个架构的稳定性角度看,一般 SOA 架构的每个接口的有限资源的情况下,所能提供的单位时间服务能力是有限的。假如超过服务能力,一般会造成整个接口服务停顿,或者应用 Crash,或者带来连锁反应,将延迟传递给服务调用方造成整个系统的服务能力丧失。有必要在服务能力超限的情况下 Fail Fast。
另外,根据排队论,由于 API 接口服务具有延迟随着请求量提升迅速提升的特点,为了保证 SLA 的低延迟,需要控制单位时间的请求量。这也是 Little’s law 所说的。
还有,公开 API 接口服务,Rate limiting 应该是一个必备的功能,否则公开的接口不知道哪一天就会被服务调用方有意无意的打垮。
所以,提供资源能够支撑的服务,将过载请求快速抛弃对整个系统架构的稳定性非常重要。这就要求在应用层实现 Rate limiting 限制。
常见的 Rate limiting 的实现方式
Proxy 层的实现,针对部分 URL 或者 API 接口进行访问频率限制
Nginx 模块
limit_req_zone $binary_remote_addr zone=one:10m rate=1r/s; server { location /search/ { limit_req zone=one burst=5; }
详细参见:ngx_http_limit_req_module
Haproxy 提供的功能
详细参见:Haproxy Rate limit 模块
Java、Scala JVM 系应用层实现
Google Guava 提供了一个 RateLimiter 实现。使用方式简单明了,在自己的应用中简单封装即可,放到 HTTP 服务或者其他逻辑接口调用的前端。
final RateLimiter rateLimiter = RateLimiter.create(2.0); // rate is "2 permits per second" void submitTasks(List<Runnable> tasks, Executor executor) { for (Runnable task : tasks) { rateLimiter.acquire(); // may wait executor.execute(task); } }
基于 Redis 功能的实现
这个在 Redis 官方文档有非常详细的实现。一般适用于所有类型的应用,比如 PHP、Python 等等。Redis 的实现方式可以支持分布式服务的访问频率的集中控制。Redis 的频率限制实现方式还适用于在应用中无法状态保存状态的场景。
相关推荐
Both designs are capable of rate limiting thousands of flows with negligible overhead (less than 3% in the tested configuration). We demonstrate that our TCP-centric design is scalable to hundreds of ...
此外,我们将探索Kong提供的更多插件,例如: Rate Limiting , Prometheus和StatsD插件。项目图应用简单服务Spring Boot Java Web应用程序公开了两个端点: /api/public :任何人都可以访问,但不安全; /api/...
ICNIRP Guidelines:2020 For Limiting Exposure to Electromagnetic
composer require danharrin/livewire-rate-limiting 引入了速率限制改进功能后,此软件包至少需要Laravelv8.x。 用法 将DanHarrin\LivewireRateLimiting\WithRateLimiting特性应用于您的Livewire组件: <?php ...
通过config.json(aka rate-limiting.conf)调整规范。 安全问题:确保您的请求缓存文件不能被公众访问。 用法: [1]编辑您的配置: 笔记: 如果“ die_on_rate_limit”为false,则访问者在受限时将被重定向到“ ...
Supply-limiting mechanisms
haproxy-http-based-rate-limiting 基于HTTP头内容和其他具有HAProxy的高级信息的限速请求的以代码为中心的博客文章的示例配置。 有关如何使用它的详细信息,请参见
安装redis_rate支持2个最新的Go版本,并且需要具有支持的Go版本。 因此,请确保初始化Go模块:go mod init github.com/my/repo 然后安装redis_rate / v9(在导入中注意v9 ;忽略它是一个普遍的错误):go get github...
PPAP的目的是用来确定供应商是否已经正确理解了顾客工程设计记录和规范的所有要求,以及其生产过程是否具有潜在能力,在实际生产过程中按规定的生产节拍满足顾客要求的产品。PPAP必须适用于提供散装材料、生产材料、...
* TCP: speed limiting, data logging, etc * TCP: passwording of tunnels * SMTP: antispam (DNS/RBL & greylisting) * SMTP: antivirus (using existing installation of Sophos AV or Grisoft AVG) * SMTP:...
部署方式为了使部署工作正常,您需要在创建一个免费帐户,并获取Redis的实例信息-REDIS_HOST,REDIS_PORT和REDIS_PASSWORD。 您必须将它们作为环境变量传递(在.env文件中或通过服务器配置,例如Heroku Config ...
漏桶算法 Leaky Bucket 是网络世界中流量整形(Traffic Shaping)或速率限制(Rate Limiting)时经常使用的一种算法 它的主要目的是控制数据注入到网络的速率 平滑网络上的突发流量 漏桶算法提供了一种机制 通过它 ...
配置选项 ... ssl_no_verify false # default: false ... rate_limit_msec 100 # default: 0 = no rate limiting raise_on_error false # default: true recoverable_status_codes 503, 400 # default: 503
正在安装yarn add axios-rate-limit用法import axios from 'axios' ;import rateLimit from 'axios-rate-limit' ;// sets max 2 requests per 1 second, other will be delayed// note maxRPS is a shorthand for ...
CB速率限制器Couchbase API Rate Limiter是一个超级简单PHP软件包,用于限制对公共API的... 安装CB Rate Limiter的最简单和推荐的方法是通过composer: composer require gnikolovski/cb-rate-limiter如何使用它? 使用
使用rate_limiter我们可以比以往更轻松地将这些策略应用于常规的dart函数。 (灵感来自 ) 指数 冲洗 消除 安装 将以下内容添加到您的pubspec.yaml ,并将[version]替换为最新版本: dependencies : rate_...
最高限速用于Express和Koa的轻量级速率限制器中间件。 用于将重复请求限制为公共API。安装$ npm install --save cov-rate-limit例考阿const Koa = require ( 'koa' )const RateLimit = require ( 'cov-rate-limit' )...
fastify-rate-limit 您的路线的低开销速率限制器。 支持 Fastify 2.x - 3.x semver 范围。 Fastify 1.x 兼容性请参考及相关版本。 安装 npm i fastify-rate-limit 用法 注册插件,如果需要,传递一些自定义选项。...
Brennan and Reed 131 analyzed the effects of hard limiting, and their conclusions suggest that it does not degrade the steady-state performance of the adaptive processor.Standard and hard-limited ...