fix(fe/chunk_uploader): improve robustness of chunk uploader

This commit is contained in:
hexxa 2022-01-09 21:19:30 +08:00 committed by Hexxa
parent aa56b935a2
commit 33ca87cf2c
2 changed files with 11 additions and 18 deletions

View file

@ -16,7 +16,7 @@ const chunkLimit = 1024 * 1024 * 50; // 50MB
const createRetryLimit = 1024; const createRetryLimit = 1024;
const uploadRetryLimit = 1024; const uploadRetryLimit = 1024;
const readRetryLimit = 8; const readRetryLimit = 8;
const backoffMax = 2000; const backoffMax = 5000;
export interface ReaderResult { export interface ReaderResult {
chunk?: string; chunk?: string;
@ -56,9 +56,9 @@ export class ChunkUploader {
}; };
} }
} catch (e) { } catch (e) {
await this.backOff();
console.error(e); console.error(e);
} }
await this.backOff();
} }
return { return {
@ -130,22 +130,12 @@ export class ChunkUploader {
} }
try { try {
let uploadResp: Response<UploadStatusResp> = undefined; const uploadResp = await this.client.uploadChunk(
for (let i = 0; i < uploadRetryLimit; i++) {
uploadResp = await this.client.uploadChunk(
filePath, filePath,
result.chunk, result.chunk,
uploaded uploaded
); );
if (uploadResp.status === 200) {
break;
} else if (uploadResp.status !== 429) {
break;
}
await this.backOff();
}
if (uploadResp.status === 200 && uploadResp.data != null) { if (uploadResp.status === 200 && uploadResp.data != null) {
this.chunkLen = Math.ceil(this.chunkLen * speedUpRatio); this.chunkLen = Math.ceil(this.chunkLen * speedUpRatio);
return { return {
@ -194,6 +184,9 @@ export class ChunkUploader {
err: `failed to get upload status: ${uploadStatusResp.statusText}`, err: `failed to get upload status: ${uploadStatusResp.statusText}`,
}; };
} catch (e) { } catch (e) {
this.chunkLen = Math.ceil(this.chunkLen * speedDownRatio);
await this.backOff();
return { return {
filePath, filePath,
uploaded: uploaded, uploaded: uploaded,

View file

@ -4,8 +4,8 @@ import (
"fmt" "fmt"
"sync" "sync"
"github.com/ihexxa/quickshare/src/golimiter"
"github.com/ihexxa/quickshare/src/db/userstore" "github.com/ihexxa/quickshare/src/db/userstore"
"github.com/ihexxa/quickshare/src/golimiter"
) )
const cacheSizeLimit = 1024 const cacheSizeLimit = 1024