Skip to content

Commit 4fcad77

Browse files
committed
Remove dynamic timeout overhead
1 parent f7cac8f commit 4fcad77

File tree

1 file changed

+17
-71
lines changed

1 file changed

+17
-71
lines changed

src/js/msp.js

Lines changed: 17 additions & 71 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,3 @@
1-
import GUI from "./gui.js";
21
import CONFIGURATOR from "./data_storage.js";
32
import { serial } from "./serial.js";
43

@@ -58,10 +57,6 @@ const MSP = {
5857
packet_error: 0,
5958
unsupported: 0,
6059

61-
MIN_TIMEOUT: 200,
62-
MAX_TIMEOUT: 2000,
63-
timeout: 200,
64-
6560
last_received_timestamp: null,
6661
listeners: [],
6762

@@ -71,10 +66,10 @@ const MSP = {
7166
cli_output: [],
7267
cli_callback: null,
7368

74-
// Add retry configuration
69+
// Simplified retry configuration
7570
MAX_RETRIES: 10,
7671
MAX_QUEUE_SIZE: 50,
77-
MIN_RETRIES: 3, // Minimum retries when queue is healthy
72+
TIMEOUT: 1000,
7873

7974
read(readInfo) {
8075
if (CONFIGURATOR.virtualMode) {
@@ -409,12 +404,12 @@ const MSP = {
409404

410405
const requestObj = {
411406
code,
412-
requestKey, // Add the unique key to the request object
407+
requestKey,
413408
requestBuffer: bufferOut,
414409
callback: callback_msp,
415410
callbackOnError: doCallbackOnError,
416411
start: performance.now(),
417-
attempts: 0, // Initialize retry counter
412+
attempts: 0,
418413
};
419414

420415
// Track only the first outstanding request for a given key
@@ -425,16 +420,9 @@ const MSP = {
425420

426421
// Send message if it has data or is a new request
427422
if (data || !isDuplicateRequest) {
428-
// Simple adaptive timeout - decrease on success, increase on timeout
429423
serial.send(bufferOut, (sendInfo) => {
430-
if (sendInfo.bytesSent === bufferOut.byteLength) {
431-
// Success: gradually decrease timeout for faster response
432-
if (this.timeout > this.MIN_TIMEOUT) {
433-
this.timeout = Math.max(this.MIN_TIMEOUT, this.timeout - 5);
434-
}
435-
if (callback_sent) {
436-
callback_sent();
437-
}
424+
if (sendInfo.bytesSent === bufferOut.byteLength && callback_sent) {
425+
callback_sent();
438426
}
439427
});
440428
}
@@ -445,86 +433,44 @@ const MSP = {
445433
_setupTimeout(requestObj, bufferOut) {
446434
requestObj.timer = setTimeout(() => {
447435
this._handleTimeout(requestObj, bufferOut);
448-
}, this.timeout);
449-
},
450-
451-
_getDynamicMaxRetries() {
452-
// Reduce retries when queue is getting full to prevent resource exhaustion
453-
if (this.callbacks.length > 30) {
454-
return 1;
455-
} // Very aggressive when queue is nearly full
456-
if (this.callbacks.length > 20) {
457-
return 2;
458-
} // Moderate reduction
459-
if (this.callbacks.length > 10) {
460-
return 3;
461-
} // Slight reduction
462-
return this.MAX_RETRIES; // Full retries when queue is healthy
436+
}, this.TIMEOUT);
463437
},
464438

465439
_handleTimeout(requestObj, bufferOut) {
466-
// Increase timeout on failure for better reliability
467-
this.timeout = Math.min(this.MAX_TIMEOUT, this.timeout + 50);
468-
469440
// Increment retry attempts
470441
requestObj.attempts++;
471442

472-
const dynamicMaxRetries = this._getDynamicMaxRetries();
473-
474443
console.warn(
475-
`MSP: data request timed-out: ${requestObj.code} ID: ${serial.connectionId} ` +
476-
`TAB: ${GUI.active_tab} TIMEOUT: ${this.timeout} ` +
477-
`QUEUE: ${this.callbacks.length}/${this.MAX_QUEUE_SIZE} (${this.callbacks.map((e) => e.code)}) ` +
478-
`ATTEMPTS: ${requestObj.attempts}/${dynamicMaxRetries}`,
444+
`MSP: data request timed-out: ${requestObj.code} ` +
445+
`QUEUE: ${this.callbacks.length}/${this.MAX_QUEUE_SIZE} ` +
446+
`ATTEMPTS: ${requestObj.attempts}/${this.MAX_RETRIES}`,
479447
);
480448

481449
// Check if max retries exceeded OR queue is too large
482-
if (requestObj.attempts >= dynamicMaxRetries || this.callbacks.length > this.MAX_QUEUE_SIZE) {
450+
if (requestObj.attempts >= this.MAX_RETRIES || this.callbacks.length > this.MAX_QUEUE_SIZE) {
483451
const reason =
484-
requestObj.attempts >= dynamicMaxRetries
485-
? `max retries (${dynamicMaxRetries})`
486-
: `queue overflow (${this.callbacks.length}/${this.MAX_QUEUE_SIZE})`;
452+
requestObj.attempts >= this.MAX_RETRIES ? `max retries (${this.MAX_RETRIES})` : `queue overflow`;
487453

488454
console.error(`MSP: Request ${requestObj.code} exceeded ${reason}, giving up`);
489-
490-
// Remove from callbacks to prevent memory leak
491455
this._removeRequestFromCallbacks(requestObj);
492456

493-
// Call error callback if available
494457
if (requestObj.callbackOnError && requestObj.callback) {
495458
requestObj.callback();
496459
}
497-
498-
return; // Stop retrying
460+
return;
499461
}
500462

501463
// Clear the existing timer before retry
502464
clearTimeout(requestObj.timer);
503465

504-
// Reset start time for this retry attempt
505-
requestObj.start = performance.now();
506-
507466
serial.send(bufferOut, (sendInfo) => {
508467
if (sendInfo.bytesSent === bufferOut.byteLength) {
509-
// Successfully sent retry
510-
requestObj.stop = performance.now();
511-
const executionTime = Math.round(requestObj.stop - requestObj.start);
512-
// Reset baseline for next retry
513-
requestObj.start = requestObj.stop;
514-
this.timeout = Math.max(this.MIN_TIMEOUT, Math.min(executionTime, this.MAX_TIMEOUT));
515-
516-
// Re-arm the timeout for retry attempts
517-
this._setupTimeout(requestObj, bufferOut);
468+
requestObj.timer = setTimeout(() => {
469+
this._handleTimeout(requestObj, bufferOut);
470+
}, this.TIMEOUT);
518471
} else {
519-
// Failed to send retry - remove request and handle error
520-
console.error(
521-
`MSP: Failed to send retry for request ${requestObj.code}: ` +
522-
`sent ${sendInfo.bytesSent}/${bufferOut.byteLength} bytes`,
523-
);
524-
472+
console.error(`MSP: Failed to send retry for request ${requestObj.code}`);
525473
this._removeRequestFromCallbacks(requestObj);
526-
527-
// Call error callback if available
528474
if (requestObj.callbackOnError && requestObj.callback) {
529475
requestObj.callback();
530476
}

0 commit comments

Comments
 (0)