agentscope
agentscope copied to clipboard
Add resource limiter
Add resource limiter module
Description
This PR adds a resource limiter module that limits the number of concurrent executions of a function based on available resources using Redis.
Changes
- Added
resource_limiter.pywhich includes theresources_limitdecorator and helper functions. - Implemented Redis-based resource counting and queuing mechanisms.
- Added a simulation/test script within the module.
- Support 2 limit mode: rate (query per minute limit) and capacity (concurrent executions allowed):
def resources_limit(function: Callable) -> Callable:
"""
The decorated class must contain `self.resource_limit_number`
and `self.resource_limit_type` (choose from `rate` and `capacity`).
- `self.resource_limit_number`: An integer representing the resource limit.
If `self.resource_limit_type` is `capacity`, it represents the maximum
number of concurrent executions allowed.
If `self.resource_limit_type` is `rate`, it represents the maximum number
of executions allowed per minute.
- `self.resource_limit_type`: A string that specifies the type of limit,
either `rate` or `capacity`.
When `self.resource_limit_type` is `rate`, the unit is counts per minute.
"""
...
How to test
- Set up a Redis server and configure the
REDIS_HOST,REDIS_PORT, andREDIS_DBenvironment variables. - Run the script and follow the prompts to test the resource limiter.
Notes
- The module uses
redislibraries, ensure they are installed in your environment.
Checklist
Please check the following items before code is ready to be reviewed.
- [X] Code has passed all tests
- [X] Docstrings have been added/updated in Google Style
- [X] Documentation has been updated
- [X] Code is ready for review
~~The current implementation of resource limiter is only for the resource number. And I will add a new implementation for qps as Dawei suggested.~~
The above feature has been implemented.
Closing for new version preparation