Skip to content

Commit

Permalink
🐛 修复并发下创建相同目录的问题
Browse files Browse the repository at this point in the history
  • Loading branch information
aoaostar committed May 7, 2022
1 parent 60e82f4 commit dca7fcb
Show file tree
Hide file tree
Showing 4 changed files with 40 additions and 17 deletions.
3 changes: 2 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -4,4 +4,5 @@ logs
config.yaml
*.exe
*.bat
main
main
alidrive
51 changes: 36 additions & 15 deletions bootstrap/drive.go
Original file line number Diff line number Diff line change
Expand Up @@ -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) {

Expand Down Expand Up @@ -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:], "/")
}
Expand All @@ -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
}
}()

}
}

Expand Down
2 changes: 1 addition & 1 deletion conf/const.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ import (
)

const (
VERSION = "v2.1"
VERSION = "v2.1.1"
)

var executable, _ = os.Executable()
Expand Down
1 change: 1 addition & 0 deletions pkg/alidrive/alidrive.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down

0 comments on commit dca7fcb

Please sign in to comment.