Skip to content

Commit

Permalink
replace RateLimit.Run with @contextmanager
Browse files Browse the repository at this point in the history
  • Loading branch information
orian committed Mar 4, 2025
1 parent ada5b01 commit 649024f
Show file tree
Hide file tree
Showing 3 changed files with 30 additions and 25 deletions.
33 changes: 12 additions & 21 deletions posthog/clickhouse/client/limit.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import dataclasses
import time
from contextlib import contextmanager
from functools import wraps
from typing import Optional
from collections.abc import Callable
Expand Down Expand Up @@ -64,29 +65,19 @@ class RateLimit:
get_time: Callable[[], int] = lambda: int(time.time())
applicable: Optional[Callable] = None # allows to put a constraint on when rate limiting is used
ttl: int = 60
redis_client = redis.get_client()

@property
def redis_client(self):
return redis.get_client()

class Run:
def __init__(self, limit, running_task_key, task_id):
self.limit = limit
self.running_task_key = running_task_key
self.task_id = task_id

def __enter__(self):
pass

def __exit__(self, applicable, running_task_key, task_id):
if self.limit:
self.limit.release(self.running_task_key, self.task_id)

@contextmanager
def run(self, *args, **kwargs):
if self.applicable and not self.applicable(*args, **kwargs):
return self.Run(None, None, None)
running_tasks_key, task_id = self.use(*args, **kwargs)
return self.Run(self, running_tasks_key, task_id)
cleanup = None
if not self.applicable or self.applicable(*args, **kwargs):
running_task_key, task_id = self.use(*args, **kwargs)
cleanup = True
try:
yield
finally:
if cleanup:
self.release(running_task_key, task_id)

def use(self, *args, **kwargs):
"""
Expand Down
14 changes: 14 additions & 0 deletions posthog/clickhouse/client/test/test_limit.py
Original file line number Diff line number Diff line change
Expand Up @@ -95,3 +95,17 @@ def test_run_applicable(self):
result += 4

assert result == 7

def test_exception(self):
result = 0
with self.assertRaises(Exception):
result += 1
with self.limit.run(is_api=True, team_id=9, task_id=17):
result += 2
raise Exception()
result += 4

Check failure on line 106 in posthog/clickhouse/client/test/test_limit.py

View workflow job for this annotation

GitHub Actions / Python code quality checks

Statement is unreachable

with self.limit.run(is_api=True, team_id=9, task_id=17):
result += 8

assert result == 11
8 changes: 4 additions & 4 deletions posthog/tasks/tasks.py
Original file line number Diff line number Diff line change
Expand Up @@ -67,11 +67,11 @@ def process_query_task(
is_api: bool,
limit_context: Optional[LimitContext] = None,
) -> None:
"""
Kick off query
Once complete save results to redis
"""
with get_api_personal_rate_limiter().run(is_api=is_api, team_id=team_id, task_id=query_id):
"""
Kick off query
Once complete save results to redis
"""
from posthog.clickhouse.client import execute_process_query

execute_process_query(
Expand Down

0 comments on commit 649024f

Please sign in to comment.