Skip to content

Commit

Permalink
chore: 优化分片上传
Browse files Browse the repository at this point in the history
- 超时时间默认24小时
- 加速分片记录的删除
  • Loading branch information
arrebole committed May 25, 2023
1 parent 3d232ce commit 63418cb
Show file tree
Hide file tree
Showing 2 changed files with 8 additions and 28 deletions.
33 changes: 7 additions & 26 deletions cache/upload.go
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
package cache

import (
"crypto/md5"
"encoding/json"
"fmt"
"time"
Expand All @@ -28,18 +27,7 @@ type MutUpload struct {
}

func (p *MutUpload) Key() string {
fingerprint := fmt.Sprintf(
"%s:%s:%d:%d",
p.Path,
p.UpPath,
p.Size,
p.PartSize,
)

return fmt.Sprintf(
"mutupload-%x",
md5.Sum([]byte(fingerprint)),
)
return fmt.Sprintf("mutupload-%s", p.UpPath)
}

// 查询分片上传任务
Expand All @@ -53,7 +41,7 @@ func FindMutUpload(fn func(key string, entity *MutUpload) bool) ([]*MutUpload, e
}

// 删除过期的分片上传记录
if time.Since(item.CreateAt).Hours() > 12 {
if time.Since(item.CreateAt).Hours() > 24 {
FindMutUploadPart(func(key string, part *MutUploadPart) bool {
if part.UploadID == item.UploadID {
db.Delete([]byte(key), nil)
Expand Down Expand Up @@ -124,22 +112,15 @@ func AddMutUploadPart(entity *MutUploadPart) error {
if err != nil {
return err
}

return db.Put([]byte(entity.Key()), data, nil)
}

func DeleteByUploadID(uploadID string) error {
FindMutUpload(func(key string, entity *MutUpload) bool {
if entity.UploadID == uploadID {
Delete(key)
}
return false
func DeleteUpload(upPath, uploadID string) error {
Range("mutupload-"+upPath, func(key, data []byte) {
Delete(string(key))
})
FindMutUploadPart(func(key string, entity *MutUploadPart) bool {
if entity.UploadID == uploadID {
Delete(key)
}
return false
Range("part-"+uploadID, func(key, data []byte) {
Delete(string(key))
})
return nil
}
3 changes: 1 addition & 2 deletions session.go
Original file line number Diff line number Diff line change
Expand Up @@ -571,7 +571,6 @@ func (sess *Session) putFileWithProgress(localPath, upPath string, localInfo os.
}

// 上传分片任务
fmt.Println(skips)
uploader := partial.NewMultiPartialUploader(
DefaultBlockSize,
fd,
Expand Down Expand Up @@ -614,7 +613,7 @@ func (sess *Session) putFileWithProgress(localPath, upPath string, localInfo os.
return err
}
// 上传完成删除记录
cache.DeleteByUploadID(initResult.UploadID)
cache.DeleteUpload(upPath, initResult.UploadID)
} else {
cfg := &upyun.PutObjectConfig{
Path: upPath,
Expand Down

0 comments on commit 63418cb

Please sign in to comment.