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 <[email protected]>
This commit is contained in:
co-authored by
Cursor
parent
fef9552cbf
commit
5149299c1b
@@ -2,9 +2,10 @@
|
||||
|
||||
from __future__ import annotations
|
||||
import os
|
||||
import structlog
|
||||
import time
|
||||
from io import BytesIO
|
||||
from typing import Self
|
||||
import structlog
|
||||
from python_utils import check_env
|
||||
|
||||
from python_repositories.interfaces import (
|
||||
@@ -34,6 +35,7 @@ class MinioAdapter(
|
||||
secret_key_env_var_name: str = "MINIO_SECRET_KEY"
|
||||
bucket_env_var_name: str = "MINIO_BUCKET"
|
||||
chunk_size: int = 5 * 2**20 # 5 MiB
|
||||
health_check_ttl_seconds: float = 1.0
|
||||
|
||||
def __init__(self) -> None:
|
||||
# Setup logger
|
||||
@@ -52,6 +54,8 @@ class MinioAdapter(
|
||||
# Prepare internal variables
|
||||
self._client: minio.Minio | None = None
|
||||
self._bucket_name: str | None = None
|
||||
self._health_check_at: float | None = None
|
||||
self._health_check_ok: bool = False
|
||||
|
||||
def __enter__(self) -> Self:
|
||||
"""Enter the context."""
|
||||
@@ -75,10 +79,11 @@ class MinioAdapter(
|
||||
|
||||
def connect(self) -> None:
|
||||
"""Connect to the Minio server."""
|
||||
# Stop if already connected
|
||||
if self.is_connected:
|
||||
if self._client is not None and self.is_connected():
|
||||
self.logger.info("Already connected to Minio")
|
||||
return
|
||||
if self._client is not None:
|
||||
self.disconnect()
|
||||
# Prepare arguments
|
||||
endpoint = str(os.getenv(self.endpoint_env_var_name))
|
||||
access_key = str(os.getenv(self.access_key_env_var_name))
|
||||
@@ -103,6 +108,7 @@ class MinioAdapter(
|
||||
# Persist information
|
||||
self._client = client
|
||||
self._bucket_name = bucket
|
||||
self._invalidate_health_cache()
|
||||
|
||||
def disconnect(self) -> None:
|
||||
"""Disconnect from the Minio server."""
|
||||
@@ -111,13 +117,36 @@ class MinioAdapter(
|
||||
# Reset client
|
||||
self._client = None
|
||||
self._bucket_name = 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 and self._bucket_name is not None
|
||||
try:
|
||||
return bool(self._client.bucket_exists(self._bucket_name))
|
||||
except Exception: # pylint: disable=broad-except
|
||||
return False
|
||||
|
||||
@property
|
||||
def is_connected(self) -> bool:
|
||||
"""Check if connected to Minio server."""
|
||||
res = self._client is not None
|
||||
self.logger.debug(res)
|
||||
return res
|
||||
"""Check if connected to the Minio server."""
|
||||
if self._client is None or self._bucket_name 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 put(
|
||||
self,
|
||||
@@ -134,8 +163,9 @@ class MinioAdapter(
|
||||
if not isinstance(content_type, str) or len(content_type) == 0:
|
||||
raise ValueError("content_type must be a non-empty string")
|
||||
# Check connection
|
||||
if self._client is None or self._bucket_name is None or not self.is_connected:
|
||||
if not self.is_connected():
|
||||
raise ConnectionError("Not connected to Minio")
|
||||
assert self._client is not None and self._bucket_name is not None
|
||||
# Prepare buffer for reading
|
||||
num_bytes = data.getbuffer().nbytes
|
||||
data.seek(0)
|
||||
@@ -159,8 +189,9 @@ class MinioAdapter(
|
||||
if not isinstance(object_name, str) or len(object_name) == 0:
|
||||
raise ValueError("object_name must be a non-empty string")
|
||||
# Check connection
|
||||
if self._client is None or self._bucket_name is None or not self.is_connected:
|
||||
if not self.is_connected():
|
||||
raise ConnectionError("Not connected to Minio")
|
||||
assert self._client is not None and self._bucket_name is not None
|
||||
# Get data from bucket
|
||||
# N.B. bucket name is set when connecting
|
||||
try:
|
||||
@@ -194,8 +225,9 @@ class MinioAdapter(
|
||||
if not isinstance(object_name, str) or len(object_name) == 0:
|
||||
raise ValueError("object_name must be a non-empty string")
|
||||
# Check connection
|
||||
if self._client is None or self._bucket_name is None or not self.is_connected:
|
||||
if not self.is_connected():
|
||||
raise ConnectionError("Not connected to Minio")
|
||||
assert self._client is not None and self._bucket_name is not None
|
||||
# Delete object from bucket
|
||||
# N.B. bucket name is set when connecting
|
||||
self._client.remove_object(
|
||||
@@ -213,8 +245,9 @@ class MinioAdapter(
|
||||
raise ValueError("prefix must be a string")
|
||||
# Check connection
|
||||
# N.B. bucket name is set when connecting
|
||||
if self._client is None or self._bucket_name is None or not self.is_connected:
|
||||
if not self.is_connected():
|
||||
raise ConnectionError("Not connected to Minio")
|
||||
assert self._client is not None and self._bucket_name is not None
|
||||
# List objects in bucket
|
||||
objects = self._client.list_objects(
|
||||
bucket_name=self._bucket_name,
|
||||
|
||||
@@ -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