Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix:GetAccessTokenContext从cache中获取字符窜为空时,从微信服务器获取 #721

Merged
merged 1 commit into from
Sep 24, 2023
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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 != "" {

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

当redis不存在access token key时,val为nil,val.(string)会crash。

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
Loading