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,
|
||||
|
||||
Reference in New Issue
Block a user