fix token refresh routine not exit error

This commit is contained in:
xiaoyaofenfen 2022-12-21 16:27:39 +08:00
parent b74d39da41
commit ed38002e65
4 changed files with 38 additions and 12 deletions

View File

@ -33,6 +33,7 @@ import (
"path" "path"
"path/filepath" "path/filepath"
"runtime" "runtime"
"sync/atomic"
"time" "time"
) )
@ -226,14 +227,19 @@ func RunDownload(paths []string, options *DownloadOptions) {
activeUser.PanClient().ClearCache() activeUser.PanClient().ClearCache()
defer activeUser.PanClient().DisableCache() defer activeUser.PanClient().DisableCache()
// pan token expired checker // pan token expired checker
go func() { continueFlag := int32(0)
for { atomic.StoreInt32(&continueFlag, 0)
defer func() {
atomic.StoreInt32(&continueFlag, 1)
}()
go func(flag *int32) {
for atomic.LoadInt32(flag) == 0 {
time.Sleep(time.Duration(1) * time.Minute) time.Sleep(time.Duration(1) * time.Minute)
if RefreshTokenInNeed(activeUser) { if RefreshTokenInNeed(activeUser) {
logger.Verboseln("update access token for download task") logger.Verboseln("update access token for download task")
} }
} }
}() }(&continueFlag)
if options == nil { if options == nil {
options = &DownloadOptions{} options = &DownloadOptions{}

View File

@ -28,6 +28,7 @@ import (
"os" "os"
"path" "path"
"strings" "strings"
"sync/atomic"
"time" "time"
) )
@ -305,15 +306,20 @@ func RunSync(defaultTask *syncdrive.SyncTask, fileDownloadParallel, fileUploadPa
panClient.DisableCache() panClient.DisableCache()
// pan token expired checker // pan token expired checker
go func() { continueFlag := int32(0)
for { atomic.StoreInt32(&continueFlag, 0)
defer func() {
atomic.StoreInt32(&continueFlag, 1)
}()
go func(flag *int32) {
for atomic.LoadInt32(flag) == 0 {
time.Sleep(time.Duration(1) * time.Minute) time.Sleep(time.Duration(1) * time.Minute)
if RefreshTokenInNeed(activeUser) { if RefreshTokenInNeed(activeUser) {
logger.Verboseln("update access token for sync task") logger.Verboseln("update access token for sync task")
panClient.UpdateToken(activeUser.WebToken) panClient.UpdateToken(activeUser.WebToken)
} }
} }
}() }(&continueFlag)
syncFolderRootPath := config.GetSyncDriveDir() syncFolderRootPath := config.GetSyncDriveDir()
if b, e := utils.PathExists(syncFolderRootPath); e == nil { if b, e := utils.PathExists(syncFolderRootPath); e == nil {

View File

@ -27,6 +27,7 @@ import (
"path/filepath" "path/filepath"
"strings" "strings"
"sync" "sync"
"sync/atomic"
"time" "time"
"github.com/tickstep/library-go/logger" "github.com/tickstep/library-go/logger"
@ -268,14 +269,19 @@ func RunUpload(localPaths []string, savePath string, opt *UploadOptions) {
activeUser.PanClient().ClearCache() activeUser.PanClient().ClearCache()
defer activeUser.PanClient().DisableCache() defer activeUser.PanClient().DisableCache()
// pan token expired checker // pan token expired checker
go func() { continueFlag := int32(0)
for { atomic.StoreInt32(&continueFlag, 0)
defer func() {
atomic.StoreInt32(&continueFlag, 1)
}()
go func(flag *int32) {
for atomic.LoadInt32(flag) == 0 {
time.Sleep(time.Duration(1) * time.Minute) time.Sleep(time.Duration(1) * time.Minute)
if RefreshTokenInNeed(activeUser) { if RefreshTokenInNeed(activeUser) {
logger.Verboseln("update access token for upload task") logger.Verboseln("update access token for upload task")
} }
} }
}() }(&continueFlag)
if opt == nil { if opt == nil {
opt = &UploadOptions{} opt = &UploadOptions{}

View File

@ -21,6 +21,7 @@ import (
"github.com/tickstep/library-go/logger" "github.com/tickstep/library-go/logger"
"github.com/urfave/cli" "github.com/urfave/cli"
"strings" "strings"
"sync/atomic"
"time" "time"
) )
@ -72,8 +73,15 @@ aliyunpan webdav start -h
return nil return nil
} }
activeUser := GetActiveUser() activeUser := GetActiveUser()
go func() {
for { // pan token expired checker
continueFlag := int32(0)
atomic.StoreInt32(&continueFlag, 0)
defer func() {
atomic.StoreInt32(&continueFlag, 1)
}()
go func(flag *int32) {
for atomic.LoadInt32(flag) == 0 {
// token刷新 // token刷新
time.Sleep(time.Duration(1) * time.Minute) time.Sleep(time.Duration(1) * time.Minute)
//time.Sleep(time.Duration(5) * time.Second) //time.Sleep(time.Duration(5) * time.Second)
@ -81,7 +89,7 @@ aliyunpan webdav start -h
logger.Verboseln("reload new access token for webdav") logger.Verboseln("reload new access token for webdav")
} }
} }
}() }(&continueFlag)
webdavServ := &webdav.WebdavConfig{ webdavServ := &webdav.WebdavConfig{
PanDriveId: "", PanDriveId: "",