Skip to content

Commit a6f0081

Browse files
committed
feat: enhance Redis Sentinel configuration to support dynamic sentinel settings
1 parent e5d2630 commit a6f0081

File tree

2 files changed

+23
-6
lines changed

2 files changed

+23
-6
lines changed

apps/maxkb/conf.py

Lines changed: 20 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -78,12 +78,26 @@ def get_cache_setting(self):
7878
},
7979
}
8080
if self.get('REDIS_SENTINEL_SENTINELS') is not None:
81-
del redis_config['default']['LOCATION']
82-
redis_config['default']['OPTIONS']['SENTINEL_SENTINELS'] = self.get('REDIS_SENTINEL_SENTINELS').split(',')
83-
redis_config['default']['OPTIONS']['SENTINEL_MASTER_SET'] = self.get('REDIS_SENTINEL_MASTER_SET')
84-
return redis_config
85-
else:
86-
return redis_config
81+
sentinels_str = self.get('REDIS_SENTINEL_SENTINELS')
82+
sentinels = [
83+
(host.strip(), int(port))
84+
for hostport in sentinels_str.split(',')
85+
for host, port in [hostport.strip().split(':')]
86+
]
87+
88+
redis_config['default']['LOCATION'] = f'redis://{self.get("REDIS_SENTINEL_MASTER")}/{self.get("REDIS_DB")}'
89+
redis_config['default']['OPTIONS'].update({
90+
'CLIENT_CLASS': 'django_redis.client.SentinelClient',
91+
'SENTINELS': sentinels,
92+
'SENTINEL_MASTER': self.get('REDIS_SENTINEL_MASTER'),
93+
'PASSWORD': self.get("REDIS_PASSWORD"),
94+
'SOCKET_TIMEOUT': 1,
95+
})
96+
97+
# 必须移除和 Sentinel 不兼容的项
98+
redis_config['default']['OPTIONS'].pop('CONNECTION_POOL_KWARGS', None)
99+
100+
return redis_config
87101

88102
def get_language_code(self):
89103
return self.get('LANGUAGE_CODE', 'zh-CN')

apps/maxkb/settings/base.py

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -172,3 +172,6 @@
172172
DEFAULT_AUTO_FIELD = 'django.db.models.BigAutoField'
173173

174174
edition = 'CE'
175+
176+
if os.environ.get('MAXKB_REDIS_SENTINEL_SENTINELS') is not None:
177+
DJANGO_REDIS_CONNECTION_FACTORY = "django_redis.pool.SentinelConnectionFactory"

0 commit comments

Comments
 (0)