v2.0.8: retry до 3 раз, чанки 60KB, меньше запросов
This commit is contained in:
+24
-13
@@ -60,7 +60,7 @@
|
||||
<body>
|
||||
<div class="topbar">
|
||||
<img src="{{ url_for('static', filename='nubes-logo.svg') }}" alt="Nubes">
|
||||
<span class="title">Сверка договоров <span style="font-weight:400;color:var(--muted);font-size:12px;">v2.0.7</span></span>
|
||||
<span class="title">Сверка договоров <span style="font-weight:400;color:var(--muted);font-size:12px;">v2.0.8</span></span>
|
||||
</div>
|
||||
|
||||
<div class="content">
|
||||
@@ -172,21 +172,35 @@
|
||||
}
|
||||
var b64 = btoa(b64Chunks.join(''));
|
||||
var checksum = SparkMD5.ArrayBuffer.hash(buffer);
|
||||
var CHUNK = 55 * 1024; // меньше запросов → меньше HTTP/2 ошибок
|
||||
var CHUNK = 60 * 1024; // ближе к лимиту 64KB → меньше чанков
|
||||
var totalChunks = Math.ceil(b64.length / CHUNK);
|
||||
var uploadId = 'u' + Date.now().toString(36) + Math.random().toString(36).substr(2, 7);
|
||||
var done = 0;
|
||||
|
||||
console.log('uploadFileChunked: ' + file.name + ' b64=' + b64.length + ' chunks=' + totalChunks + ' uploadId=' + uploadId);
|
||||
|
||||
function fetchWithRetry(url, body, retries) {
|
||||
retries = retries || 3;
|
||||
return fetch(url, {
|
||||
method: 'POST',
|
||||
headers: {'Content-Type': 'application/json'},
|
||||
body: JSON.stringify(body)
|
||||
}).catch(function(e) {
|
||||
if (retries > 0) {
|
||||
console.log('retry ' + url + ' (' + retries + ' left): ' + e.message);
|
||||
return new Promise(function(r) { setTimeout(r, 1000); }).then(function() {
|
||||
return fetchWithRetry(url, body, retries - 1);
|
||||
});
|
||||
}
|
||||
throw e;
|
||||
});
|
||||
}
|
||||
|
||||
function sendChunk(i) {
|
||||
if (i >= totalChunks) {
|
||||
console.log('finalize: ' + uploadId);
|
||||
fetch('/v2/finalize', {
|
||||
method: 'POST',
|
||||
headers: {'Content-Type': 'application/json'},
|
||||
body: JSON.stringify({upload_id: uploadId, filename: file.name, total: totalChunks, checksum: checksum})
|
||||
}).then(function(r) {
|
||||
fetchWithRetry('/v2/finalize', {upload_id: uploadId, filename: file.name, total: totalChunks, checksum: checksum})
|
||||
.then(function(r) {
|
||||
if (!r.ok) throw new Error('HTTP ' + r.status);
|
||||
return r.json();
|
||||
}).then(function(data) {
|
||||
@@ -197,18 +211,15 @@
|
||||
}
|
||||
var piece = b64.substring(i * CHUNK, (i + 1) * CHUNK);
|
||||
console.log('chunk ' + i + '/' + totalChunks + ' send ' + piece.length + ' bytes');
|
||||
fetch('/v2/chunk', {
|
||||
method: 'POST',
|
||||
headers: {'Content-Type': 'application/json'},
|
||||
body: JSON.stringify({upload_id: uploadId, index: i, total: totalChunks, data: piece})
|
||||
}).then(function(r) {
|
||||
fetchWithRetry('/v2/chunk', {upload_id: uploadId, index: i, total: totalChunks, data: piece})
|
||||
.then(function(r) {
|
||||
if (!r.ok) throw new Error('HTTP ' + r.status);
|
||||
return r.json();
|
||||
}).then(function(data) {
|
||||
if (!data.ok) { reject(new Error(data.error)); return; }
|
||||
done++;
|
||||
if (onProgress) onProgress({pct: Math.round(done / totalChunks * 100), done: done, total: totalChunks});
|
||||
setTimeout(function() { sendChunk(i + 1); }, 3000); // дать HTTP/2 стримам закрыться
|
||||
setTimeout(function() { sendChunk(i + 1); }, 3000);
|
||||
}).catch(function(e) { reject(e); });
|
||||
}
|
||||
sendChunk(0);
|
||||
|
||||
Reference in New Issue
Block a user