--- title: "脚本编译速率限制配置" date: 2026-02-16 lastmod: 2026-02-16 description: "控制脚本编译速率限制的配置项说明" tags: ["脚本配置", "性能保护", "安全控制"] summary: "配置项作用 # script.max_compilations_rate 配置项控制在指定时间内允许编译的脚本数量限制。此配置用于防止过多的脚本编译操作消耗过多 CPU 资源,保护系统稳定性。 配置项类型 # 该配置项为动态配置,可以在运行时通过集群设置 API 进行修改。 默认值 # use-context(根据脚本上下文自动调整) 是否必需 # 可选配置项(有默认值) 取值范围 # - use-context(自动)或具体速率(如 10/1m) 配置格式 # # 默认配置(自动调整) script.max_compilations_rate: use-context # 固定速率限制 script.max_compilations_rate: 10/1m # 更宽松的限制 script.max_compilations_rate: 100/1m # 无限制 script.max_compilations_rate: unlimited 相关配置项 # 配置项 默认值 说明 script.max_compilations_rate use-context 编译速率限制 script.cache.max_size 100 编译缓存大小 script.cache.expire 0ms 缓存过期时间 工作原理 # 编译速率限制机制:" --- ## 配置项作用 `script.max_compilations_rate` 配置项控制在指定时间内允许编译的脚本数量限制。此配置用于防止过多的脚本编译操作消耗过多 CPU 资源,保护系统稳定性。 ## 配置项类型 该配置项为**动态配置**,可以在运行时通过集群设置 API 进行修改。 ## 默认值 ``` use-context(根据脚本上下文自动调整) ``` ## 是否必需 **可选配置项**(有默认值) ## 取值范围 ``` - use-context(自动)或具体速率(如 10/1m) ``` ## 配置格式 ```yaml # 默认配置(自动调整) script.max_compilations_rate: use-context # 固定速率限制 script.max_compilations_rate: 10/1m # 更宽松的限制 script.max_compilations_rate: 100/1m # 无限制 script.max_compilations_rate: unlimited ``` ## 相关配置项 | 配置项 | 默认值 | 说明 | |-------|-------|------| | `script.max_compilations_rate` | use-context | 编译速率限制 | | `script.cache.max_size` | 100 | 编译缓存大小 | | `script.cache.expire` | 0ms | 缓存过期时间 | ## 工作原理 编译速率限制机制: ``` ┌─────────────────────────────────────────────────────────────────┐ │ 编译速率限制 │ └─────────────────────────────────────────────────────────────────┘ 脚本编译请求 │ ▼ 检查编译计数 │ ├── use-context 模式 │ │ │ ├── 根据脚本上下文自动调整 │ ├── 脚本查询 → 较宽松 │ └── 脚本更新 → 较严格 │ └── 固定速率模式 │ └── 计算时间段内编译次数 │ ├── 未超过限制 → 允许编译 └── 超过限制 → 拒绝请求 ``` ## 使用场景 ### 1. 默认配置(推荐) ```yaml script.max_compilations_rate: use-context ``` 适用于大多数集群配置,自动根据场景调整。 ### 2. 严格限制 ```yaml script.max_compilations_rate: 10/1m ``` **适用场景:** - 资源受限环境 - 防止脚本滥用 - 保护系统稳定性 ### 3. 宽松限制 ```yaml script.max_compilations_rate: 100/1m ``` **适用场景:** - 大量脚本操作 - 脚本开发测试 - 资源充足 ### 4. 无限制 ```yaml script.max_compilations_rate: unlimited ``` **适用场景:** - 受信任环境 - 需要大量编译 - 有其他保护机制 ## 推荐设置建议 | 环境 | 推荐配置 | 说明 | |-----|---------|------| | 生产环境 | use-context | 自动调整 | | 资源受限 | 10-50/1m | 严格限制 | | 开发测试 | 100-500/1m | 宽松限制 | | 高频脚本 | use-context | 根据上下文 | ## 动态配置示例 ```bash # 更新集群配置 PUT /_cluster/settings { "transient": { "script.max_compilations_rate": "50/1m" } } # 设置为无限制 PUT /_cluster/settings { "transient": { "script.max_compilations_rate": "unlimited" } } ``` ## 监控建议 ```bash # 查看当前配置 GET /_cluster/settings?filter_path=*.script.max_compilations_rate # 查看脚本统计 GET /_nodes/stats/script?filter_path=**.compilations # 查看缓存状态 GET /_nodes/stats/script?filter_path=**.cache ``` ## 注意事项 1. **动态更新**:此配置为动态配置,可在线修改 2. **use-context 推荐**:生产环境建议使用自动模式 3. **资源保护**:防止过多编译消耗 CPU 4. **与缓存配合**:与编译缓存配合使用 5. **合理设置**:根据脚本使用频率调整