Improve auth-flow & check killswitch every 15min #54
+865
−424
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: