Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add resource limiter #366

Open
wants to merge 8 commits into
base: main
Choose a base branch
from
Open

Conversation

rayrayraykk
Copy link
Collaborator

@rayrayraykk rayrayraykk commented Jul 25, 2024

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.

  • Code has passed all tests
  • Docstrings have been added/updated in Google Style
  • Documentation has been updated
  • Code is ready for review

@rayrayraykk
Copy link
Collaborator Author

rayrayraykk commented Jul 25, 2024

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 rayrayraykk changed the title add resource limiter [WIP]add resource limiter Jul 26, 2024
@rayrayraykk rayrayraykk changed the title [WIP]add resource limiter Add resource limiter Jul 29, 2024
src/agentscope/models/model.py Outdated Show resolved Hide resolved
src/agentscope/utils/resource_limiter.py Outdated Show resolved Hide resolved
return result


if __name__ == "__main__":
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It is better to move this into examples

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It is better to move this into examples

It looks like the examples only contain the multi-agent application instead of the function utils example.

queue_key = f"resources_queue_for_{class_name}"

request_id = str(uuid.uuid4()) # Use UUID for unique request IDs
redis_client.lpush(queue_key, request_id)
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Redis has atomic counter functions (INCR, DECR), which are more efficient than list.

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Redis has atomic counter functions (INCR, DECR), which are more efficient than list.

The list is used for FIFO.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants