Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

完善 access token 刷新逻辑 #54

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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 = 1000 // 1000ms后重试
} 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
Loading