agentscope icon indicating copy to clipboard operation
agentscope copied to clipboard

Add resource limiter

Open rayrayraykk opened this issue 1 year ago • 1 comments

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.py which includes the resources_limit decorator 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

  1. Set up a Redis server and configure the REDIS_HOST, REDIS_PORT, and REDIS_DB environment variables.
  2. Run the script and follow the prompts to test the resource limiter.

Notes

  • The module uses redis libraries, 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

rayrayraykk avatar Jul 25 '24 10:07 rayrayraykk

~~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.

rayrayraykk avatar Jul 25 '24 11:07 rayrayraykk

Closing for new version preparation

DavdGao avatar Aug 13 '25 13:08 DavdGao