Skip to content

Commit 88bf161

Browse files
committed
Add dynamic retries
1 parent 072b49c commit 88bf161

File tree

1 file changed

+22
-5
lines changed

1 file changed

+22
-5
lines changed

src/js/msp.js

Lines changed: 22 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -73,6 +73,8 @@ const MSP = {
7373

7474
// Add retry configuration
7575
MAX_RETRIES: 10,
76+
MAX_QUEUE_SIZE: 50,
77+
MIN_RETRIES: 3, // Minimum retries when queue is healthy
7678

7779
read(readInfo) {
7880
if (CONFIGURATOR.virtualMode) {
@@ -427,23 +429,38 @@ const MSP = {
427429
}, this.timeout);
428430
},
429431

432+
_getDynamicMaxRetries() {
433+
// Reduce retries when queue is getting full to prevent resource exhaustion
434+
if (this.callbacks.length > 30) return 1; // Very aggressive when queue is nearly full
435+
if (this.callbacks.length > 20) return 2; // Moderate reduction
436+
if (this.callbacks.length > 10) return 3; // Slight reduction
437+
return this.MAX_RETRIES; // Full retries when queue is healthy
438+
},
439+
430440
_handleTimeout(requestObj, bufferOut) {
431441
// Increase timeout on failure for better reliability
432442
this.timeout = Math.min(this.MAX_TIMEOUT, this.timeout + 50);
433443

434444
// Increment retry attempts
435445
requestObj.attempts++;
436446

447+
const dynamicMaxRetries = this._getDynamicMaxRetries();
448+
437449
console.warn(
438450
`MSP: data request timed-out: ${requestObj.code} ID: ${serial.connectionId} ` +
439451
`TAB: ${GUI.active_tab} TIMEOUT: ${this.timeout} ` +
440-
`QUEUE: ${this.callbacks.length} (${this.callbacks.map((e) => e.code)}) ` +
441-
`ATTEMPTS: ${requestObj.attempts}/${this.MAX_RETRIES}`,
452+
`QUEUE: ${this.callbacks.length}/${this.MAX_QUEUE_SIZE} (${this.callbacks.map((e) => e.code)}) ` +
453+
`ATTEMPTS: ${requestObj.attempts}/${dynamicMaxRetries}`,
442454
);
443455

444-
// Check if max retries exceeded
445-
if (requestObj.attempts >= this.MAX_RETRIES) {
446-
console.error(`MSP: Request ${requestObj.code} exceeded max retries (${this.MAX_RETRIES}), giving up`);
456+
// Check if max retries exceeded OR queue is too large
457+
if (requestObj.attempts >= dynamicMaxRetries || this.callbacks.length > this.MAX_QUEUE_SIZE) {
458+
const reason =
459+
requestObj.attempts >= dynamicMaxRetries
460+
? `max retries (${dynamicMaxRetries})`
461+
: `queue overflow (${this.callbacks.length}/${this.MAX_QUEUE_SIZE})`;
462+
463+
console.error(`MSP: Request ${requestObj.code} exceeded ${reason}, giving up`);
447464

448465
// Remove from callbacks to prevent memory leak
449466
this._removeRequestFromCallbacks(requestObj);

0 commit comments

Comments
 (0)