|
1 | 1 | from typing import Dict
|
2 |
| -from urllib.parse import parse_qs, urlparse |
| 2 | +from urllib.parse import parse_qs, urlencode, urlparse, urlunparse |
3 | 3 |
|
4 | 4 | from django.conf import settings
|
5 | 5 | from django.core.exceptions import ImproperlyConfigured
|
@@ -163,16 +163,27 @@ def get_connection_pool(self, params):
|
163 | 163 | # explicitly set service_name and sentinel_manager for the
|
164 | 164 | # SentinelConnectionPool constructor since will be called by from_url
|
165 | 165 | 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 |
| - |
169 | 166 | # convert "is_master" to a boolean if set on the URL, otherwise if not
|
170 | 167 | # 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") |
172 | 170 | 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, url.netloc, url.path, url.params, new_query, url.fragment) |
| 180 | + ) |
174 | 181 |
|
175 |
| - return pool |
| 182 | + cp_params.update( |
| 183 | + service_name=url.hostname, sentinel_manager=self._sentinel, url=new_url |
| 184 | + ) |
| 185 | + |
| 186 | + return super().get_connection_pool(cp_params) |
176 | 187 |
|
177 | 188 |
|
178 | 189 | def get_connection_factory(path=None, options=None):
|
|
0 commit comments