Skip to content

Commit

Permalink
自动更新Token,panic后增加自动重启
Browse files Browse the repository at this point in the history
  • Loading branch information
iGoogle-ink committed Aug 3, 2024
1 parent 52f9f61 commit 9db3aa2
Show file tree
Hide file tree
Showing 4 changed files with 60 additions and 49 deletions.
95 changes: 50 additions & 45 deletions mini/access_token.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,52 +10,52 @@ import (

// 获取小程序全局唯一后台接口调用凭据(access_token)
// 微信小程序文档:https://developers.weixin.qq.com/miniprogram/dev/OpenApiDoc/mp-access-token/getAccessToken.html
func (s *SDK) getAccessToken() (err error) {
defer func() {
if err != nil {
// reset default refresh internal
s.RefreshInternal = time.Second * 20
if s.callback != nil {
go s.callback("", "", 0, err)
}
}
}()

path := "/cgi-bin/token?grant_type=client_credential&appid=" + s.Appid + "&secret=" + s.Secret
at := &AccessToken{}
if _, err = s.DoRequestGet(s.ctx, path, at); err != nil {
return
}
if at.Errcode != Success {
err = fmt.Errorf("errcode(%d), errmsg(%s)", at.Errcode, at.Errmsg)
return
}
s.accessToken = at.AccessToken
s.RefreshInternal = time.Second * time.Duration(at.ExpiresIn)
if s.callback != nil {
go s.callback(s.Appid, at.AccessToken, at.ExpiresIn, nil)
}
return nil
}
//func (s *SDK) getAccessToken() (err error) {
// defer func() {
// if err != nil {
// // reset default refresh internal
// s.RefreshInternal = time.Second * 20
// if s.callback != nil {
// go s.callback("", "", 0, err)
// }
// }
// }()
//
// path := "/cgi-bin/token?grant_type=client_credential&appid=" + s.Appid + "&secret=" + s.Secret
// at := &AccessToken{}
// if _, err = s.DoRequestGet(s.ctx, path, at); err != nil {
// return
// }
// if at.Errcode != Success {
// err = fmt.Errorf("errcode(%d), errmsg(%s)", at.Errcode, at.Errmsg)
// return
// }
// s.accessToken = at.AccessToken
// s.RefreshInternal = time.Second * time.Duration(at.ExpiresIn)
// if s.callback != nil {
// go s.callback(s.Appid, at.AccessToken, at.ExpiresIn, nil)
// }
// return nil
//}

func (s *SDK) goAutoRefreshAccessToken() {
defer func() {
if r := recover(); r != nil {
buf := make([]byte, 64<<10)
buf = buf[:runtime.Stack(buf, false)]
s.logger.Errorf("mini_goAutoRefreshAccessToken: panic recovered: %s\n%s", r, buf)
}
}()
for {
// every one hour, request new access token, default 10s
time.Sleep(s.RefreshInternal / 2)
err := s.getAccessToken()
if err != nil {
s.logger.Errorf("get access token error, after 10s retry: %+v", err)
continue
}
}
}
//func (s *SDK) goAutoRefreshAccessToken() {
// defer func() {
// if r := recover(); r != nil {
// buf := make([]byte, 64<<10)
// buf = buf[:runtime.Stack(buf, false)]
// s.logger.Errorf("mini_goAutoRefreshAccessToken: panic recovered: %s\n%s", r, buf)
// }
// }()
// for {
// // every one hour, request new access token, default 10s
// time.Sleep(s.RefreshInternal / 2)
// err := s.getAccessToken()
// if err != nil {
// s.logger.Errorf("get access token error, after 10s retry: %+v", err)
// continue
// }
// }
//}

// 获取稳定版接口调用凭据
// 微信小程序文档:https://developers.weixin.qq.com/miniprogram/dev/OpenApiDoc/mp-access-token/getStableAccessToken.html
Expand Down Expand Up @@ -93,6 +93,11 @@ func (s *SDK) goAutoRefreshStableAccessToken() {
buf := make([]byte, 64<<10)
buf = buf[:runtime.Stack(buf, false)]
s.logger.Errorf("mini_goAutoRefreshAccessToken: panic recovered: %s\n%s", r, buf)
time.Sleep(time.Second * 3)
if err := s.getStableAccessToken(); err != nil {
// 失败就不再自动刷新了
return
}
s.goAutoRefreshStableAccessToken()
}
}()
Expand Down
1 change: 1 addition & 0 deletions open/access_token.go
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,7 @@ func (s *SDK) goAutoRefreshAccessTokenJob() {
buf := make([]byte, 64<<10)
buf = buf[:runtime.Stack(buf, false)]
s.logger.Errorf("open_goAutoRefreshAccessToken: panic recovered: %s\n%s", r, buf)
time.Sleep(time.Second * 3)
s.goAutoRefreshAccessTokenJob()
}
}()
Expand Down
11 changes: 8 additions & 3 deletions public/access_token.go
Original file line number Diff line number Diff line change
Expand Up @@ -36,13 +36,18 @@ func (s *SDK) getAccessToken() (err error) {
return nil
}

func (s *SDK) goAutoRefreshAccessToken() {
func (s *SDK) goAutoRefreshAccessTokenJob() {
defer func() {
if r := recover(); r != nil {
buf := make([]byte, 64<<10)
buf = buf[:runtime.Stack(buf, false)]
s.logger.Errorf("public_goAutoRefreshAccessToken: panic recovered: %s\n%s", r, buf)
s.goAutoRefreshAccessToken()
s.logger.Errorf("public_goAutoRefreshAccessTokenJob: panic recovered: %s\n%s", r, buf)
time.Sleep(time.Second * 3)
if err := s.getAccessToken(); err != nil {
// 失败就不再自动刷新了
return
}
s.goAutoRefreshAccessTokenJob()
}
}()
for {
Expand Down
2 changes: 1 addition & 1 deletion public/public.go
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@ func New(appid, secret string, autoManageToken bool) (p *SDK, err error) {
if err = p.getAccessToken(); err != nil {
return nil, err
}
go p.goAutoRefreshAccessToken()
go p.goAutoRefreshAccessTokenJob()
}
return
}
Expand Down

0 comments on commit 9db3aa2

Please sign in to comment.