Skip to content
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

Feat access token cache #1249

Open
wants to merge 5 commits into
base: develop
Choose a base branch
from

Conversation

EngRajabi
Copy link
Contributor

@EngRajabi EngRajabi commented May 30, 2020

New Feature

Cache response access token

Motivation for New Feature

  • Improve performance for cache identity response.
  • Improve performance for cache disk file repository.
  • In large projects and with many simultaneous requests, this cache is required.

@EngRajabi
Copy link
Contributor Author

@TomPallister

Copy link

@Hossein-Edraki Hossein-Edraki left a comment

Choose a reason for hiding this comment

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

That's Good

@raman-m
Copy link
Member

raman-m commented Jul 19, 2023

Hi Mohsen!
Thanks for the PR!

What issue is this PR related to?

@raman-m raman-m changed the base branch from master to develop July 19, 2023 17:38
@raman-m
Copy link
Member

raman-m commented Jul 19, 2023

@EngRajabi commented on May 30, 2020:

improve performance for cache identity response.
improve performance for cache disk file repository.

In comparison to what performance indicators was the current performance improved?
Could you rewrite the description of your feature request please?

@raman-m raman-m self-requested a review July 19, 2023 17:58
@raman-m raman-m added feature A new feature question Initially seen a question could become a new feature or bug or closed ;) needs feedback Issue is waiting on feedback before acceptance waiting Waiting for answer to question or feedback from issue raiser labels Jul 19, 2023
@EngRajabi
Copy link
Contributor Author

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.

Copy link
Contributor Author

@EngRajabi EngRajabi left a comment

Choose a reason for hiding this comment

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

merge conflict

@raman-m
Copy link
Member

raman-m commented Jul 20, 2023

@EngRajabi commented on July 20, 2023

Did you use Service Discovery + Identity Server setup, or classic setup + Identity Server Bearer Tokens?
🆗 We will talk more during code review...

@raman-m
Copy link
Member

raman-m commented Jul 20, 2023

Unfortunately there are no builds for these commits!
So, I cannot start code review without having an actual valid/green build...

@raman-m
Copy link
Member

raman-m commented Aug 1, 2023

Mohsen, congrats! 🎉
The feature branch has been rebased onto ThreeMammals:develop successfully!

Now we can go with code review...

And, once again,
What issue is this PR related to?

@raman-m raman-m removed question Initially seen a question could become a new feature or bug or closed ;) waiting Waiting for answer to question or feedback from issue raiser labels Aug 1, 2023
_environmentFilePath = $"{AppContext.BaseDirectory}{ConfigurationFileName}{(string.IsNullOrEmpty(hostingEnvironment.EnvironmentName) ? string.Empty : ".")}{hostingEnvironment.EnvironmentName}.json";
_cacheKey = "InternalDiskFileConfigurationRepository";
Copy link
Collaborator

Choose a reason for hiding this comment

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

could be a constant

Copy link
Member

Choose a reason for hiding this comment

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

Agree with Ray!
It should be public static const field or property.

What is the strange key value?
Can we write "BlaBlaBlaKey"? 🤣

Copy link
Member

Choose a reason for hiding this comment

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

Suggested change
_cacheKey = "InternalDiskFileConfigurationRepository";
_cacheKey = nameof(DiskFileConfigurationRepository);

Copy link
Member

@raman-m raman-m left a comment

Choose a reason for hiding this comment

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

I have found

  • 1 major issue
  • a few minor issues aka hardcode
  • minor styling & formatting issues

Could you fix them, Mohsen, please?

@@ -83,7 +83,7 @@ public async Task<Response> Set(FileConfiguration ocelotConfiguration)

if (result.Response)
{
_cache.AddAndDelete(_configurationKey, ocelotConfiguration, TimeSpan.FromSeconds(3), _configurationKey);
_cache.AddAndDelete(_configurationKey, ocelotConfiguration, TimeSpan.FromSeconds(5), _configurationKey);
Copy link
Member

@raman-m raman-m Aug 4, 2023

Choose a reason for hiding this comment

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

${\color{orange}Hardcode\ has\ been\ detected!}$

It is better to define public static const field.
Is this value used somewhere else?

As I understood, we cache for 5 seconds, right?
Why 3 or 5 seconds?

Comment on lines 20 to +17
public AuthenticationMiddleware(RequestDelegate next,
IOcelotLoggerFactory loggerFactory)
IOcelotLoggerFactory loggerFactory, IMemoryCache memoryCache)
Copy link
Member

@raman-m raman-m Aug 4, 2023

Choose a reason for hiding this comment

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

Styling issue

