safraeli commited on
Commit
0a569be
·
verified ·
1 Parent(s): 6ffe367

Fix Redis singleton: prevent retry storm on init failure

Browse files
Files changed (1) hide show
  1. src/data/redis_cache.py +8 -2
src/data/redis_cache.py CHANGED
@@ -29,16 +29,19 @@ log = logging.getLogger(__name__)
29
  # ---------------------------------------------------------------------------
30
 
31
  _instance: Optional["RedisCache"] = None
 
32
  _lock = threading.Lock()
33
 
34
 
35
  def get_redis() -> Optional["RedisCache"]:
36
  """Return the global RedisCache instance, or *None* if not configured."""
37
- global _instance
38
 
39
  # Fast path (no lock)
40
  if _instance is not None:
41
  return _instance
 
 
42
 
43
  url = os.environ.get("UPSTASH_REDIS_URL")
44
  token = os.environ.get("UPSTASH_REDIS_TOKEN")
@@ -50,12 +53,15 @@ def get_redis() -> Optional["RedisCache"]:
50
  # Double-check after acquiring lock
51
  if _instance is not None:
52
  return _instance
 
 
53
  try:
54
  _instance = RedisCache(url=url, token=token)
55
  log.info("Redis connected: %s", url.split("@")[-1] if "@" in url else url[:40])
56
  return _instance
57
  except Exception as exc:
58
- log.error("Redis init failed: %s", exc)
 
59
  return None
60
 
61
 
 
29
  # ---------------------------------------------------------------------------
30
 
31
  _instance: Optional["RedisCache"] = None
32
+ _init_failed: bool = False
33
  _lock = threading.Lock()
34
 
35
 
36
  def get_redis() -> Optional["RedisCache"]:
37
  """Return the global RedisCache instance, or *None* if not configured."""
38
+ global _instance, _init_failed
39
 
40
  # Fast path (no lock)
41
  if _instance is not None:
42
  return _instance
43
+ if _init_failed:
44
+ return None
45
 
46
  url = os.environ.get("UPSTASH_REDIS_URL")
47
  token = os.environ.get("UPSTASH_REDIS_TOKEN")
 
53
  # Double-check after acquiring lock
54
  if _instance is not None:
55
  return _instance
56
+ if _init_failed:
57
+ return None
58
  try:
59
  _instance = RedisCache(url=url, token=token)
60
  log.info("Redis connected: %s", url.split("@")[-1] if "@" in url else url[:40])
61
  return _instance
62
  except Exception as exc:
63
+ _init_failed = True
64
+ log.error("Redis init failed (will not retry): %s", exc)
65
  return None
66
 
67