Fix: Renew token early, prevent concurrent token refreshes

Signed-off-by: Knut Ahlers <knut@ahlers.me>
This commit is contained in:
Knut Ahlers 2022-11-20 13:26:31 +01:00
parent 461697bf8d
commit 75ba452149
Signed by: luzifer
GPG key ID: D91C3E91E4CAD6F5
2 changed files with 5 additions and 4 deletions

View file

@ -10,7 +10,7 @@ import (
func getCronFunc(mc mercedes.Client) func() {
return func() {
for i := range cfg.VehicleID {
go runFetcher(mc, cfg.VehicleID[i])
runFetcher(mc, cfg.VehicleID[i])
}
}
}

View file

@ -19,8 +19,9 @@ import (
)
const (
requestTimeout = 10 * time.Second
stateExpiry = 5 * time.Minute
requestTimeout = 10 * time.Second
stateExpiry = 5 * time.Minute
tokenGraceRenew = -5 * time.Minute
)
type (
@ -181,7 +182,7 @@ func (a APIClient) request(path string, output any) error {
tok := &oauth2.Token{AccessToken: at, RefreshToken: rt, Expiry: exp}
// Renew token if required
if tok.Expiry.Before(time.Now()) {
if tok.Expiry.Add(tokenGraceRenew).Before(time.Now()) {
src := a.getOauth2Config("").TokenSource(ctx, tok)
if tok, err = src.Token(); err != nil {
return errors.Wrap(err, "renewing token")