-
Notifications
You must be signed in to change notification settings - Fork 60
feat(gooddata-pipelines): Process workspace backup sequentially #1138
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
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,9 @@ | ||
| # (C) 2025 GoodData Corporation | ||
|
|
||
| """ | ||
| Utility modules for gooddata-pipelines package. | ||
| """ | ||
|
|
||
| from .rate_limiter import RateLimiter | ||
|
|
||
| __all__ = ["RateLimiter"] |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,64 @@ | ||
| # (C) 2025 GoodData Corporation | ||
|
|
||
| import time | ||
| import threading | ||
| import functools | ||
| from typing import Callable, Any, Literal | ||
|
|
||
|
|
||
| class RateLimiter: | ||
| """ | ||
| Rate limiter usable as a decorator and as a context manager. | ||
| - Shared instance decorator: limiter = RateLimiter(); @limiter | ||
| - Per-function decorator: @RateLimiter(calls_per_second=2) | ||
| - Context manager: with RateLimiter(2): ... | ||
| """ | ||
|
|
||
| def __init__(self, calls_per_second: float = 1.0) -> None: | ||
| if calls_per_second <= 0: | ||
| raise ValueError("calls_per_second must be greater than 0") | ||
|
|
||
| self.calls_per_second = calls_per_second | ||
| self.min_interval = 1.0 / calls_per_second | ||
|
|
||
| self._lock = threading.Lock() | ||
| self._last_call_time = 0.0 | ||
|
|
||
| def wait_if_needed(self) -> float: | ||
|
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. Why does this method have a return value? If I see it correctly, it is not stored or read anywhere... |
||
| """Sleep if needed to maintain the rate limit, return actual sleep time.""" | ||
| with self._lock: | ||
| now = time.monotonic() | ||
| since_last = now - self._last_call_time | ||
|
|
||
| if since_last < self.min_interval: | ||
| sleep_time = self.min_interval - since_last | ||
| time.sleep(sleep_time) | ||
| self._last_call_time = time.monotonic() | ||
| return sleep_time | ||
| else: | ||
| self._last_call_time = now | ||
| return 0.0 | ||
|
|
||
| # Decorator support | ||
| def __call__(self, func: Callable[..., Any]) -> Callable[..., Any]: | ||
| @functools.wraps(func) | ||
| def wrapper(*args: Any, **kwargs: Any) -> Any: | ||
| self.wait_if_needed() | ||
| return func(*args, **kwargs) | ||
|
|
||
| return wrapper | ||
|
|
||
| # Context manager support | ||
| def __enter__(self) -> "RateLimiter": | ||
| self.wait_if_needed() | ||
|
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 think it would be great if we could use the rate limiter as a decorator instead of context manager... My assumption is that it could be more versatile (for handling the methods from gooddata_sdk) and less verbose (we could wrap functions/methods with |
||
| return self | ||
|
|
||
| def __exit__( | ||
| self, exc_type: Any, exc_val: Any, exc_tb: Any | ||
| ) -> Literal[False]: | ||
| return False | ||
|
|
||
| def reset(self) -> None: | ||
| """Reset the limiter (useful in tests).""" | ||
| with self._lock: | ||
| self._last_call_time = 0.0 | ||
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.
I think it would be great if this had some tests