mirror of
https://github.com/BerriAI/litellm.git
synced 2025-04-25 18:54:30 +00:00
(fix proxy redis) Add redis sentinel support (#6154)
* add sentinel_password support * add doc for setting redis sentinel password * fix redis sentinel - use sentinel password
This commit is contained in:
parent
86607a2018
commit
d136641954
2 changed files with 17 additions and 3 deletions
|
@ -136,6 +136,7 @@ litellm_settings:
|
||||||
type: "redis"
|
type: "redis"
|
||||||
service_name: "mymaster"
|
service_name: "mymaster"
|
||||||
sentinel_nodes: [["localhost", 26379]]
|
sentinel_nodes: [["localhost", 26379]]
|
||||||
|
sentinel_password: "password" # [OPTIONAL]
|
||||||
```
|
```
|
||||||
|
|
||||||
</TabItem>
|
</TabItem>
|
||||||
|
@ -149,6 +150,7 @@ You can configure redis sentinel in your .env by setting `REDIS_SENTINEL_NODES`
|
||||||
```env
|
```env
|
||||||
REDIS_SENTINEL_NODES='[["localhost", 26379]]'
|
REDIS_SENTINEL_NODES='[["localhost", 26379]]'
|
||||||
REDIS_SERVICE_NAME = "mymaster"
|
REDIS_SERVICE_NAME = "mymaster"
|
||||||
|
REDIS_SENTINEL_PASSWORD = "password"
|
||||||
```
|
```
|
||||||
|
|
||||||
:::note
|
:::note
|
||||||
|
|
|
@ -12,13 +12,13 @@ import json
|
||||||
|
|
||||||
# s/o [@Frank Colson](https://www.linkedin.com/in/frank-colson-422b9b183/) for this redis implementation
|
# s/o [@Frank Colson](https://www.linkedin.com/in/frank-colson-422b9b183/) for this redis implementation
|
||||||
import os
|
import os
|
||||||
from typing import List, Optional, Union
|
from typing import Dict, List, Optional, Union
|
||||||
|
|
||||||
import redis # type: ignore
|
import redis # type: ignore
|
||||||
import redis.asyncio as async_redis # type: ignore
|
import redis.asyncio as async_redis # type: ignore
|
||||||
|
|
||||||
import litellm
|
import litellm
|
||||||
from litellm import get_secret
|
from litellm import get_secret, get_secret_str
|
||||||
|
|
||||||
from ._logging import verbose_logger
|
from ._logging import verbose_logger
|
||||||
|
|
||||||
|
@ -141,6 +141,13 @@ def _get_redis_client_logic(**env_overrides):
|
||||||
if _sentinel_nodes is not None and isinstance(_sentinel_nodes, str):
|
if _sentinel_nodes is not None and isinstance(_sentinel_nodes, str):
|
||||||
redis_kwargs["sentinel_nodes"] = json.loads(_sentinel_nodes)
|
redis_kwargs["sentinel_nodes"] = json.loads(_sentinel_nodes)
|
||||||
|
|
||||||
|
_sentinel_password: Optional[str] = redis_kwargs.get(
|
||||||
|
"sentinel_password", None
|
||||||
|
) or get_secret_str("REDIS_SENTINEL_PASSWORD")
|
||||||
|
|
||||||
|
if _sentinel_password is not None:
|
||||||
|
redis_kwargs["sentinel_password"] = _sentinel_password
|
||||||
|
|
||||||
_service_name: Optional[str] = redis_kwargs.get("service_name", None) or get_secret( # type: ignore
|
_service_name: Optional[str] = redis_kwargs.get("service_name", None) or get_secret( # type: ignore
|
||||||
"REDIS_SERVICE_NAME"
|
"REDIS_SERVICE_NAME"
|
||||||
)
|
)
|
||||||
|
@ -217,6 +224,7 @@ def _init_redis_sentinel(redis_kwargs) -> redis.Redis:
|
||||||
|
|
||||||
def _init_async_redis_sentinel(redis_kwargs) -> async_redis.Redis:
|
def _init_async_redis_sentinel(redis_kwargs) -> async_redis.Redis:
|
||||||
sentinel_nodes = redis_kwargs.get("sentinel_nodes")
|
sentinel_nodes = redis_kwargs.get("sentinel_nodes")
|
||||||
|
sentinel_password = redis_kwargs.get("sentinel_password")
|
||||||
service_name = redis_kwargs.get("service_name")
|
service_name = redis_kwargs.get("service_name")
|
||||||
|
|
||||||
if not sentinel_nodes or not service_name:
|
if not sentinel_nodes or not service_name:
|
||||||
|
@ -227,7 +235,11 @@ def _init_async_redis_sentinel(redis_kwargs) -> async_redis.Redis:
|
||||||
verbose_logger.debug("init_redis_sentinel: sentinel nodes are being initialized.")
|
verbose_logger.debug("init_redis_sentinel: sentinel nodes are being initialized.")
|
||||||
|
|
||||||
# Set up the Sentinel client
|
# Set up the Sentinel client
|
||||||
sentinel = async_redis.Sentinel(sentinel_nodes, socket_timeout=0.1)
|
sentinel = async_redis.Sentinel(
|
||||||
|
sentinel_nodes,
|
||||||
|
socket_timeout=0.1,
|
||||||
|
password=sentinel_password,
|
||||||
|
)
|
||||||
|
|
||||||
# Return the master instance for the given service
|
# Return the master instance for the given service
|
||||||
|
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue