Commit 2398ffd
FIX: Credential instance cache (#483)
### Work Item / Issue Reference
<!--
IMPORTANT: Please follow the PR template guidelines below.
For mssql-python maintainers: Insert your ADO Work Item ID below
For external contributors: Insert Github Issue number below
Only one reference is required - either GitHub issue OR ADO Work Item.
-->
<!-- mssql-python maintainers: ADO Work Item -->
>
[AB#43254](https://sqlclientdrivers.visualstudio.com/c6d89619-62de-46a0-8b46-70b92a84d85e/_workitems/edit/43254)
<!-- External contributors: GitHub Issue -->
> GitHub Issue: #388
-------------------------------------------------------------------
### Summary
This pull request introduces credential instance caching for Azure
Active Directory (AAD) authentication in the `mssql_python` package,
improving performance by reusing credential objects instead of creating
new ones for each token request. It also adds comprehensive tests for
the caching logic and edge cases, and includes a new benchmark script to
measure the performance impact of credential caching.
**Credential instance caching and logic changes:**
* Added a module-level credential cache (`_credential_cache`) and lock
in `auth.py` to reuse Azure Identity credential instances per
authentication type, enabling the SDK's in-memory token cache and
reducing redundant token acquisitions. (`mssql_python/auth.py`)
* Modified `AADAuth.get_raw_token` to use the cached credential instance
rather than creating a new one each call, ensuring efficient token
reuse. (`mssql_python/auth.py` )
**Testing improvements:**
* Added a pytest fixture to clear the credential cache between tests,
ensuring test isolation. (`tests/test_008_auth.py` )
* Introduced a new `TestCredentialInstanceCache` class with tests
verifying credential reuse, separation by auth type, and cache state.
(`tests/test_008_auth.py`)
* Added tests for error handling and edge cases, including missing Azure
libraries, authentication errors, and unusual connection string
parameter cases. (`tests/test_008_auth.py` )
**Benchmarking:**
* Added a new benchmark script (`benchmarks/bench_credential_cache.py`)
to measure and compare the performance of credential caching versus the
previous behavior of creating new credentials for every token request.
(`benchmarks/bench_credential_cache.py`)
---------
Co-authored-by: gargsaumya <saumyagarg.100@gmail.com>1 parent 57ed673 commit 2398ffd
File tree
3 files changed
+479
-6
lines changed- benchmarks
- mssql_python
- tests
3 files changed
+479
-6
lines changed| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
| 1 | + | |
| 2 | + | |
| 3 | + | |
| 4 | + | |
| 5 | + | |
| 6 | + | |
| 7 | + | |
| 8 | + | |
| 9 | + | |
| 10 | + | |
| 11 | + | |
| 12 | + | |
| 13 | + | |
| 14 | + | |
| 15 | + | |
| 16 | + | |
| 17 | + | |
| 18 | + | |
| 19 | + | |
| 20 | + | |
| 21 | + | |
| 22 | + | |
| 23 | + | |
| 24 | + | |
| 25 | + | |
| 26 | + | |
| 27 | + | |
| 28 | + | |
| 29 | + | |
| 30 | + | |
| 31 | + | |
| 32 | + | |
| 33 | + | |
| 34 | + | |
| 35 | + | |
| 36 | + | |
| 37 | + | |
| 38 | + | |
| 39 | + | |
| 40 | + | |
| 41 | + | |
| 42 | + | |
| 43 | + | |
| 44 | + | |
| 45 | + | |
| 46 | + | |
| 47 | + | |
| 48 | + | |
| 49 | + | |
| 50 | + | |
| 51 | + | |
| 52 | + | |
| 53 | + | |
| 54 | + | |
| 55 | + | |
| 56 | + | |
| 57 | + | |
| 58 | + | |
| 59 | + | |
| 60 | + | |
| 61 | + | |
| 62 | + | |
| 63 | + | |
| 64 | + | |
| 65 | + | |
| 66 | + | |
| 67 | + | |
| 68 | + | |
| 69 | + | |
| 70 | + | |
| 71 | + | |
| 72 | + | |
| 73 | + | |
| 74 | + | |
| 75 | + | |
| 76 | + | |
| 77 | + | |
| 78 | + | |
| 79 | + | |
| 80 | + | |
| 81 | + | |
| 82 | + | |
| 83 | + | |
| 84 | + | |
| 85 | + | |
| 86 | + | |
| 87 | + | |
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
6 | 6 | | |
7 | 7 | | |
8 | 8 | | |
| 9 | + | |
9 | 10 | | |
10 | 11 | | |
11 | 12 | | |
12 | 13 | | |
13 | 14 | | |
| 15 | + | |
| 16 | + | |
| 17 | + | |
| 18 | + | |
| 19 | + | |
| 20 | + | |
| 21 | + | |
14 | 22 | | |
15 | 23 | | |
16 | 24 | | |
| |||
36 | 44 | | |
37 | 45 | | |
38 | 46 | | |
39 | | - | |
| 47 | + | |
40 | 48 | | |
41 | | - | |
42 | | - | |
43 | | - | |
44 | | - | |
| 49 | + | |
| 50 | + | |
| 51 | + | |
45 | 52 | | |
46 | 53 | | |
47 | 54 | | |
| |||
83 | 90 | | |
84 | 91 | | |
85 | 92 | | |
86 | | - | |
| 93 | + | |
| 94 | + | |
| 95 | + | |
| 96 | + | |
| 97 | + | |
| 98 | + | |
| 99 | + | |
| 100 | + | |
| 101 | + | |
| 102 | + | |
| 103 | + | |
| 104 | + | |
| 105 | + | |
87 | 106 | | |
88 | 107 | | |
89 | 108 | | |
| |||
0 commit comments