graphql-limit-count
graphql-limit-count 插件使用固定窗口算法,根据 GraphQL 查询(Queries)或变更(Mutations)的深度来限制请求速率。
在 GraphQL 中,深度是指查询或变更中的嵌套层级数。以下是一个深度为 3 的查询示例:
{
a {
b {
c
}
}
}
graphql-limit-count 插件通过在给定时间间隔内限制深度的配额来进行速率限制。例如,如果将 30 秒间隔内的配额设置为 4,则允许深度为 3 的请求通过。在该 30 秒内剩余的配额为 1。如果在同一个 30 秒间隔内发送一个深度为 2 的请求,它将被拒绝。
本地限速与基于 Redis 的限速
graphql-limit-count 插件支持两种限速模式:
- 本地限速:每个网关实例独立实施限速。每个实例维护自己的计数器,因此当流量分散到多个实例时,实际限额约为“限额 × 实例数”。未设置
policy或将其设置为local时,这是默认模式。 - 基于 Redis 的限速:通过 Redis 在所有网关实例之间共享限额。所有实例共享同一配额,因此配置的限额适用于全部网关实例。
示例
以下示例使用 GitHub GraphQL API 端点作为上游,并演示了如何在不同场景下配置 graphql-limit-count。
要进行后续操作,请创建一个 GitHub 个人访问令牌(Personal Access Token),并为你想要交互的资源配置适当的权限范围。
基于远程地址进行速率限制
以下示例演示了如何通过单个变量 remote_addr 对 GraphQL 请求进行速率限制。
创建一个启用了 graphql-limit-count 插件的路由,配置为每个远程地址在 30 秒窗口内允许的深度配额为 2:
- Admin API
- ADC
- Ingress Controller
curl "http://127.0.0.1:9180/apisix/admin/routes" -X PUT \
-H "X-API-KEY: ${ADMIN_API_KEY}" \
-d '{
"id": "graphql-limit-count-route",
"uri": "/graphql",
"plugins": {
"graphql-limit-count": {
"count": 2,
"time_window": 30,
"rejected_code": 429,
"key_type": "var",
"key": "remote_addr",
"policy": "local"
}
},
"upstream": {
"type": "roundrobin",
"pass_host": "node",
"scheme": "https",
"nodes": {
"api.github.com:443": 1
}
}
}'
adc.yaml
services:
- name: graphql-service
routes:
- uris:
- /graphql
name: graphql-limit-count-route
plugins:
graphql-limit-count:
count: 2
time_window: 30
rejected_code: 429
key_type: var
key: remote_addr
policy: local
upstream:
type: roundrobin
scheme: https
nodes:
- host: api.github.com
port: 443
weight: 1
将配置同步到网关:
adc sync -f adc.yaml
- Gateway API
- APISIX CRD
graphql-limit-count-ic.yaml
apiVersion: v1
kind: Service
metadata:
namespace: aic
name: github-graphql-external-domain
spec:
type: ExternalName
externalName: api.github.com
---
apiVersion: apisix.apache.org/v1alpha1
kind: BackendTrafficPolicy
metadata:
namespace: aic
name: github-graphql-https
spec:
targetRefs:
- name: github-graphql-external-domain
kind: Service
group: ""
passHost: node
scheme: https
---
apiVersion: apisix.apache.org/v1alpha1
kind: PluginConfig
metadata:
namespace: aic
name: graphql-limit-count-plugin-config
spec:
plugins:
- name: graphql-limit-count
config:
count: 2
time_window: 30
rejected_code: 429
key_type: var
key: remote_addr
policy: local
---
apiVersion: gateway.networking.k8s.io/v1
kind: HTTPRoute
metadata:
namespace: aic
name: graphql-limit-count-route
spec:
parentRefs:
- name: apisix
rules:
- matches:
- path:
type: Exact
value: /graphql
filters:
- type: ExtensionRef
extensionRef:
group: apisix.apache.org
kind: PluginConfig
name: graphql-limit-count-plugin-config
backendRefs:
- name: github-graphql-external-domain
port: 443
graphql-limit-count-ic.yaml
apiVersion: apisix.apache.org/v2
kind: ApisixUpstream
metadata:
namespace: aic
name: github-graphql-external-domain
spec:
ingressClassName: apisix
scheme: https
passHost: node
externalNodes:
- type: Domain
name: api.github.com
port: 443
---
apiVersion: apisix.apache.org/v2
kind: ApisixRoute
metadata:
namespace: aic
name: graphql-limit-count-route
spec:
ingressClassName: apisix
http:
- name: graphql-limit-count-route
match:
paths:
- /graphql
upstreams:
- name: github-graphql-external-domain
plugins:
- name: graphql-limit-count
enable: true
config:
count: 2
time_window: 30
rejected_code: 429
key_type: var
key: remote_addr
policy: local
将配置应用到集群:
kubectl apply -f graphql-limit-count-ic.yaml