Skip to content

Commit 49c4cfa

Browse files
authored
fix:GetAccessTokenContext从cache中获取字符窜为空时,从微信服务器获取 (#721)
Co-authored-by: markwang <[email protected]>
1 parent ead8a6f commit 49c4cfa

File tree

1 file changed

+6
-4
lines changed

1 file changed

+6
-4
lines changed

credential/default_access_token.go

+6-4
Original file line numberDiff line numberDiff line change
@@ -66,17 +66,19 @@ func (ak *DefaultAccessToken) GetAccessToken() (accessToken string, err error) {
6666
func (ak *DefaultAccessToken) GetAccessTokenContext(ctx context.Context) (accessToken string, err error) {
6767
// 先从cache中取
6868
accessTokenCacheKey := fmt.Sprintf("%s_access_token_%s", ak.cacheKeyPrefix, ak.appID)
69-
if val := ak.cache.Get(accessTokenCacheKey); val != nil {
70-
return val.(string), nil
69+
val := ak.cache.Get(accessTokenCacheKey)
70+
if accessToken = val.(string); accessToken != "" {
71+
return
7172
}
7273

7374
// 加上lock,是为了防止在并发获取token时,cache刚好失效,导致从微信服务器上获取到不同token
7475
ak.accessTokenLock.Lock()
7576
defer ak.accessTokenLock.Unlock()
7677

7778
// 双检,防止重复从微信服务器获取
78-
if val := ak.cache.Get(accessTokenCacheKey); val != nil {
79-
return val.(string), nil
79+
val = ak.cache.Get(accessTokenCacheKey)
80+
if accessToken = val.(string); accessToken != "" {
81+
return
8082
}
8183

8284
// cache失效,从微信服务器获取

0 commit comments

Comments
 (0)