niemne commented on issue #13179:
URL: https://github.com/apache/apisix/issues/13179#issuecomment-4245575413
refer: (#12977, #12967, #13000, #13004)
1. Schema Changes:
- `rate` and `burst` will accept both numbers and strings (for variable
expressions like `$http_a`).
- Introduce an optional `rules` array for multi-level limiting. Each rule
contains its own `rate`, `burst`, `key`, `key_type`, and optional
`header_prefix`.
- Use `oneOf` validation to require either the legacy fields (`rate` +
`burst`) or the `rules` array, ensuring strict backward compatibility.
2. Implementation Approach:
- Add a runtime helper to resolve `rate`/`burst` string variables to numbers
via `core.utils.resolve_var`.
- In the `access` phase, normalize the configuration into a unified rule
list: legacy mode wraps single fields into one rule; multi-rule mode iterates
the `rules` array, resolves variables, and filters invalid entries.
- Dynamically create the `resty.limit.req` object per request. The
`lrucache.plugin_ctx` will be removed to avoid stale/colliding state when rates
change dynamically, fully aligning with the recent `limit-count` and
`limit-conn` PRs.
- Execute the leaky bucket algorithm (`lim:incoming`) for each normalized
rule, apply delay/reject logic, and support per-rule `header_prefix` for
differentiated response headers.
3. Backward Compatibility:
- Existing configurations without the `rules` field will continue to work
exactly as before.
- Removing the LRU cache guarantees accurate dynamic rate limiting without
side effects.
4. Example Configurations:
- Variable rate (legacy mode):
```json
{"rate": "$http_a", "burst": 10, "key": "remote_addr", "policy": "local"}
```
- Multi-level rules:
```json
{"rules": [{"rate": 100, "burst": 20, "key": "remote_addr"}, {"rate":
"$http_a", "burst": 50, "key": "http_x_user_id", "header_prefix": "user"}],
"policy": "redis", "rejected_code": 429}
```
@Baoyuantop Please review this proposal. Welcome any feedback or
suggestions.
--
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
To unsubscribe, e-mail: [email protected]
For queries about this service, please contact Infrastructure at:
[email protected]