From dca7fcb51385dca1e2b4c7eb7859f8333ad50e2e Mon Sep 17 00:00:00 2001 From: AOAOSTAR <86001674+aoaostar@users.noreply.github.com> Date: Sat, 7 May 2022 23:09:41 +0800 Subject: [PATCH] =?UTF-8?q?=F0=9F=90=9B=20=E4=BF=AE=E5=A4=8D=E5=B9=B6?= =?UTF-8?q?=E5=8F=91=E4=B8=8B=E5=88=9B=E5=BB=BA=E7=9B=B8=E5=90=8C=E7=9B=AE?= =?UTF-8?q?=E5=BD=95=E7=9A=84=E9=97=AE=E9=A2=98?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .gitignore | 3 ++- bootstrap/drive.go | 51 ++++++++++++++++++++++++++++------------ conf/const.go | 2 +- pkg/alidrive/alidrive.go | 1 + 4 files changed, 40 insertions(+), 17 deletions(-) diff --git a/.gitignore b/.gitignore index 99023c1..7696dcd 100644 --- a/.gitignore +++ b/.gitignore @@ -4,4 +4,5 @@ logs config.yaml *.exe *.bat -main \ No newline at end of file +main +alidrive \ No newline at end of file diff --git a/bootstrap/drive.go b/bootstrap/drive.go index 5478e0d..9bd29b7 100644 --- a/bootstrap/drive.go +++ b/bootstrap/drive.go @@ -17,7 +17,8 @@ type folderChan struct { id *string } -var tree map[string]interface{} +var tree = make(map[string]interface{}) +var locks = make(map[string]*sync.Mutex) func TreeFolders(drive *alidrive.AliDrive, remotePath string, dirs map[string]string) { @@ -88,13 +89,16 @@ func createFolder(folderChan chan folderChan, drive *alidrive.AliDrive, bar *mpb bar.Increment() continue } + // 从读取已有值 split := strings.Split(folder.pathname, "/") var parentFolderId = drive.Instance.ParentPath var pathname = folder.pathname + tmp := tree for i := 0; i < len(split); i++ { - if v, b := tree[split[i]]; b { - if v2, b2 := v.(map[string]interface{})["__alidrive_id"].(*string); b2 && *v2 != "" { + var b bool + if tmp, b = tmp[split[i]].(map[string]interface{}); b { + if v2, b2 := tmp["__alidrive_id"].(*string); b2 && *v2 != "" { parentFolderId = *v2 pathname = strings.Join(split[i+1:], "/") } @@ -103,20 +107,37 @@ func createFolder(folderChan chan folderChan, drive *alidrive.AliDrive, bar *mpb } } - //重试n次 - for i := 0; i < 4; i++ { - folderId, err := drive.CreateFolders(pathname, parentFolderId) - if err != nil { - if i == 3 { - conf.Output.Panic(err, " parentFolderId ", parentFolderId) + func() { + + index := strings.Index(pathname, "/") + var lockKey string + if index > 0 { + lockKey = pathname[:index] + "_" + parentFolderId + } else { + lockKey = filepath.Base(pathname) + "_" + parentFolderId + } + if _, b := locks[lockKey]; !b { + locks[lockKey] = &sync.Mutex{} + } + locks[lockKey].Lock() + + defer locks[lockKey].Unlock() + //重试n次 + for i := 0; i < 4; i++ { + folderId, err := drive.CreateFolders(pathname, parentFolderId) + if err != nil { + if i == 3 { + conf.Output.Panic(err, " parentFolderId ", parentFolderId) + } + conf.Output.Warnf("第%+v次重试", i+1) + continue } - conf.Output.Warnf("第%+v次重试", i+1) - continue + *folder.id = folderId + bar.Increment() + break } - *folder.id = folderId - bar.Increment() - break - } + }() + } } diff --git a/conf/const.go b/conf/const.go index fb7cc77..b46d169 100644 --- a/conf/const.go +++ b/conf/const.go @@ -10,7 +10,7 @@ import ( ) const ( - VERSION = "v2.1" + VERSION = "v2.1.1" ) var executable, _ = os.Executable() diff --git a/pkg/alidrive/alidrive.go b/pkg/alidrive/alidrive.go index 627316d..5ffa986 100644 --- a/pkg/alidrive/alidrive.go +++ b/pkg/alidrive/alidrive.go @@ -260,6 +260,7 @@ func (drive *AliDrive) Upload(file util.FileStream) error { } func (drive *AliDrive) CreateFolders(path string, rootPath string) (string, error) { + path = filepath.ToSlash(path) split := strings.Split(path, "/") var parentFileId = rootPath