From 568dc452339512675c2ae556b861a6057b8a212f Mon Sep 17 00:00:00 2001 From: ecederstrand Date: Thu, 7 Mar 2024 11:59:06 +0100 Subject: [PATCH] fix: Allow setting max_connections from config when in autodiscover mode --- exchangelib/account.py | 10 ++++++++-- exchangelib/protocol.py | 9 +++++++++ tests/test_autodiscover.py | 24 +++++++++++++++--------- 3 files changed, 32 insertions(+), 11 deletions(-) diff --git a/exchangelib/account.py b/exchangelib/account.py index 966db5cb..402f5ea1 100644 --- a/exchangelib/account.py +++ b/exchangelib/account.py @@ -166,11 +166,16 @@ def __init__( raise InvalidTypeError("config", config, Configuration) if autodiscover: if config: - auth_type, retry_policy, version = config.auth_type, config.retry_policy, config.version + auth_type, retry_policy, version, max_connections = ( + config.auth_type, + config.retry_policy, + config.version, + config.max_connections, + ) if not credentials: credentials = config.credentials else: - auth_type, retry_policy, version = None, None, None + auth_type, retry_policy, version, max_connections = None, None, None, None self.ad_response, self.protocol = Autodiscovery( email=primary_smtp_address, credentials=credentials ).discover() @@ -180,6 +185,7 @@ def __init__( self.protocol.config.retry_policy = retry_policy if version: self.protocol.config.version = version + self.protocol.max_connections = max_connections primary_smtp_address = self.ad_response.autodiscover_smtp_address else: if not config: diff --git a/exchangelib/protocol.py b/exchangelib/protocol.py index 2b8dc52b..1d4d50c3 100644 --- a/exchangelib/protocol.py +++ b/exchangelib/protocol.py @@ -131,6 +131,15 @@ def credentials(self, value): self.config._credentials = value self.close() + @property + def max_connections(self): + return self._session_pool_maxsize + + @max_connections.setter + def max_connections(self, value): + with self._session_pool_lock: + self._session_pool_maxsize = value or self.SESSION_POOLSIZE + @property def retry_policy(self): return self.config.retry_policy diff --git a/tests/test_autodiscover.py b/tests/test_autodiscover.py index eb56f12b..2df07f05 100644 --- a/tests/test_autodiscover.py +++ b/tests/test_autodiscover.py @@ -847,16 +847,22 @@ def test_redirect_url_is_valid(self, m): self.assertTrue(a._redirect_url_is_valid(self.account.protocol.config.service_endpoint)) def test_protocol_default_values(self): - # Test that retry_policy and auth_type always get a value regardless of how we create an Account - self.get_account() - a = Account( - self.account.primary_smtp_address, - autodiscover=True, - config=self.account.protocol.config, - ) - self.assertIsNotNone(a.protocol.auth_type) - self.assertIsNotNone(a.protocol.retry_policy) + # Test that retry_policy, auth_type and max_connections always get values regardless of how we create an Account + _max_conn = self.account.protocol.config.max_connections + try: + self.account.protocol.config.max_connections = 3 + a = Account( + self.account.primary_smtp_address, + autodiscover=True, + config=self.account.protocol.config, + ) + self.assertIsNotNone(a.protocol.auth_type) + self.assertIsNotNone(a.protocol.retry_policy) + self.assertEqual(a.protocol._session_pool_maxsize, 3) + finally: + self.account.protocol.config.max_connections = _max_conn a = Account(self.account.primary_smtp_address, autodiscover=True, credentials=self.account.protocol.credentials) self.assertIsNotNone(a.protocol.auth_type) self.assertIsNotNone(a.protocol.retry_policy) + self.assertEqual(a.protocol._session_pool_maxsize, a.protocol.SESSION_POOLSIZE)