Skip to content

Commit 7d8716d

Browse files
committed
Fix sentinel mode read-write separation
1 parent e11150a commit 7d8716d

File tree

1 file changed

+21
-6
lines changed

1 file changed

+21
-6
lines changed

django_redis/pool.py

Lines changed: 21 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
from typing import Dict
2-
from urllib.parse import parse_qs, urlparse
2+
from urllib.parse import parse_qs, urlparse,urlencode,urlunparse
33

44
from django.conf import settings
55
from django.core.exceptions import ImproperlyConfigured
@@ -163,15 +163,30 @@ def get_connection_pool(self, params):
163163
# explicitly set service_name and sentinel_manager for the
164164
# SentinelConnectionPool constructor since will be called by from_url
165165
cp_params = dict(params)
166-
cp_params.update(service_name=url.hostname, sentinel_manager=self._sentinel)
167-
pool = super().get_connection_pool(cp_params)
168-
169166
# convert "is_master" to a boolean if set on the URL, otherwise if not
170167
# provided it defaults to True.
171-
is_master = parse_qs(url.query).get("is_master")
168+
query_params = parse_qs(url.query)
169+
is_master = query_params.get("is_master")
172170
if is_master:
173-
pool.is_master = to_bool(is_master[0])
171+
cp_params['is_master'] = to_bool(is_master[0])
172+
# then remove the "is_master" query string from the URL
173+
# so it doesn't interfere with the SentinelConnectionPool constructor
174+
if 'is_master' in query_params:
175+
del query_params['is_master']
176+
new_query = urlencode(query_params, doseq=True)
177+
178+
new_url = urlunparse((
179+
url.scheme,
180+
url.netloc,
181+
url.path,
182+
url.params,
183+
new_query,
184+
url.fragment
185+
))
186+
187+
cp_params.update(service_name=url.hostname, sentinel_manager=self._sentinel,url=new_url)
174188

189+
pool = super().get_connection_pool(cp_params)
175190
return pool
176191

177192

0 commit comments

Comments
 (0)