Skip to content

Commit

Permalink
fix:GetAccessTokenContext从cache中获取字符窜为空时,从微信服务器获取 (#721)
Browse files Browse the repository at this point in the history
Co-authored-by: markwang <www.wang61@qq.com>
  • Loading branch information
markwang1992 and markwang1992 authored Sep 24, 2023
1 parent ead8a6f commit 49c4cfa
Showing 1 changed file with 6 additions and 4 deletions.
10 changes: 6 additions & 4 deletions credential/default_access_token.go
Original file line number Diff line number Diff line change
Expand Up @@ -66,17 +66,19 @@ func (ak *DefaultAccessToken) GetAccessToken() (accessToken string, err error) {
func (ak *DefaultAccessToken) GetAccessTokenContext(ctx context.Context) (accessToken string, err error) {
// 先从cache中取
accessTokenCacheKey := fmt.Sprintf("%s_access_token_%s", ak.cacheKeyPrefix, ak.appID)
if val := ak.cache.Get(accessTokenCacheKey); val != nil {
return val.(string), nil
val := ak.cache.Get(accessTokenCacheKey)
if accessToken = val.(string); accessToken != "" {
return
}

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

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

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

0 comments on commit 49c4cfa

Please sign in to comment.