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: adding semaphore locking for PersistedAccessTokenCache #667

Conversation

taneltinits
Copy link

closes #

@CLAassistant
Copy link

CLA assistant check
Thank you for your submission! We really appreciate it. Like many open source projects, we ask that you sign our Contributor License Agreement before we can accept your contribution.


tanel.tinits seems not to be a GitHub user. You need a GitHub account to be able to sign the CLA. If you have already a GitHub account, please add the email address used for this commit to your account.
You have signed the CLA already but the status is still pending? Let us recheck it.

Copy link
Collaborator

@ChrisKujawa ChrisKujawa left a comment

Choose a reason for hiding this comment

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

Thanks! I really appreciate your contribution, please sign our CLA otherwise I can't merge it. Furthermore, I added some comments, maybe we can split up the PR to fix these different issues concurrency and TTL separately.

Did you tried whether it solves your issues you have observed?

@@ -70,12 +81,26 @@ private async Task<string> GetValidToken(string audience, AccessToken currentAcc
{
var now = DateTimeOffset.UtcNow.ToUnixTimeMilliseconds();
var dueDate = currentAccessToken.DueDate;
if (now < dueDate)

if (now < dueDate - 15000) // Adding 15 sec buffer to update the token before it really expires.
Copy link
Collaborator

Choose a reason for hiding this comment

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

Lets focus on one topic/part in this PR. We can do/fix this with #642

@@ -86,7 +111,17 @@ private async Task<AccessToken> FetchNewAccessToken(string audience)
{
var newAccessToken = await accessTokenFetcherAsync();
CachedCredentials[audience] = newAccessToken;
WriteCredentials();

await semaphore.WaitAsync();
Copy link
Collaborator

Choose a reason for hiding this comment

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

❓ I guess we could use just lock here right? Any specifics/benefits about the semphore here (as you also use just one count)? Please be aware coming from the java world, so I might not be aware of some language specifics from c#.

Copy link
Author

Choose a reason for hiding this comment

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

I will check if I can use just a lock. imo I can block the thread and wait until its available with lock. But in current case I want to check from another thread if the lock is in place and then it would not wait to fetch another token. Rather it could use existing token if its still valid.
There is some Monitor that is using lock behind the scene.
I will mark this PR as draft for now and come back with the updates.

Copy link
Collaborator

Choose a reason for hiding this comment

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

Thanks that makes sense 👍

@taneltinits taneltinits marked this pull request as draft April 16, 2024 06:34
@taneltinits
Copy link
Author

Managed to solve the issue by using another token supplier for the zb-client. Not using the CamundaCloudTokenProvider.

@taneltinits taneltinits closed this May 7, 2024
@taneltinits taneltinits deleted the feature/tokencache-semaphore branch May 7, 2024 12:15
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.

3 participants