feat: server-side ACK flow control (chunk→ack→next), bump 1.0.17
Deploy loadtest / validate (push) Waiting to run
Deploy loadtest / validate (push) Waiting to run
This commit is contained in:
+3
-2
@@ -7,7 +7,7 @@ from flask_sock import Sock
|
|||||||
app = Flask(__name__)
|
app = Flask(__name__)
|
||||||
sock = Sock(app)
|
sock = Sock(app)
|
||||||
|
|
||||||
VERSION = '1.0.16'
|
VERSION = '1.0.17'
|
||||||
|
|
||||||
|
|
||||||
@app.route('/')
|
@app.route('/')
|
||||||
@@ -22,7 +22,7 @@ def health():
|
|||||||
|
|
||||||
@sock.route('/ws-upload')
|
@sock.route('/ws-upload')
|
||||||
def ws_upload(ws):
|
def ws_upload(ws):
|
||||||
"""WebSocket: получает чанки, считает байты и время"""
|
"""WebSocket: получает чанки, ACK после каждого, считает байты и время"""
|
||||||
t0 = time.time()
|
t0 = time.time()
|
||||||
total = 0
|
total = 0
|
||||||
while True:
|
while True:
|
||||||
@@ -32,6 +32,7 @@ def ws_upload(ws):
|
|||||||
if isinstance(chunk, str) and chunk == 'DONE':
|
if isinstance(chunk, str) and chunk == 'DONE':
|
||||||
break
|
break
|
||||||
total += len(chunk)
|
total += len(chunk)
|
||||||
|
ws.send(json.dumps({'ack': total}))
|
||||||
elapsed = round((time.time() - t0) * 1000)
|
elapsed = round((time.time() - t0) * 1000)
|
||||||
ws.send(json.dumps({'ok': True, 'bytes': total, 'elapsed_ms': elapsed}))
|
ws.send(json.dumps({'ok': True, 'bytes': total, 'elapsed_ms': elapsed}))
|
||||||
|
|
||||||
|
|||||||
+23
-26
@@ -211,28 +211,8 @@ function uploadFileWS(url, f, idx) {
|
|||||||
ws.onopen = function () {
|
ws.onopen = function () {
|
||||||
var reader = new FileReader();
|
var reader = new FileReader();
|
||||||
|
|
||||||
function sendOrWait(data) {
|
|
||||||
if (ws.bufferedAmount > 256 * 1024) {
|
|
||||||
setTimeout(function () { sendOrWait(data); }, 10);
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
ws.send(data);
|
|
||||||
offset += CHUNK;
|
|
||||||
if (offset < f.size) {
|
|
||||||
var pct = Math.round(offset / f.size * 100);
|
|
||||||
if (pct - lastProgress >= 10) {
|
|
||||||
lastProgress = pct;
|
|
||||||
f.progress = pct;
|
|
||||||
renderTable();
|
|
||||||
}
|
|
||||||
readNext();
|
|
||||||
} else {
|
|
||||||
ws.send('DONE');
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
reader.onload = function (e) {
|
reader.onload = function (e) {
|
||||||
sendOrWait(e.target.result);
|
ws.send(e.target.result);
|
||||||
};
|
};
|
||||||
function readNext() {
|
function readNext() {
|
||||||
var end = Math.min(offset + CHUNK, f.size);
|
var end = Math.min(offset + CHUNK, f.size);
|
||||||
@@ -244,11 +224,28 @@ function uploadFileWS(url, f, idx) {
|
|||||||
ws.onmessage = function (e) {
|
ws.onmessage = function (e) {
|
||||||
try {
|
try {
|
||||||
var r = JSON.parse(e.data);
|
var r = JSON.parse(e.data);
|
||||||
clearTimeout(timer);
|
if (r.ack) {
|
||||||
f.progress = 100;
|
// Сервер подтвердил получение N байт
|
||||||
renderTable();
|
offset = r.ack;
|
||||||
ws.close();
|
var pct = Math.round(offset / f.size * 100);
|
||||||
resolve({ bytes: r.bytes, elapsed_ms: Date.now() - startTime });
|
if (pct - lastProgress >= 10) {
|
||||||
|
lastProgress = pct;
|
||||||
|
f.progress = pct;
|
||||||
|
renderTable();
|
||||||
|
}
|
||||||
|
if (offset < f.size) {
|
||||||
|
readNext();
|
||||||
|
} else {
|
||||||
|
ws.send('DONE');
|
||||||
|
}
|
||||||
|
} else if (r.ok) {
|
||||||
|
// Финальный ответ
|
||||||
|
clearTimeout(timer);
|
||||||
|
f.progress = 100;
|
||||||
|
renderTable();
|
||||||
|
ws.close();
|
||||||
|
resolve({ bytes: r.bytes, elapsed_ms: Date.now() - startTime });
|
||||||
|
}
|
||||||
} catch (err) {
|
} catch (err) {
|
||||||
reject(new Error('Bad response'));
|
reject(new Error('Bad response'));
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user