Strengthen is_connected with cached health probes.
Convert is_connected to a method that verifies backend liveness via TTL-cached ping (Redis) or bucket_exists (MinIO), with cache invalidation on connect/disconnect. Co-authored-by: Cursor <cursoragent@cursor.com>
This commit is contained in:
co-authored by
Cursor
parent
fef9552cbf
commit
5149299c1b
@@ -4,6 +4,7 @@ from __future__ import annotations
|
||||
from typing import Self, cast
|
||||
import os
|
||||
import structlog
|
||||
import time
|
||||
|
||||
from python_utils import check_env
|
||||
|
||||
@@ -33,6 +34,7 @@ class RedisAdapter(
|
||||
uri_env_var_name: str = "REDIS_URI"
|
||||
path: str = "." # JSON root path, updated in __init__
|
||||
encoding: str = "UTF-8"
|
||||
health_check_ttl_seconds: float = 1.0
|
||||
|
||||
def __init__(self) -> None:
|
||||
# Setup logger
|
||||
@@ -44,6 +46,8 @@ class RedisAdapter(
|
||||
# Prepare internal variables
|
||||
self._client: redis.Redis | None = None
|
||||
self.path: str = RedisPath.root_path()
|
||||
self._health_check_at: float | None = None
|
||||
self._health_check_ok: bool = False
|
||||
|
||||
def __enter__(self) -> Self:
|
||||
"""Enter the context."""
|
||||
@@ -67,6 +71,10 @@ class RedisAdapter(
|
||||
|
||||
def connect(self) -> None:
|
||||
"""Connect to the Redis server."""
|
||||
if self._client is not None:
|
||||
self._client.close()
|
||||
self._client = None
|
||||
self._invalidate_health_cache()
|
||||
# Prepare arguments
|
||||
uri = str(os.getenv(self.uri_env_var_name))
|
||||
# Connect client
|
||||
@@ -81,6 +89,7 @@ class RedisAdapter(
|
||||
raise ConnectionError(f"Could not connect to Redis at {uri}") from exc
|
||||
# Persist client
|
||||
self._client = client
|
||||
self._invalidate_health_cache()
|
||||
|
||||
def disconnect(self) -> None:
|
||||
"""Disconnect from the Redis server."""
|
||||
@@ -89,13 +98,36 @@ class RedisAdapter(
|
||||
self._client.close()
|
||||
# Reset client
|
||||
self._client = None
|
||||
self._invalidate_health_cache()
|
||||
|
||||
def _invalidate_health_cache(self) -> None:
|
||||
self._health_check_at = None
|
||||
self._health_check_ok = False
|
||||
|
||||
def _probe_connection(self) -> bool:
|
||||
assert self._client is not None
|
||||
try:
|
||||
return bool(self._client.ping())
|
||||
except (redis.ConnectionError, redis.TimeoutError):
|
||||
return False
|
||||
|
||||
@property
|
||||
def is_connected(self) -> bool:
|
||||
"""Check if connected to Redis server."""
|
||||
res = self._client is not None
|
||||
self.logger.debug(res)
|
||||
return res
|
||||
"""Check if connected to the Redis server."""
|
||||
if self._client is None:
|
||||
return False
|
||||
now = time.monotonic()
|
||||
if self._health_check_at is not None:
|
||||
seconds_since_last_health_check = now - self._health_check_at
|
||||
cache_is_fresh = (
|
||||
seconds_since_last_health_check < self.health_check_ttl_seconds
|
||||
)
|
||||
if cache_is_fresh:
|
||||
return self._health_check_ok
|
||||
result = self._probe_connection()
|
||||
self._health_check_at = now
|
||||
self._health_check_ok = result
|
||||
self.logger.debug("Connection status", connected=result)
|
||||
return result
|
||||
|
||||
def set(self, key: str, data: dict) -> None:
|
||||
"""Set a JSON object in Redis."""
|
||||
@@ -105,8 +137,9 @@ class RedisAdapter(
|
||||
if not isinstance(data, dict) or len(data) == 0:
|
||||
raise ValueError("Data must be a non-empty dictionary")
|
||||
# Check connection
|
||||
if self._client is None or not self.is_connected:
|
||||
if not self.is_connected():
|
||||
raise ConnectionError("Not connected to Redis")
|
||||
assert self._client is not None
|
||||
# Set data
|
||||
self._client.json().set(key, self.path, data)
|
||||
self.logger.debug(f"Set {key} to {data}")
|
||||
@@ -117,8 +150,9 @@ class RedisAdapter(
|
||||
if not isinstance(key, str) or len(key) == 0:
|
||||
raise ValueError("Key must be a non-empty string")
|
||||
# Check connection
|
||||
if self._client is None or not self.is_connected:
|
||||
if not self.is_connected():
|
||||
raise ConnectionError("Not connected to Redis")
|
||||
assert self._client is not None
|
||||
# Get data
|
||||
data = cast(
|
||||
dict | None,
|
||||
@@ -133,8 +167,9 @@ class RedisAdapter(
|
||||
if not isinstance(key, str) or len(key) == 0:
|
||||
raise ValueError("Key must be a non-empty string")
|
||||
# Check connection
|
||||
if self._client is None or not self.is_connected:
|
||||
if not self.is_connected():
|
||||
raise ConnectionError("Not connected to Redis")
|
||||
assert self._client is not None
|
||||
# Delete data
|
||||
self._client.json().delete(key)
|
||||
self.logger.debug(f"Deleted {key}")
|
||||
@@ -145,8 +180,9 @@ class RedisAdapter(
|
||||
if not isinstance(pattern, str) or len(pattern) == 0:
|
||||
raise ValueError("Pattern must be a non-empty string")
|
||||
# Check connection
|
||||
if self._client is None or not self.is_connected:
|
||||
if not self.is_connected():
|
||||
raise ConnectionError("Not connected to Redis")
|
||||
assert self._client is not None
|
||||
# List keys
|
||||
keys_raw = cast(
|
||||
list[bytes],
|
||||
|
||||
Reference in New Issue
Block a user