Skip to content

Commit

Permalink
Update default_access_token.go (#805)
Browse files Browse the repository at this point in the history
* Update default_access_token.go

微信获取稳定版token,只有不等于空字符串的情况下才会返回access_token信息,未空的情况,继续调去微信服务

* 微信稳定性获取 access_token 接口,添加防止并发性获取 access_token 和多次微信服务获取代码

---------

Co-authored-by: w_yangyili <w_yangyili@xiwang.com>
  • Loading branch information
yangyl12345 and w_yangyili authored Nov 26, 2024
1 parent 3fbe863 commit a571bf3
Showing 1 changed file with 24 additions and 9 deletions.
33 changes: 24 additions & 9 deletions credential/default_access_token.go
Original file line number Diff line number Diff line change
Expand Up @@ -101,10 +101,11 @@ func (ak *DefaultAccessToken) GetAccessTokenContext(ctx context.Context) (access
// 不强制更新access_token,可用于不同环境不同服务而不需要分布式锁以及公用缓存,避免access_token争抢
// https://developers.weixin.qq.com/miniprogram/dev/OpenApiDoc/mp-access-token/getStableAccessToken.html
type StableAccessToken struct {
appID string
appSecret string
cacheKeyPrefix string
cache cache.Cache
appID string
appSecret string
cacheKeyPrefix string
cache cache.Cache
accessTokenLock *sync.Mutex
}

// NewStableAccessToken new StableAccessToken
Expand All @@ -113,10 +114,11 @@ func NewStableAccessToken(appID, appSecret, cacheKeyPrefix string, cache cache.C
panic("cache is need")
}
return &StableAccessToken{
appID: appID,
appSecret: appSecret,
cache: cache,
cacheKeyPrefix: cacheKeyPrefix,
appID: appID,
appSecret: appSecret,
cache: cache,
cacheKeyPrefix: cacheKeyPrefix,
accessTokenLock: new(sync.Mutex),
}
}

Expand All @@ -130,7 +132,20 @@ func (ak *StableAccessToken) GetAccessTokenContext(ctx context.Context) (accessT
// 先从cache中取
accessTokenCacheKey := fmt.Sprintf("%s_stable_access_token_%s", ak.cacheKeyPrefix, ak.appID)
if val := ak.cache.Get(accessTokenCacheKey); val != nil {
return val.(string), nil
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 {
if accessToken = val.(string); accessToken != "" {
return
}
}

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

0 comments on commit a571bf3

Please sign in to comment.