Temporarily fix Twitch4J Oauth timeout #94
Merged
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.
closes #70 (although further work is needed)
After some experiments, I figured out that this error occurs when the application is running while the access token runs out. Twitch4J then fails to get a new accessToken, although it is still possible, because the bot will work fine again, if you restart the application. This occurs somewhere between 3 and 6 Hours after application start, depending on when the accessToken was created.
The logs mentioned something about not finding the twitch client ID. I experimented with different options to give the client ID to the twitch client, but nothing changed.
Particularly interesting was the CredentialManager with A IStorageBackend and a AuthenticationController, because it sounds exactly what we would need. But both are not really used by the CredentialManager, and the credential manager is discarded at the end of the TwitchClientBuilder. Only the TwitchIdentityProvider is extracted from it. The IdentityProvider does contain the twitchClientId, but the credential refresh fails for some reason anyway.
The only solution that could be found in a short timeframe was periodically restarting the entire twitch module, because a full restart of the twitch bot always worked.
Because the twitch access tokens seem to have a lifetime of 6 hours (measured from the token creation, not app startup), and the token can only be refreshed a few hours before its lifetime ends, a restart cycle of every 2 Hours was chosen.
The entire solution is suboptimal, since this means that the bot will roughly be offline for at least twice per stream, for ca. 5 seconds each. This is entirely preventable.
A better solution would be to ask the twitch4J maintainers how the automatic token refreshing is supposed to be used, and what it requires. But this may require a minimum example to make it reproducible.