mirror of
https://github.com/Luzifer/mercedes-byocar-exporter.git
synced 2024-11-08 14:00:09 +00:00
Fix: Renew token early, prevent concurrent token refreshes
Signed-off-by: Knut Ahlers <knut@ahlers.me>
This commit is contained in:
parent
461697bf8d
commit
75ba452149
2 changed files with 5 additions and 4 deletions
|
@ -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])
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
|
@ -21,6 +21,7 @@ import (
|
|||
const (
|
||||
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")
|
||||
|
|
Loading…
Reference in a new issue