Improve auth-flow & check killswitch every 15min#54
Open
jacobbaungard wants to merge 8 commits intofaanskit:masterfrom
Open
Improve auth-flow & check killswitch every 15min#54jacobbaungard wants to merge 8 commits intofaanskit:masterfrom
jacobbaungard wants to merge 8 commits intofaanskit:masterfrom
Conversation
Signed-off-by: Jacob Baungard Hansen <me@jacobbaungard.com>
This commit ensures we can re-use JWT tokens, and refresh accordingly, instead of relogging every time, which often causes 429/rate-limits from checkwatt. The client of the library should pass in an authinfo object (by reference) when using the manager. The manager will update any info in the authinfo object as needed.
Signed-off-by: Jacob Baungard Hansen <me@jacobbaungard.com>
The killswitch suffers often from 429/too many requests. After agreement with CW, we check it only every 15minute. On startup we check it immediately, and then generate a random time interval for the next check. This should ensure the load gets evenly spread out between installations.
Add a one minute margin to the JWT/refresh token expiry checks to ensure the token doesn't expire shortly after the check.
So they work with the new stateinfo object.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Auth flow
Instead of doing a full login flow at every update (i.e 1 min interval from HA), we re-use the JWT token if possible. If the JWT token is expired, we refresh the token if possible, instead of running the full login flow. This should be a much more efficient flow. When checking expire of the jwt/refresh token, we add a one minute margin, to ensure clients have time to complete their requests after the login flow.
Clients will need to pass in (by reference) a CheckwattStateInfo. This is meant to keep the state in memory between multiple CheckwattManagers (HA creates a new one every update, perhaps that can be changed in the future).
Kill switch
It turns out the checking of the killswitch is what causes the 429 seen in various installations. After agreement
with CW, with this PR we check it only every 15minute, which should lower the load on CWs server significantly.
On startup we check the killswitch immediately, and then generate a random time interval for the next check. This should ensure the load gets evenly spread out between installations.
Testing
I've tested this quite a bit over the last week or so, making adjusts as I go. Everything seems to work fine, although in my installation I still get 429s when checking the killswitch - likely we'd need many people to update before we see the effect of this change.
If someone wants to test this on an actual HA installation, this is how I've run my docker-based HA installation:
You may also want to increase the logging level to debug while testing: