1
0
mirror of https://github.com/Mrs4s/MiraiGo.git synced 2025-05-04 19:17:38 +08:00

feat: improve upload file parallel

相同的文件不会并行上传
This commit is contained in:
wdvxdr 2021-03-04 13:26:37 +08:00
parent f7b2235a99
commit 3f91730dc4
No known key found for this signature in database
GPG Key ID: 55FF1414A69CEBA6

View File

@ -8,6 +8,7 @@ import (
"math/rand" "math/rand"
"os" "os"
"runtime/debug" "runtime/debug"
"sync"
"google.golang.org/protobuf/proto" "google.golang.org/protobuf/proto"
@ -54,6 +55,8 @@ type (
} }
) )
var fileSingleFlight = sync.Map{}
func init() { func init() {
decoders["OidbSvc.0x6d8_1"] = decodeOIDB6d81Response decoders["OidbSvc.0x6d8_1"] = decodeOIDB6d81Response
decoders["OidbSvc.0x6d6_0"] = decodeOIDB6d60Response decoders["OidbSvc.0x6d6_0"] = decodeOIDB6d60Response
@ -156,6 +159,17 @@ func (fs *GroupFileSystem) GetFilesByFolder(folderId string) ([]*GroupFile, []*G
} }
func (fs *GroupFileSystem) UploadFile(p, name, folderId string) error { func (fs *GroupFileSystem) UploadFile(p, name, folderId string) error {
// 同文件等待其他线程上传
if wg, ok := fileSingleFlight.Load(p); ok {
wg.(*sync.WaitGroup).Wait()
} else {
wg := &sync.WaitGroup{}
wg.Add(1)
fileSingleFlight.Store(p, wg)
defer wg.Done()
defer fileSingleFlight.Delete(p)
}
file, err := os.OpenFile(p, os.O_RDONLY, 0666) file, err := os.OpenFile(p, os.O_RDONLY, 0666)
if err != nil { if err != nil {
return errors.Wrap(err, "open file error") return errors.Wrap(err, "open file error")