From 0ca90cca653f2c548021a5cd61978b6f073b369e Mon Sep 17 00:00:00 2001 From: ryanlee-gemini Date: Wed, 18 Dec 2024 10:16:45 +0800 Subject: [PATCH 1/2] =?UTF-8?q?=E5=AE=8C=E5=96=84=20access=20token=20?= =?UTF-8?q?=E5=88=B7=E6=96=B0=E9=80=BB=E8=BE=91?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- token/token_source.go | 16 ++++++++++++++-- 1 file changed, 14 insertions(+), 2 deletions(-) diff --git a/token/token_source.go b/token/token_source.go index e237820..49b8393 100644 --- a/token/token_source.go +++ b/token/token_source.go @@ -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 { @@ -200,7 +212,7 @@ func StartRefreshAccessToken(ctx context.Context, tokenSource oauth2.TokenSource } } }() - return err + return nil } var ( From 77aba12973a057cffec12f77e611339e42bf012c Mon Sep 17 00:00:00 2001 From: ryanlee-gemini Date: Wed, 18 Dec 2024 10:29:38 +0800 Subject: [PATCH 2/2] =?UTF-8?q?=E5=AE=8C=E5=96=84=20access=20token=20?= =?UTF-8?q?=E5=88=B7=E6=96=B0=E9=80=BB=E8=BE=91?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- token/token_source.go | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/token/token_source.go b/token/token_source.go index 49b8393..2e936ed 100644 --- a/token/token_source.go +++ b/token/token_source.go @@ -188,7 +188,7 @@ func StartRefreshAccessToken(ctx context.Context, tokenSource oauth2.TokenSource panic("get token failed continuously for more than ten times") } consecutiveFailures++ - refreshMilliSec = int64(time.Second) + refreshMilliSec = 1000 // 1000ms后重试 } else { consecutiveFailures = 0 refreshMilliSec = getRefreshMilliSec(tk.ExpiresIn)