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

EdgeHub: Fix TokenUpdate logic #206

Merged
merged 16 commits into from
Sep 12, 2018
Merged

EdgeHub: Fix TokenUpdate logic #206

merged 16 commits into from
Sep 12, 2018

Conversation

varunpuranik
Copy link
Contributor

When getting a new token to send on the CBS link, if a soon expiring token is received, instead of returning it, try to get a fresh token. This allows EdgeHub to wait for the device to send a new token, instead of going into token refresh loop.

This PR also fixes this issue - #182

TaskCompletionSource<string> tcs = this.tokenGetter
.GetOrElse(
() =>
// We have to catch UnauthorizedAccessException, because on IsTokenUsable, we call parse from
Copy link
Contributor

Choose a reason for hiding this comment

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

Where does this catch occur?

Copy link
Contributor Author

@varunpuranik varunpuranik Aug 24, 2018

Choose a reason for hiding this comment

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

In GetTokenExpiry (and IsTokenUsable also catches all exceptions)

myagley
myagley previously approved these changes Aug 27, 2018
myagley
myagley previously approved these changes Aug 28, 2018
@@ -263,33 +264,55 @@ void InternalConnectionStatusChangesHandler(ConnectionStatus status, ConnectionS
async Task<string> GetNewToken(string iotHub, string id, string currentToken, IIdentity currentIdentity)
Copy link
Member

Choose a reason for hiding this comment

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

Could you please add some comments explaining the retry logic.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Sure.


retrying = true;
Copy link
Member

Choose a reason for hiding this comment

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

Are we iterating the loop again only to log a message? Could we log the NewTokenObtained event here and return the new token right away?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

We could, but wanted to avoid the duplicate code. This seemed cleaner :)


if (newTokenGetterCreated)
{
if (retrying)
Copy link
Member

Choose a reason for hiding this comment

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

It is not clear to me when and how many times 'newTokenGetterCreated' and 'retrying' would both be true. Perhaps some comments could help clarify when tokenGetters are set and cleared.

this.tokenGetter = Option.None<TaskCompletionSource<string>>();
Events.NewTokenObtained(newCredentials.Identity.IotHubHostName, newCredentials.Identity.Id, tokenAuth.Token);
tg.SetResult(tokenAuth.Token);
Copy link
Member

Choose a reason for hiding this comment

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

Why does this order matter?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Because the code below expects that the tokenGetter will be reset when the value is set. And that also answers your other question, I suppose

Copy link
Member

Choose a reason for hiding this comment

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

In principle, if order is important, then we should do these under a lock. Otherwise order shouldn't matter.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

As discussed offline, the order is important because the caller is waiting for SetResult to be called (not looping), and when it resumes, it expects this.tokenGetter to be reset.

return taskCompletionSource;
});

// If a new tokenGetter was created, then invoke the connection status changed handler
Copy link
Member

Choose a reason for hiding this comment

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

It is still not clear to me from the comments when a new tokenGetter is created. That is key to understanding this loop.

Copy link
Member

Choose a reason for hiding this comment

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

Given the lack of general documentation in our code base, having comments that explain not just things local to a piece of code, but spanning components of code, would be very helpful.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Yes, I agree with you. I am just a bit weary of comments getting stale in that case...

dsajanice
dsajanice previously approved these changes Sep 4, 2018
@varunpuranik varunpuranik merged commit 9d2ba5e into master Sep 12, 2018
@damonbarry damonbarry deleted the varunpuranik/Tokupdfix1 branch September 20, 2018 16:57
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.

4 participants