From 11bf9221c240d3b66c0d58bd47def0eaa97cbfa6 Mon Sep 17 00:00:00 2001 From: sky22333 Date: Sat, 16 May 2026 06:26:21 +0800 Subject: [PATCH] =?UTF-8?q?fix=20=E4=BF=AE=E5=A4=8D=E8=BF=94=E5=9B=9E?= =?UTF-8?q?=E4=BA=86=E8=BF=87=E6=9C=9F=E7=9A=84=E5=8C=BF=E5=90=8Dtoken?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/utils/cache.go | 14 +++++++++++--- src/utils/cache_test.go | 4 ++++ 2 files changed, 15 insertions(+), 3 deletions(-) diff --git a/src/utils/cache.go b/src/utils/cache.go index 488ce80..00edf06 100644 --- a/src/utils/cache.go +++ b/src/utils/cache.go @@ -102,10 +102,18 @@ func ExtractTTLFromResponse(responseBody []byte) time.Duration { defaultTTL := 30 * time.Minute if json.Unmarshal(responseBody, &tokenResp) == nil && tokenResp.ExpiresIn > 0 { - safeTTL := time.Duration(tokenResp.ExpiresIn-300) * time.Second - if safeTTL > 5*time.Minute { - return safeTTL + expires := time.Duration(tokenResp.ExpiresIn) * time.Second + skew := expires / 10 + if skew > 5*time.Minute { + skew = 5 * time.Minute } + if skew < 10*time.Second { + skew = 10 * time.Second + } + if expires > skew { + return expires - skew + } + return expires / 2 } return defaultTTL diff --git a/src/utils/cache_test.go b/src/utils/cache_test.go index e31a81e..a5b2a8b 100644 --- a/src/utils/cache_test.go +++ b/src/utils/cache_test.go @@ -34,6 +34,10 @@ func TestExtractTTLFromResponse(t *testing.T) { t.Fatalf("TTL = %s, want 55m", ttl) } + if ttl := ExtractTTLFromResponse([]byte(`{"expires_in":300}`)); ttl != 270*time.Second { + t.Fatalf("short TTL = %s, want 270s", ttl) + } + if ttl := ExtractTTLFromResponse([]byte(`{}`)); ttl != 30*time.Minute { t.Fatalf("default TTL = %s", ttl) }