From 7c4f91b780884b54aebf1c34c1181d4c6b431a7e Mon Sep 17 00:00:00 2001 From: Shiv Tyagi Date: Tue, 24 Mar 2026 22:17:12 +0530 Subject: [PATCH] web: make rate limiter storage uri configurable --- web/core/config.py | 15 +++++++++++++++ web/core/limiter.py | 6 +----- 2 files changed, 16 insertions(+), 5 deletions(-) diff --git a/web/core/config.py b/web/core/config.py index 9bfa170..93225eb 100644 --- a/web/core/config.py +++ b/web/core/config.py @@ -67,6 +67,21 @@ def remotes_json_path(self) -> str: """Path to remotes.json configuration.""" return os.path.join(self.base_dir, 'configs', 'remotes.json') + @property + def rate_limiter_storage_uri(self) -> str: + """ + Storage URI for the rate limiter. + + Uses CBS_RATE_LIMITER_STORAGE_URI env var if set and non-empty, + otherwise falls back to redis backend. When using the redis backend, + db index 1 is used to avoid conflicts with any other components that + use db index 0. + """ + uri = os.getenv('CBS_RATE_LIMITER_STORAGE_URI', '').strip() + if uri: + return uri + return f"redis://{self.redis_host}:{self.redis_port}/1" + @property def enable_inbuilt_builder(self) -> bool: """Whether to enable the inbuilt builder.""" diff --git a/web/core/limiter.py b/web/core/limiter.py index ec821a1..831dedf 100644 --- a/web/core/limiter.py +++ b/web/core/limiter.py @@ -10,13 +10,9 @@ settings = get_settings() -# We use the same redis instance which is used to store build metadata -# and other cached data. To keep that data separate, we use db-1 of the -# redis instance instead of the default db-0. -REDIS_DB_NUMBER = 1 limiter = Limiter( key_func=get_remote_address, - storage_uri=f"redis://{settings.redis_host}:{settings.redis_port}/{REDIS_DB_NUMBER}", + storage_uri=settings.rate_limiter_storage_uri, strategy="fixed-window", )