-
Notifications
You must be signed in to change notification settings - Fork 10
added rate limiting #221
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
Open
bradley-erickson
wants to merge
2
commits into
berickson/202504-process-metrics
Choose a base branch
from
berickson/202504-llm-limiting
base: berickson/202504-process-metrics
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
added rate limiting #221
Changes from all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1 +1 @@ | ||
| 0.1.0+2025.04.16T16.05.04.940Z.74ace276.berickson.202504.process.metrics | ||
| 0.1.0+2025.04.30T13.42.23.840Z.72e9d99b.berickson.202504.llm.limiting |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1 +1 @@ | ||
| 0.1.0+2025.04.10T17.37.57.685Z.95ba1c24.berickson.202504.process.metrics | ||
| 0.1.0+2025.04.30T13.42.23.840Z.72e9d99b.berickson.202504.llm.limiting |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,78 @@ | ||
| ''' | ||
| This file defines a rate limit decorator function. | ||
| ''' | ||
| import asyncio | ||
| import collections | ||
| import functools | ||
| import inspect | ||
| import time | ||
|
|
||
| import learning_observer.auth | ||
| import learning_observer.constants | ||
|
|
||
| RATE_LIMITERS = {} | ||
| RL_LOCK = asyncio.Lock() | ||
|
|
||
|
|
||
| def create_rate_limiter(service_name): | ||
| '''Creates a rate limiter for a specific service. | ||
| ''' | ||
| async def check_rate_limit(id): | ||
| '''Check if id has hit the rate limit. | ||
|
|
||
| Uses sliding window to track recent requests. Returns | ||
| whether the current request should be allowed based on | ||
| the configured limits. | ||
| ''' | ||
| # TODO fetch from pmss/define appropriate window | ||
| max_requests_per_window = 2 | ||
| window_seconds = 60 | ||
|
|
||
| async with RL_LOCK: | ||
| now = time.time() | ||
|
|
||
| # Initialize id/service tracking | ||
| limiter_key = f'rate_limit:{service_name}:{id}' | ||
| if limiter_key not in RATE_LIMITERS: | ||
| RATE_LIMITERS[limiter_key] = collections.deque() | ||
|
|
||
| # Expire old requests | ||
| prior_requests_timestamps = RATE_LIMITERS[limiter_key] | ||
| while prior_requests_timestamps and (now - prior_requests_timestamps[0]) > window_seconds: | ||
| prior_requests_timestamps.popleft() | ||
|
|
||
| if len(prior_requests_timestamps) >= max_requests_per_window: | ||
| return False | ||
|
|
||
| prior_requests_timestamps.append(now) | ||
| return True | ||
| return check_rate_limit | ||
|
|
||
|
|
||
| def rate_limited(service_name): | ||
| '''Decorator for async functions needing rate limiting''' | ||
| def decorator(func): | ||
| @functools.wraps(func) | ||
| async def wrapper(*args, **kwargs): | ||
| # Get the appropriate rate limiter | ||
| if 'runtime' not in kwargs: | ||
| raise TypeError(f'`{func.__name__}` requires `runtime` keyword argument for checking rate limits.') | ||
|
|
||
| runtime = kwargs['runtime'] | ||
|
|
||
| check_rate_limit = create_rate_limiter(service_name) | ||
|
|
||
| # Check rate limits before execution | ||
| request = runtime.get_request() | ||
| user = await learning_observer.auth.get_active_user(request) | ||
| user_id = user[learning_observer.constants.USER_ID] | ||
| if not await check_rate_limit(user_id): | ||
| raise PermissionError(f'Rate limit exceeded for {service_name} service') | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I'm actually a bit confused when I'd want this behavior. It'd be helpful to know when this is used. Seems like what I want:
Simply failing seems like it might be annoying to the user. |
||
|
|
||
| function_signature = inspect.signature(func) | ||
| if 'runtime' not in function_signature.parameters: | ||
| kwargs = {k: v for k, v in kwargs.items() if k != 'runtime'} | ||
|
|
||
| return await func(*args, **kwargs) | ||
| return wrapper | ||
| return decorator | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1 +1 @@ | ||
| 0.1.0+2025.04.16T16.05.04.940Z.74ace276.berickson.202504.process.metrics | ||
| 0.1.0+2025.04.18T12.16.24.803Z.1e9da34c.berickson.202504.llm.limiting |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1 +1 @@ | ||
| 0.1.0+2025.04.07T20.48.55.419Z.3bdcc7c9.berickson.202504.process.metrics | ||
| 0.1.0+2025.04.18T12.16.24.803Z.1e9da34c.berickson.202504.llm.limiting |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This should clearly document what kinds of functions this can wrap, and the protocol. Things like this:
Should be in the docstring. Basically, I want to know how I use this.