作者 yangfu

fix token repeat fetch in concurrent env

@@ -183,6 +183,11 @@ func (notify *GetuiNotification) GetAuthToken() (token string, err error) { @@ -183,6 +183,11 @@ func (notify *GetuiNotification) GetAuthToken() (token string, err error) {
183 183
184 authMux.Lock() 184 authMux.Lock()
185 defer authMux.Unlock() 185 defer authMux.Unlock()
  186 + // recheck
  187 + if authtoken != "" && expire.Unix() > time.Now().Unix() {
  188 + token = authtoken
  189 + return
  190 + }
186 url := notify.Url(notify.Options.AppId, authSign) 191 url := notify.Url(notify.Options.AppId, authSign)
187 notify.Request = httplib.Post(strings.TrimSpace(url)) 192 notify.Request = httplib.Post(strings.TrimSpace(url))
188 req := &AuthSignRequest{ 193 req := &AuthSignRequest{
@@ -3,6 +3,7 @@ package getuiV2 @@ -3,6 +3,7 @@ package getuiV2
3 import ( 3 import (
4 "openapi/pkg/infrastructure/push" 4 "openapi/pkg/infrastructure/push"
5 "openapi/pkg/infrastructure/utils" 5 "openapi/pkg/infrastructure/utils"
  6 + "sync"
6 "testing" 7 "testing"
7 ) 8 )
8 9
@@ -77,3 +78,29 @@ func TestGetuiPrd(t *testing.T) { @@ -77,3 +78,29 @@ func TestGetuiPrd(t *testing.T) {
77 t.Fatal(err) 78 t.Fatal(err)
78 } 79 }
79 } 80 }
  81 +
  82 +func TestGetAuthToken(t *testing.T) {
  83 + var wg sync.WaitGroup
  84 + round := 100
  85 + notification := &GetuiNotification{}
  86 + var tokenMap = make(map[string]string)
  87 + notification.Init(
  88 + push.DebugModule(true),
  89 +
  90 + push.AppId("WgrbaaStTk7JElrXOCgUg6"),
  91 + push.AppKey("FG5lbqVrHa5rS9NVfxNP7"),
  92 + push.AppMasterSecret("FW3jMNLJrRARYKv2iqA5H5"),
  93 + )
  94 + for i := 0; i < round; i++ {
  95 + wg.Add(1)
  96 + go func() {
  97 + defer wg.Done()
  98 + token, _ := notification.GetAuthToken()
  99 + tokenMap[token] = token
  100 + }()
  101 + }
  102 + wg.Wait()
  103 + if len(tokenMap) > 1 {
  104 + t.Fatalf("token want 1 get %v", len(tokenMap))
  105 + }
  106 +}