-
Notifications
You must be signed in to change notification settings - Fork 1.6k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Caching ClaimsPrincipal
during authentication and re-caching FileConfiguration
objects in Consul and Disk File configuration repos
#1249
base: develop
Are you sure you want to change the base?
Conversation
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
That's Good
Hi Mohsen! What issue is this PR related to? |
@EngRajabi commented on May 30, 2020:
In comparison to what performance indicators was the current performance improved? |
I was in an enterprise project that used the reference token type. I had a performance problem. Because in every request, it needs to request the identity of the server. The problem was solved with this cache. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
merge conflict
Did you use Service Discovery + Identity Server setup, or classic setup + Identity Server Bearer Tokens? |
Unfortunately there are no builds for these commits! |
cd5675c
to
0561ad5
Compare
Mohsen, congrats! 🎉 Now we can go with code review... And, once again, |
c8c0634
to
4c2b93e
Compare
4c2b93e
to
a4e33f4
Compare
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The primary issue with this PR is the lack of a feature switcher❗
We suggest upgrading the middleware to enforce cache usage, which could be a breaking change for Ocelot applications in production. Therefore, we need to implement the upgrade with the cache turned off by default, and introduce a new JSON option to enable/disable caching as needed.
Additional suggestions include:
- Implementing unit+acceptance tests to cover the new static
CacheTtlSeconds
property - Developing integration tests with usage of the
DefaultMemoryCache{T}
caching service
await _next(httpContext); | ||
return; | ||
} | ||
|
||
Logger.LogInformation(() => $"The path '{path}' is an authenticated route! {MiddlewareName} checking if client is authenticated..."); | ||
var token = httpContext.Request.Headers.Authorization; | ||
var cacheKey = $"{nameof(AuthenticationMiddleware)}.{nameof(IHeaderDictionary.Authorization)}:{token}"; | ||
if (!_memoryCache.TryGetValue(cacheKey, out ClaimsPrincipal principal)) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
We are upgrading with enforced use of the cache❗
@@ -42,22 +53,29 @@ private void Initialize(string folder) | |||
|
|||
public Task<Response<FileConfiguration>> Get() | |||
{ | |||
string jsonConfiguration; | |||
var configuration = _cache.Get(CacheKey, region: CacheKey); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Enforced use of the cache❗
@@ -49,7 +51,7 @@ public void MiddlewareName_Cstor_ReturnsTypeName() | |||
isNextCalled = true; | |||
return Task.CompletedTask; | |||
}; | |||
_middleware = new AuthenticationMiddleware(_next, _factory.Object); | |||
_middleware = new AuthenticationMiddleware(_next, _factory.Object, new MemoryCache(new MemoryCacheOptions())); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Enforcing the use of new MemoryCache
turns the test into an integration test.
} | ||
|
||
|
||
// TODO Add integration tests: var aspMemoryCache = new DefaultMemoryCache<FileConfiguration>(new MemoryCache(new MemoryCacheOptions())); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Add integration tests!
ClaimsPrincipal
during authentication and re-caching FileConfiguration
objects in Consul and Disk File repos
ClaimsPrincipal
during authentication and re-caching FileConfiguration
objects in Consul and Disk File reposClaimsPrincipal
during authentication and re-caching FileConfiguration
objects in Consul and Disk File configuration repos
New Feature
Cache response access token
Motivation for New Feature