T-DOC
  • Laravel-11
  • Laravel-10
  • Laravel-6X
  • Laravel-5.5
Dcat-Admin
  • Yii2
  • 重构2
  • Laravel-11
  • Laravel-10
  • Laravel-6X
  • Laravel-5.5
Dcat-Admin
  • Yii2
  • 重构2
  • 前言

    • 关于 Yii
    • 从 Yii 1.1 升级
    • Yii 2.0 升级说明
    • 贡献者指引
    • 捐献和赞助
  • 版本管理

    • 发行说明
    • 版本说明
    • 更新记录
  • 快速入门

    • 安装 Yii
    • 目录结构
    • 部署
    • Hello world
    • 使用表单
    • 使用数据库
    • 使用 Gii 生成代码
    • 编辑器与 IDE
  • 核心架构

    • 请求生命周期
    • 单一入口
    • 应用主体
    • 组件
    • 属性
    • 事件
    • 行为
    • 配置
    • 别名
    • 类自动加载
    • 服务定位器
    • 依赖注入容器
  • 高级应用模板

    • 安装
    • 框架结构
    • 运行测试
    • 环境与配置
    • 自定义应用模板
  • 基础功能

    • 路由
    • 控制器
    • 过滤器
    • 请求
    • 响应
    • 模型
    • 视图
    • 模块
    • 小部件
    • 前端资源
    • 扩展
    • Sessions 和 Cookies
    • 错误处理
    • 日志
  • 数据库

    • 数据库访问对象(DAO)
    • 查询构造器
    • 活动记录
    • 数据库迁移
    • Redis
    • Sphinx
    • Elasticsearch
    • MongoDB
  • 安全

    • 认证
    • 授权
    • 加密
    • Email 认证
    • 验证码
    • 社会化登录
    • 密码
  • 进阶功能

    • 缓存
    • 发送邮件
    • HTTP 客户端
    • 国际化
    • 队列
    • 控制台应用
    • 助手类
    • 验证器
  • RESTful Web 服务

    • 快速入门
    • 资源
    • 控制器
    • 路由
    • 格式化响应
    • 认证
    • 速率限制(限流)
    • 版本化
    • 错误处理
  • 开发工具

    • 调试工具栏和调试器
    • 使用 Gii 生成代码

速率限制(限流)

Yii 中文文档 /

速率限制(限流)

To prevent abuse, you should consider adding rate limiting to your APIs. For example, you may want to limit the API usage of each user to be at most 100 API calls within a period of 10 minutes. If too many requests are received from a user within the stated period of the time, a response with status code 429 (meaning “Too Many Requests”) should be returned.

To enable rate limiting, the [[yii\web\User::identityClass|user identity class]] should implement [[yii\filters\RateLimitInterface]]. This interface requires implementation of three methods:

  • getRateLimit(): returns the maximum number of allowed requests and the time period (e.g., [100, 600] means there can be at most 100 API calls within 600 seconds).
  • loadAllowance(): returns the number of remaining requests allowed and the corresponding UNIX timestamp when the rate limit was last checked.
  • saveAllowance(): saves both the number of remaining requests allowed and the current UNIX timestamp.

You may want to use two columns in the user table to record the allowance and timestamp information. With those defined, then loadAllowance() and saveAllowance() can be implemented to read and save the values of the two columns corresponding to the current authenticated user. To improve performance, you may also consider storing these pieces of information in a cache or NoSQL storage.

Implementation in the User model could look like the following:

public function getRateLimit($request, $action)
{
    return [$this->rateLimit, 1]; // $rateLimit requests per second
}

public function loadAllowance($request, $action)
{
    return [$this->allowance, $this->allowance_updated_at];
}

public function saveAllowance($request, $action, $allowance, $timestamp)
{
    $this->allowance = $allowance;
    $this->allowance_updated_at = $timestamp;
    $this->save();
}

Once the identity class implements the required interface, Yii will automatically use [[yii\filters\RateLimiter]] configured as an action filter for [[yii\rest\Controller]] to perform rate limiting check. The rate limiter will throw a [[yii\web\TooManyRequestsHttpException]] when the rate limit is exceeded.

You may configure the rate limiter as follows in your REST controller classes:

public function behaviors()
{
    $behaviors = parent::behaviors();
    $behaviors['rateLimiter']['enableRateLimitHeaders'] = false;
    return $behaviors;
}

When rate limiting is enabled, by default every response will be sent with the following HTTP headers containing the current rate limiting information:

  • X-Rate-Limit-Limit, the maximum number of requests allowed with a time period
  • X-Rate-Limit-Remaining, the number of remaining requests in the current time period
  • X-Rate-Limit-Reset, the number of seconds to wait in order to get the maximum number of allowed requests

You may disable these headers by configuring [[yii\filters\RateLimiter::enableRateLimitHeaders]] to be false, as shown in the above code example.

💖喜欢本文档的,欢迎点赞、收藏、留言或转发,谢谢支持!
作者邮箱:zhuzixian520@126.com,github地址:github.com/zhuzixian520

Last Updated:
Contributors: Hans
Prev
认证
Next
版本化