Suggested change
public AuthenticationMiddleware(RequestDelegate next,
IOcelotLoggerFactory loggerFactory)
IOcelotLoggerFactory loggerFactory, IMemoryCache memoryCache)
public AuthenticationMiddleware(
RequestDelegate next,
IOcelotLoggerFactory loggerFactory,
IMemoryCache memoryCache)

Goal: Each param is located on own line.
Or, put these params into one line. It's OK because there are up to 3 params only.

Comment on lines +34 to +31
Logger.LogInformation(
$"{httpContext.Request.Path} is an authenticated route. {MiddlewareName} checking if client is authenticated");
Copy link
Member

@raman-m raman-m Aug 4, 2023

Choose a reason for hiding this comment

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

Formatting issue: Should be one line!

Comment on lines +44 to +42
var result =
await httpContext.AuthenticateAsync(downstreamRoute.AuthenticationOptions
.AuthenticationProviderKey);
Copy link
Member

@raman-m raman-m Aug 4, 2023

Choose a reason for hiding this comment

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

Formatting issue: 3 lines!
But it should be one line actually.


userClaim = result.Principal;

_memoryCache.Set(cacheKey, userClaim, TimeSpan.FromMinutes(5));
Copy link
Member

@raman-m raman-m Aug 4, 2023

Choose a reason for hiding this comment

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

${\color{orange}Hardcode\ has\ been\ detected!}$

5 is hardcode.
Should be public static const field.

_environmentFilePath = $"{AppContext.BaseDirectory}{ConfigurationFileName}{(string.IsNullOrEmpty(hostingEnvironment.EnvironmentName) ? string.Empty : ".")}{hostingEnvironment.EnvironmentName}.json";
_cacheKey = "InternalDiskFileConfigurationRepository";
Copy link
Member

Choose a reason for hiding this comment

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

Agree with Ray!
It should be public static const field or property.

What is the strange key value?
Can we write "BlaBlaBlaKey"? 🤣

_environmentFilePath = $"{AppContext.BaseDirectory}{ConfigurationFileName}{(string.IsNullOrEmpty(hostingEnvironment.EnvironmentName) ? string.Empty : ".")}{hostingEnvironment.EnvironmentName}.json";
_cacheKey = "InternalDiskFileConfigurationRepository";
Copy link
Member

Choose a reason for hiding this comment

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

Suggested change
_cacheKey = "InternalDiskFileConfigurationRepository";
_cacheKey = nameof(DiskFileConfigurationRepository);

@@ -63,6 +76,9 @@ public Task<Response> Set(FileConfiguration fileConfiguration)
}

_changeTokenSource.Activate();

_cache.AddAndDelete(_cacheKey, fileConfiguration, TimeSpan.FromMinutes(5), _cacheKey);
Copy link
Member

@raman-m raman-m Aug 4, 2023

Choose a reason for hiding this comment

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

${\color{orange}Hardcode\ has\ been\ detected!}$

5 should be public static const field or property.

Why do you use _cacheKey as a value of the region parameter?

{
return Task.FromResult<Response<FileConfiguration>>(new OkResponse<FileConfiguration>(configuration));
}

Copy link
Member

Choose a reason for hiding this comment

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

${\color{red}No\ adding\ the\ value\ to\ the\ cache!}$

Current implementation reads the value from the cache, and returns the value if it exists.

When the cache is empty, after reading the file content in lock and deserialization in line 52, we have to add the value to the cache immediately.

So, you forgot to renew the cache by Add method after the line 52:

            lock (_lock)
            {
                jsonConfiguration = System.IO.File.ReadAllText(_environmentFilePath);
            }
            var fileConfiguration = JsonConvert.DeserializeObject<FileConfiguration>(jsonConfiguration);

            // TODO Add this line
            _cache.AddAndDelete(_cacheKey, fileConfiguration, TimeSpan.FromMinutes(5), _cacheKey);
            // OR
            _cache.Add(_cacheKey, fileConfiguration, TimeSpan.FromMinutes(5), _cacheKey);
            // return

@raman-m
Copy link
Member

raman-m commented Aug 4, 2023

@EngRajabi

Questions 👇

Based on PR title "Feat access token cache", I would say that I cannot get: how do you manage caching of access token?
What did I see in the PR that you manage content of the file configuration.
Do we need to change the title of PR?
Or
Did you forget to write a code for caching of access token?


Maybe you were inspired by some issue from backlog?
What issue is this PR related to?

@RaynaldM RaynaldM self-requested a review August 7, 2023 07:12
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
feature A new feature needs feedback Issue is waiting on feedback before acceptance
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

5 participants