Skip to content

Commit b0d6217

Browse files
committed
web: make rate limiter storage uri configurable
1 parent 7fa60f9 commit b0d6217

2 files changed

Lines changed: 16 additions & 5 deletions

File tree

web/core/config.py

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -67,6 +67,21 @@ def remotes_json_path(self) -> str:
6767
"""Path to remotes.json configuration."""
6868
return os.path.join(self.base_dir, 'configs', 'remotes.json')
6969

70+
@property
71+
def rate_limiter_storage_uri(self) -> str:
72+
"""
73+
Storage URI for the rate limiter.
74+
75+
Uses CBS_RATE_LIMITER_STORAGE_URI env var if set and non-empty,
76+
otherwise falls back to redis backend. When using the redis backend,
77+
db index 1 is used to avoid conflicts with any other components that
78+
use db index 0.
79+
"""
80+
uri = os.getenv('CBS_RATE_LIMITER_STORAGE_URI', '').strip()
81+
if uri:
82+
return uri
83+
return f"redis://{self.redis_host}:{self.redis_port}/1"
84+
7085
@property
7186
def enable_inbuilt_builder(self) -> bool:
7287
"""Whether to enable the inbuilt builder."""

web/core/limiter.py

Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -10,13 +10,9 @@
1010

1111
settings = get_settings()
1212

13-
# We use the same redis instance which is used to store build metadata
14-
# and other cached data. To keep that data separate, we use db-1 of the
15-
# redis instance instead of the default db-0.
16-
REDIS_DB_NUMBER = 1
1713
limiter = Limiter(
1814
key_func=get_remote_address,
19-
storage_uri=f"redis://{settings.redis_host}:{settings.redis_port}/{REDIS_DB_NUMBER}",
15+
storage_uri=settings.rate_limiter_storage_uri,
2016
strategy="fixed-window",
2117
)
2218

0 commit comments

Comments
 (0)