Skip to content

Commit

Permalink
完善 access token 刷新逻辑
Browse files Browse the repository at this point in the history
  • Loading branch information
rianli committed Dec 18, 2024
1 parent 07ab80d commit 0ca90cc
Showing 1 changed file with 14 additions and 2 deletions.
16 changes: 14 additions & 2 deletions token/token_source.go
Original file line number Diff line number Diff line change
Expand Up @@ -179,8 +179,20 @@ func StartRefreshAccessToken(ctx context.Context, tokenSource oauth2.TokenSource
}
log.Debugf("token:%+v ", tk)
go func() {
var consecutiveFailures int
for {
refreshMilliSec := getRefreshMilliSec(tk.ExpiresIn)
var refreshMilliSec int64
//上一轮获取 tk 失败
if tk == nil {
if consecutiveFailures > 10 {
panic("get token failed continuously for more than ten times")
}
consecutiveFailures++
refreshMilliSec = int64(time.Second)
} else {
consecutiveFailures = 0
refreshMilliSec = getRefreshMilliSec(tk.ExpiresIn)
}
log.Debugf("refresh after %d milli sec", refreshMilliSec)
ticker := time.NewTicker(time.Duration(refreshMilliSec) * time.Millisecond).C
select {
Expand All @@ -200,7 +212,7 @@ func StartRefreshAccessToken(ctx context.Context, tokenSource oauth2.TokenSource
}
}
}()
return err
return nil
}

var (
Expand Down

0 comments on commit 0ca90cc

Please sign in to comment.