Skip to content

[Core] Use MSAL HTTP cache - #20234

Merged
Jiashuo Li (jiasli) merged 2 commits into
Azure:devfrom
jiasli:http-cache
Nov 16, 2021
Merged

Jiashuo Li (jiasli) merged 2 commits into
Azure:devfrom
jiasli:http-cache

Conversation

@jiasli

Copy link
Copy Markdown
Contributor

Description

Incorporate MSAL HTTP cache introduced by AzureAD/microsoft-authentication-library-for-python#407, so that HTTP responses can be cached for

This significantly improves MSAL performance in both normal "get access token" calls and AAD server outages..

@yonzhan

Copy link
Copy Markdown
Collaborator

Core

Comment on lines +106 to 118
def _load_msal_http_cache(self):
import atexit
import pickle

logger.debug("_load_msal_http_cache: %s", self._http_cache_file)
try:
with open(self._http_cache_file, 'rb') as f:
persisted_http_cache = pickle.load(f) # Take a snapshot
except: # pylint: disable=bare-except
persisted_http_cache = pickle.load(f)
except (pickle.UnpicklingError, FileNotFoundError) as ex:
logger.debug("Failed to load MSAL HTTP cache: %s", ex)
persisted_http_cache = {} # Ignore a non-exist or corrupted http_cache
atexit.register(lambda: pickle.dump(
# When exit, flush it back to the file.

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes it is effectively the same as MSAL's recommended pattern. Ship it!

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Well, I am a little bit confused. Haven't we decided not to catch EOFError, AttributeError, ImportError, IndexError (AzureAD/microsoft-authentication-library-for-python#407 (comment))? They still appear in https://msal-python.readthedocs.io/en/latest/#publicclientapplication

image

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You are right. I created a new PR in MSAL to remove them. AzureAD/microsoft-authentication-library-for-python#439

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

6 participants