Skip to content

Commit fea3ca1

Browse files
feat: add weight validation (must be >= 1) to HttpRequestRegexMatcher
Reject non-positive weight values in the constructor to prevent weight=0 (free requests) or negative weights (budget restoration) from bypassing rate limits. Adds two tests for validation. Co-Authored-By: Daryna Ishchenko <darina.ishchenko17@gmail.com>
1 parent 9bdf9db commit fea3ca1

2 files changed

Lines changed: 12 additions & 0 deletions

File tree

airbyte_cdk/sources/streams/call_rate.py

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -179,6 +179,8 @@ def __init__(
179179
where different endpoints consume different amounts from a shared budget.
180180
If not set, each request counts as 1.
181181
"""
182+
if weight is not None and weight < 1:
183+
raise ValueError(f"weight must be >= 1, got {weight}")
182184
self._weight = weight
183185
self._method = method.upper() if method else None
184186

unit_tests/sources/streams/test_call_rate.py

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -376,6 +376,16 @@ def test_matcher_weight_is_stored(self):
376376
matcher = HttpRequestRegexMatcher(url_path_pattern=r"/api/test", weight=60)
377377
assert matcher.weight == 60
378378

379+
def test_matcher_rejects_zero_weight(self):
380+
"""HttpRequestRegexMatcher raises ValueError for weight=0."""
381+
with pytest.raises(ValueError, match="weight must be >= 1"):
382+
HttpRequestRegexMatcher(url_path_pattern=r"/api/test", weight=0)
383+
384+
def test_matcher_rejects_negative_weight(self):
385+
"""HttpRequestRegexMatcher raises ValueError for negative weight."""
386+
with pytest.raises(ValueError, match="weight must be >= 1"):
387+
HttpRequestRegexMatcher(url_path_pattern=r"/api/test", weight=-5)
388+
379389
def test_policy_get_weight_returns_matcher_weight(self):
380390
"""BaseCallRatePolicy.get_weight returns weight from the matching matcher."""
381391
policy = MovingWindowCallRatePolicy(

0 commit comments

Comments
 (0)