v2.0.8: retry до 3 раз, чанки 60KB, меньше запросов

This commit is contained in:
2026-06-18 07:17:52 +04:00
parent 627c0178d1
commit 09d32951de
+24 -13
View File
@@ -60,7 +60,7 @@
<body> <body>
<div class="topbar"> <div class="topbar">
<img src="{{ url_for('static', filename='nubes-logo.svg') }}" alt="Nubes"> <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>
<div class="content"> <div class="content">
@@ -172,21 +172,35 @@
} }
var b64 = btoa(b64Chunks.join('')); var b64 = btoa(b64Chunks.join(''));
var checksum = SparkMD5.ArrayBuffer.hash(buffer); 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 totalChunks = Math.ceil(b64.length / CHUNK);
var uploadId = 'u' + Date.now().toString(36) + Math.random().toString(36).substr(2, 7); var uploadId = 'u' + Date.now().toString(36) + Math.random().toString(36).substr(2, 7);
var done = 0; var done = 0;
console.log('uploadFileChunked: ' + file.name + ' b64=' + b64.length + ' chunks=' + totalChunks + ' uploadId=' + uploadId); 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) { function sendChunk(i) {
if (i >= totalChunks) { if (i >= totalChunks) {
console.log('finalize: ' + uploadId); console.log('finalize: ' + uploadId);
fetch('/v2/finalize', { fetchWithRetry('/v2/finalize', {upload_id: uploadId, filename: file.name, total: totalChunks, checksum: checksum})
method: 'POST', .then(function(r) {
headers: {'Content-Type': 'application/json'},
body: JSON.stringify({upload_id: uploadId, filename: file.name, total: totalChunks, checksum: checksum})
}).then(function(r) {
if (!r.ok) throw new Error('HTTP ' + r.status); if (!r.ok) throw new Error('HTTP ' + r.status);
return r.json(); return r.json();
}).then(function(data) { }).then(function(data) {
@@ -197,18 +211,15 @@
} }
var piece = b64.substring(i * CHUNK, (i + 1) * CHUNK); var piece = b64.substring(i * CHUNK, (i + 1) * CHUNK);
console.log('chunk ' + i + '/' + totalChunks + ' send ' + piece.length + ' bytes'); console.log('chunk ' + i + '/' + totalChunks + ' send ' + piece.length + ' bytes');
fetch('/v2/chunk', { fetchWithRetry('/v2/chunk', {upload_id: uploadId, index: i, total: totalChunks, data: piece})
method: 'POST', .then(function(r) {
headers: {'Content-Type': 'application/json'},
body: JSON.stringify({upload_id: uploadId, index: i, total: totalChunks, data: piece})
}).then(function(r) {
if (!r.ok) throw new Error('HTTP ' + r.status); if (!r.ok) throw new Error('HTTP ' + r.status);
return r.json(); return r.json();
}).then(function(data) { }).then(function(data) {
if (!data.ok) { reject(new Error(data.error)); return; } if (!data.ok) { reject(new Error(data.error)); return; }
done++; done++;
if (onProgress) onProgress({pct: Math.round(done / totalChunks * 100), done: done, total: totalChunks}); 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); }); }).catch(function(e) { reject(e); });
} }
sendChunk(0); sendChunk(0);