fix(uploader, files/handlers): fix incorrect unlock, catch and check error after calling api (#26)

* fix(uploader, files/handlers): fix incorrect unlock, catch and check after calling api

* fix(uploader): fix uploader test
This commit is contained in:
Hexxa 2020-12-21 23:20:06 +08:00 committed by GitHub
parent 64493ec76a
commit cb26003b99
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
3 changed files with 32 additions and 11 deletions

View file

@ -109,15 +109,32 @@ export class FileUploader {
} else { } else {
this.errMsg = uploadResp.statusText; this.errMsg = uploadResp.statusText;
this.chunkLen = Math.ceil(this.chunkLen * speedDownRatio); this.chunkLen = Math.ceil(this.chunkLen * speedDownRatio);
const uploadStatusResp = await this.uploadStatus(this.filePath);
let uploadStatusResp: Response<UploadStatusResp> = undefined;
try {
uploadStatusResp = await this.uploadStatus(this.filePath);
} catch (e) {
if (uploadStatusResp == null) {
this.errMsg = `${this.errMsg}; unknown error: empty uploadStatus response`;
break;
} else if (uploadStatusResp.status === 500) {
if (
!uploadStatusResp.statusText.includes("fail to lock the file") &&
uploadStatusResp.statusText !== ""
) {
this.errMsg = `${this.errMsg}; unknown error: ${uploadStatusResp.statusText}`;
break;
}
} else if (uploadStatusResp.status === 600) {
this.errMsg = `${this.errMsg}; unknown error: ${uploadStatusResp.statusText}`;
break;
} else {
// ignore error and retry
}
}
if (uploadStatusResp.status === 200) { if (uploadStatusResp.status === 200) {
this.offset = uploadStatusResp.data.uploaded; this.offset = uploadStatusResp.data.uploaded;
} else if (uploadStatusResp.status === 600) {
this.errMsg = "unknown error";
break;
} else {
// do nothing and retry
} }
} }

View file

@ -76,14 +76,17 @@ func (h *FileHandlers) NewAutoLocker(c *gin.Context, key string) *AutoLocker {
func (lk *AutoLocker) Exec(handler func()) { func (lk *AutoLocker) Exec(handler func()) {
var err error var err error
kv := lk.h.deps.KV() kv := lk.h.deps.KV()
locked := false
defer func() { defer func() {
if p := recover(); p != nil { if p := recover(); p != nil {
fmt.Println(p) fmt.Println(p)
} }
if locked {
if err = kv.Unlock(lk.key); err != nil { if err = kv.Unlock(lk.key); err != nil {
fmt.Println(err) fmt.Println(err)
} }
}
}() }()
if err = kv.TryLock(lk.key); err != nil { if err = kv.TryLock(lk.key); err != nil {
@ -91,6 +94,7 @@ func (lk *AutoLocker) Exec(handler func()) {
return return
} }
locked = true
handler() handler()
} }

View file

@ -234,11 +234,11 @@ func TestFileHandlers(t *testing.T) {
t.Run("test dirs APIs: Mkdir-Create-UploadChunk-List", func(t *testing.T) { t.Run("test dirs APIs: Mkdir-Create-UploadChunk-List", func(t *testing.T) {
for dirPath, files := range map[string]map[string]string{ for dirPath, files := range map[string]map[string]string{
"dir/path1/": map[string]string{ "dir/path1": map[string]string{
"f1.md": "11111", "f1.md": "11111",
"f2.md": "22222222222", "f2.md": "22222222222",
}, },
"dir/path1/path2": map[string]string{ "dir/path2/path2": map[string]string{
"f3.md": "3333333", "f3.md": "3333333",
}, },
} { } {