From c295f44d9c7ed5d53b7c327f9f2a06e09b1bb87a Mon Sep 17 00:00:00 2001 From: jiangweidong Date: Tue, 1 Nov 2022 10:59:04 +0800 Subject: [PATCH] =?UTF-8?q?perf:=20=E6=94=AF=E6=8C=81=E9=83=A8=E7=BD=B2?= =?UTF-8?q?=E5=9C=A8=E5=BC=80=E5=90=AF=E8=AE=A4=E8=AF=81=E7=9A=84Redis=20S?= =?UTF-8?q?entinel?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- apps/jumpserver/settings/base.py | 8 +++++++- apps/jumpserver/settings/libs.py | 20 +++++++++++++++----- requirements/requirements.txt | 2 +- utils/start_celery_beat.py | 10 ++++++++-- 4 files changed, 31 insertions(+), 9 deletions(-) diff --git a/apps/jumpserver/settings/base.py b/apps/jumpserver/settings/base.py index 7f9b88118..4b2d74a41 100644 --- a/apps/jumpserver/settings/base.py +++ b/apps/jumpserver/settings/base.py @@ -290,6 +290,8 @@ REDIS_PROTOCOL = 'rediss' if REDIS_USE_SSL else 'redis' # Cache use sentinel REDIS_SENTINELS_HOST = CONFIG.REDIS_SENTINELS_HOST REDIS_SENTINEL_SERVICE_NAME, REDIS_SENTINELS = parse_sentinels_host(REDIS_SENTINELS_HOST) +REDIS_SENTINEL_PASSWORD = CONFIG.REDIS_SENTINEL_PASSWORD +REDIS_SENTINEL_SOCKET_TIMEOUT = CONFIG.REDIS_SENTINEL_SOCKET_TIMEOUT # Cache config REDIS_OPTIONS = { @@ -310,7 +312,11 @@ if REDIS_SENTINEL_SERVICE_NAME and REDIS_SENTINELS: } REDIS_OPTIONS.update({ 'CLIENT_CLASS': 'django_redis.client.SentinelClient', - 'SENTINELS': REDIS_SENTINELS, 'PASSWORD': CONFIG.REDIS_PASSWORD + 'SENTINELS': REDIS_SENTINELS, 'PASSWORD': CONFIG.REDIS_PASSWORD, + 'SENTINEL_KWARGS': { + 'password': REDIS_SENTINEL_PASSWORD, + 'socket_timeout': REDIS_SENTINEL_SOCKET_TIMEOUT + } }) DJANGO_REDIS_CONNECTION_FACTORY = 'django_redis.pool.SentinelConnectionFactory' else: diff --git a/apps/jumpserver/settings/libs.py b/apps/jumpserver/settings/libs.py index bccd9ebb9..20beb5478 100644 --- a/apps/jumpserver/settings/libs.py +++ b/apps/jumpserver/settings/libs.py @@ -4,9 +4,9 @@ import os import ssl from .base import ( - REDIS_SSL_CA, REDIS_SSL_CERT, REDIS_SSL_KEY, - REDIS_SSL_REQUIRED, REDIS_USE_SSL, - REDIS_SENTINEL_SERVICE_NAME, REDIS_SENTINELS, + REDIS_SSL_CA, REDIS_SSL_CERT, REDIS_SSL_KEY, REDIS_SSL_REQUIRED, REDIS_USE_SSL, + REDIS_SENTINEL_SERVICE_NAME, REDIS_SENTINELS, REDIS_SENTINEL_PASSWORD, + REDIS_SENTINEL_SOCKET_TIMEOUT ) from ..const import CONFIG, PROJECT_DIR @@ -100,6 +100,10 @@ REDIS_HOST = { if REDIS_SENTINEL_SERVICE_NAME and REDIS_SENTINELS: REDIS_HOST['sentinels'] = REDIS_SENTINELS REDIS_HOST['master_name'] = REDIS_SENTINEL_SERVICE_NAME + REDIS_HOST['sentinel_kwargs'] = { + 'password': REDIS_SENTINEL_PASSWORD, + 'socket_timeout': REDIS_SENTINEL_SOCKET_TIMEOUT + } else: REDIS_HOST['address'] = (CONFIG.REDIS_HOST, CONFIG.REDIS_PORT) @@ -124,8 +128,14 @@ if REDIS_SENTINEL_SERVICE_NAME and REDIS_SENTINELS: 'protocol': 'sentinel', 'password': CONFIG.REDIS_PASSWORD, 'host': item[0], 'port': item[1], 'db': CONFIG.REDIS_DB_CELERY } for item in REDIS_SENTINELS]) - CELERY_BROKER_TRANSPORT_OPTIONS = {"master_name": REDIS_SENTINEL_SERVICE_NAME} - CELERY_RESULT_BACKEND_TRANSPORT_OPTIONS = {"master_name": REDIS_SENTINEL_SERVICE_NAME} + SENTINEL_OPTIONS = { + 'master_name': REDIS_SENTINEL_SERVICE_NAME, + 'sentinel_kwargs': { + 'password': REDIS_SENTINEL_PASSWORD, + 'socket_timeout': REDIS_SENTINEL_SOCKET_TIMEOUT + } + } + CELERY_BROKER_TRANSPORT_OPTIONS = CELERY_RESULT_BACKEND_TRANSPORT_OPTIONS = SENTINEL_OPTIONS else: CELERY_BROKER_URL = CELERY_BROKER_URL_FORMAT % { 'protocol': 'rediss' if REDIS_USE_SSL else 'redis', diff --git a/requirements/requirements.txt b/requirements/requirements.txt index 5e94c69e5..a9a631c33 100644 --- a/requirements/requirements.txt +++ b/requirements/requirements.txt @@ -83,7 +83,7 @@ Pillow==9.1.1 pytz==2022.1 # Runtime django-proxy==1.2.1 -channels-redis==3.4.0 +channels-redis==4.0.0 channels==3.0.4 daphne==3.0.2 python-daemon==2.3.0 diff --git a/utils/start_celery_beat.py b/utils/start_celery_beat.py index eabc0812b..247906f5c 100644 --- a/utils/start_celery_beat.py +++ b/utils/start_celery_beat.py @@ -33,15 +33,21 @@ if settings.REDIS_USE_SSL: REDIS_SENTINEL_SERVICE_NAME = settings.REDIS_SENTINEL_SERVICE_NAME REDIS_SENTINELS = settings.REDIS_SENTINELS +REDIS_SENTINEL_PASSWORD = settings.REDIS_SENTINEL_PASSWORD +REDIS_SENTINEL_SOCKET_TIMEOUT = settings.REDIS_SENTINEL_SOCKET_TIMEOUT if REDIS_SENTINEL_SERVICE_NAME and REDIS_SENTINELS: connection_params['sentinels'] = REDIS_SENTINELS - sentinel_client = Sentinel(**connection_params) + sentinel_client = Sentinel( + **connection_params, sentinel_kwargs={ + 'password': REDIS_SENTINEL_PASSWORD, + 'socket_timeout': REDIS_SENTINEL_SOCKET_TIMEOUT + } + ) redis_client = sentinel_client.master_for(REDIS_SENTINEL_SERVICE_NAME) else: connection_params['host'] = settings.REDIS_HOST connection_params['port'] = settings.REDIS_PORT redis_client = Redis(**connection_params) -print("Connection params: ", connection_params) scheduler = "django_celery_beat.schedulers:DatabaseScheduler" processes = []