diff --git a/Changelog b/Changelog index fb542e80..ee3d385f 100644 --- a/Changelog +++ b/Changelog @@ -4,6 +4,17 @@ Change history ================ +.. _version-4.0.3: + +4.0.3 +===== +:release-date: TBA +:release-by: Ask Solem + +- MongoDB: Fixed problem with using readPreference option at pymongo 3.x. + + Contributed by **Mikhail Elovskikh**. + .. _version-4.0.2: 4.0.2 diff --git a/kombu/transport/mongodb.py b/kombu/transport/mongodb.py index 2a7f8ff3..75372ab0 100644 --- a/kombu/transport/mongodb.py +++ b/kombu/transport/mongodb.py @@ -274,18 +274,21 @@ class Channel(virtual.Channel): if self.connect_timeout else None), } options.update(parsed['options']) + options = self._prepare_client_options(options) return hostname, dbname, options def _prepare_client_options(self, options): if pymongo.version_tuple >= (3,): options.pop('auto_start_request', None) + if isinstance(options.get('readpreference'), int): + modes = pymongo.read_preferences._MONGOS_MODES + options['readpreference'] = modes[options['readpreference']] return options def _open(self, scheme='mongodb://'): - hostname, dbname, options = self._parse_uri(scheme=scheme) + hostname, dbname, conf = self._parse_uri(scheme=scheme) - conf = self._prepare_client_options(options) conf['host'] = hostname env = _detect_environment() diff --git a/t/unit/transport/test_mongodb.py b/t/unit/transport/test_mongodb.py index 99cc5531..fb5b0c0b 100644 --- a/t/unit/transport/test_mongodb.py +++ b/t/unit/transport/test_mongodb.py @@ -81,6 +81,12 @@ class test_mongodb_uri_parsing: assert hostname == 'mongodb://foo:bar@localhost/dbname' assert dbname == 'dbname' + def test_correct_readpreference(self): + url = 'mongodb://localhost/dbname?readpreference=nearest' + channel = _create_mock_connection(url).default_channel + hostname, dbname, options = channel._parse_uri() + assert options['readpreference'] == 'nearest' + class BaseMongoDBChannelCase: