Skip to content

Commit

Permalink
fix: Allow setting max_connections from config when in autodiscover mode
Browse files Browse the repository at this point in the history
  • Loading branch information
ecederstrand committed Mar 7, 2024
1 parent 7401f51 commit 568dc45
Show file tree
Hide file tree
Showing 3 changed files with 32 additions and 11 deletions.
10 changes: 8 additions & 2 deletions exchangelib/account.py
Original file line number Diff line number Diff line change
Expand Up @@ -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()
Expand All @@ -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:
Expand Down
9 changes: 9 additions & 0 deletions exchangelib/protocol.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
24 changes: 15 additions & 9 deletions tests/test_autodiscover.py
Original file line number Diff line number Diff line change
Expand Up @@ -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)

0 comments on commit 568dc45

Please sign in to comment.