|
@@ -1,6 +1,7 @@
|
|
|
from pydantic_settings import BaseSettings, SettingsConfigDict
|
|
from pydantic_settings import BaseSettings, SettingsConfigDict
|
|
|
from typing import Any, Dict, List
|
|
from typing import Any, Dict, List
|
|
|
from dotenv import load_dotenv
|
|
from dotenv import load_dotenv
|
|
|
|
|
+from urllib.parse import quote
|
|
|
import os
|
|
import os
|
|
|
load_dotenv()
|
|
load_dotenv()
|
|
|
|
|
|
|
@@ -70,9 +71,28 @@ class Settings(BaseSettings):
|
|
|
|
|
|
|
|
@property
|
|
@property
|
|
|
def REDIS_SENTINEL_URL(self) -> str:
|
|
def REDIS_SENTINEL_URL(self) -> str:
|
|
|
- # Celery sentinel transport 需要 "sentinel://host:port;sentinel://host:port"
|
|
|
|
|
|
|
+ # 标准 Sentinel URL:
|
|
|
|
|
+ # redis+sentinel://[username[:password]@]host1:port1,host2:port2/db?sentinel_master=mymaster
|
|
|
|
|
+ if self.REDIS_SENTINEL_USERNAME:
|
|
|
|
|
+ user = quote(self.REDIS_SENTINEL_USERNAME, safe="")
|
|
|
|
|
+ pwd = quote(self.REDIS_PASSWORD, safe="")
|
|
|
|
|
+ auth = f"{user}:{pwd}@"
|
|
|
|
|
+ elif self.REDIS_PASSWORD:
|
|
|
|
|
+ pwd = quote(self.REDIS_PASSWORD, safe="")
|
|
|
|
|
+ auth = f":{pwd}@"
|
|
|
|
|
+ else:
|
|
|
|
|
+ auth = ""
|
|
|
|
|
+ hosts = ",".join(f"{host}:{port}" for host, port in self.REDIS_SENTINEL_NODES)
|
|
|
|
|
+ master = quote(self.REDIS_MASTER_NAME, safe="")
|
|
|
|
|
+ return f"redis+sentinel://{auth}{hosts}/{self.REDIS_DB}?sentinel_master={master}"
|
|
|
|
|
+
|
|
|
|
|
+ @property
|
|
|
|
|
+ def REDIS_CELERY_SENTINEL_URL(self) -> str:
|
|
|
|
|
+ # Celery/Kombu 兼容的 Sentinel URL:
|
|
|
|
|
+ # sentinel://host:port/db;sentinel://host:port/db
|
|
|
return ";".join(
|
|
return ";".join(
|
|
|
- f"sentinel://{host}:{port}" for host, port in self.REDIS_SENTINEL_NODES
|
|
|
|
|
|
|
+ f"sentinel://{host}:{port}/{self.REDIS_DB}"
|
|
|
|
|
+ for host, port in self.REDIS_SENTINEL_NODES
|
|
|
)
|
|
)
|
|
|
|
|
|
|
|
@property
|
|
@property
|