Kaaado commited on
Commit
c229cda
·
1 Parent(s): 5b75e17

fix: default rate_limiter to memory:// when local redis is used

Browse files
Files changed (1) hide show
  1. app/core/rate_limiter.py +6 -14
app/core/rate_limiter.py CHANGED
@@ -1,11 +1,10 @@
1
  """
2
  Detection Service — Rate Limiter
3
- Uses slowapi with Redis storage (or in-memory fallback if Redis unavailable).
4
- Per-token rate limiting is handled by security.py via Redis.
5
  """
6
  from __future__ import annotations
7
 
8
- import socket
9
  from slowapi import Limiter
10
  from slowapi.util import get_remote_address
11
 
@@ -13,20 +12,13 @@ from app.core.config import get_settings
13
 
14
 
15
  def _make_limiter() -> Limiter:
16
- """Create limiter lazily — gracefully fallback to memory if local Redis is unreachable."""
17
  settings = get_settings()
18
  storage_uri = settings.redis_url
19
 
20
- if "localhost" in storage_uri or "127.0.0.1" in storage_uri:
21
- try:
22
- sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
23
- sock.settimeout(0.5)
24
- res = sock.connect_ex(("127.0.0.1", 6379))
25
- sock.close()
26
- if res != 0:
27
- storage_uri = "memory://"
28
- except Exception:
29
- storage_uri = "memory://"
30
 
31
  return Limiter(
32
  key_func=get_remote_address,
 
1
  """
2
  Detection Service — Rate Limiter
3
+ Uses slowapi with in-memory storage (or Redis if explicitly configured with external REDIS_URL).
4
+ Per-token rate limiting is handled by security.py via Redis/memory.
5
  """
6
  from __future__ import annotations
7
 
 
8
  from slowapi import Limiter
9
  from slowapi.util import get_remote_address
10
 
 
12
 
13
 
14
  def _make_limiter() -> Limiter:
15
+ """Create limiter — defaults to memory:// for cloud/standalone deployments unless remote Redis specified."""
16
  settings = get_settings()
17
  storage_uri = settings.redis_url
18
 
19
+ # Fallback to in-memory rate limiting if redis_url points to local or unconfigured host
20
+ if not storage_uri or "localhost" in storage_uri or "127.0.0.1" in storage_uri:
21
+ storage_uri = "memory://"
 
 
 
 
 
 
 
22
 
23
  return Limiter(
24
  key_func=get_remote_address,