hyb
2026-01-09 4cb426cb3ae31e772a09d4ade5b2f0242aaeefa0
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
from abc import abstractmethod
from typing import Optional, Union
 
from redis.asyncio import Redis, RedisCluster
from redis.data_structure import WeightedList
from redis.multidb.circuit import CircuitBreaker
from redis.multidb.database import AbstractDatabase, BaseDatabase
from redis.typing import Number
 
 
class AsyncDatabase(AbstractDatabase):
    """Database with an underlying asynchronous redis client."""
 
    @property
    @abstractmethod
    def client(self) -> Union[Redis, RedisCluster]:
        """The underlying redis client."""
        pass
 
    @client.setter
    @abstractmethod
    def client(self, client: Union[Redis, RedisCluster]):
        """Set the underlying redis client."""
        pass
 
    @property
    @abstractmethod
    def circuit(self) -> CircuitBreaker:
        """Circuit breaker for the current database."""
        pass
 
    @circuit.setter
    @abstractmethod
    def circuit(self, circuit: CircuitBreaker):
        """Set the circuit breaker for the current database."""
        pass
 
 
Databases = WeightedList[tuple[AsyncDatabase, Number]]
 
 
class Database(BaseDatabase, AsyncDatabase):
    def __init__(
        self,
        client: Union[Redis, RedisCluster],
        circuit: CircuitBreaker,
        weight: float,
        health_check_url: Optional[str] = None,
    ):
        self._client = client
        self._cb = circuit
        self._cb.database = self
        super().__init__(weight, health_check_url)
 
    @property
    def client(self) -> Union[Redis, RedisCluster]:
        return self._client
 
    @client.setter
    def client(self, client: Union[Redis, RedisCluster]):
        self._client = client
 
    @property
    def circuit(self) -> CircuitBreaker:
        return self._cb
 
    @circuit.setter
    def circuit(self, circuit: CircuitBreaker):
        self._cb = circuit