1
- import GUI from "./gui.js" ;
2
1
import CONFIGURATOR from "./data_storage.js" ;
3
2
import { serial } from "./serial.js" ;
4
3
@@ -58,9 +57,9 @@ const MSP = {
58
57
packet_error : 0 ,
59
58
unsupported : 0 ,
60
59
61
- MIN_TIMEOUT : 200 ,
62
- MAX_TIMEOUT : 2000 ,
63
- timeout : 200 ,
60
+ MIN_TIMEOUT : 1 , // Very fast timeout
61
+ MAX_TIMEOUT : 200 , // Upper bound for retries
62
+ timeout : 1 , // Start with 1ms
64
63
65
64
last_received_timestamp : null ,
66
65
listeners : [ ] ,
@@ -71,10 +70,9 @@ const MSP = {
71
70
cli_output : [ ] ,
72
71
cli_callback : null ,
73
72
74
- // Add retry configuration
75
- MAX_RETRIES : 10 ,
73
+ // Simplified retry configuration
74
+ MAX_RETRIES : 10 , // Fixed retry limit
76
75
MAX_QUEUE_SIZE : 50 ,
77
- MIN_RETRIES : 3 , // Minimum retries when queue is healthy
78
76
79
77
read ( readInfo ) {
80
78
if ( CONFIGURATOR . virtualMode ) {
@@ -409,12 +407,12 @@ const MSP = {
409
407
410
408
const requestObj = {
411
409
code,
412
- requestKey, // Add the unique key to the request object
410
+ requestKey,
413
411
requestBuffer : bufferOut ,
414
412
callback : callback_msp ,
415
413
callbackOnError : doCallbackOnError ,
416
414
start : performance . now ( ) ,
417
- attempts : 0 , // Initialize retry counter
415
+ attempts : 0 ,
418
416
} ;
419
417
420
418
// Track only the first outstanding request for a given key
@@ -425,16 +423,9 @@ const MSP = {
425
423
426
424
// Send message if it has data or is a new request
427
425
if ( data || ! isDuplicateRequest ) {
428
- // Simple adaptive timeout - decrease on success, increase on timeout
429
426
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
- }
427
+ if ( sendInfo . bytesSent === bufferOut . byteLength && callback_sent ) {
428
+ callback_sent ( ) ;
438
429
}
439
430
} ) ;
440
431
}
@@ -448,83 +439,43 @@ const MSP = {
448
439
} , this . timeout ) ;
449
440
} ,
450
441
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
463
- } ,
464
-
465
442
_handleTimeout ( requestObj , bufferOut ) {
466
- // Increase timeout on failure for better reliability
467
- this . timeout = Math . min ( this . MAX_TIMEOUT , this . timeout + 50 ) ;
468
-
469
443
// Increment retry attempts
470
444
requestObj . attempts ++ ;
471
445
472
- const dynamicMaxRetries = this . _getDynamicMaxRetries ( ) ;
473
-
474
446
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 } ` ,
447
+ `MSP: data request timed-out: ${ requestObj . code } ` +
448
+ `QUEUE: ${ this . callbacks . length } /${ this . MAX_QUEUE_SIZE } ` +
449
+ `ATTEMPTS: ${ requestObj . attempts } /${ this . MAX_RETRIES } ` ,
479
450
) ;
480
451
481
452
// Check if max retries exceeded OR queue is too large
482
- if ( requestObj . attempts >= dynamicMaxRetries || this . callbacks . length > this . MAX_QUEUE_SIZE ) {
453
+ if ( requestObj . attempts >= this . MAX_RETRIES || this . callbacks . length > this . MAX_QUEUE_SIZE ) {
483
454
const reason =
484
- requestObj . attempts >= dynamicMaxRetries
485
- ? `max retries (${ dynamicMaxRetries } )`
486
- : `queue overflow (${ this . callbacks . length } /${ this . MAX_QUEUE_SIZE } )` ;
455
+ requestObj . attempts >= this . MAX_RETRIES ? `max retries (${ this . MAX_RETRIES } )` : `queue overflow` ;
487
456
488
457
console . error ( `MSP: Request ${ requestObj . code } exceeded ${ reason } , giving up` ) ;
489
-
490
- // Remove from callbacks to prevent memory leak
491
458
this . _removeRequestFromCallbacks ( requestObj ) ;
492
459
493
- // Call error callback if available
494
460
if ( requestObj . callbackOnError && requestObj . callback ) {
495
461
requestObj . callback ( ) ;
496
462
}
497
-
498
- return ; // Stop retrying
463
+ return ;
499
464
}
500
465
501
466
// Clear the existing timer before retry
502
467
clearTimeout ( requestObj . timer ) ;
503
468
504
- // Reset start time for this retry attempt
505
- requestObj . start = performance . now ( ) ;
506
-
507
469
serial . send ( bufferOut , ( sendInfo ) => {
508
470
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 ) ;
471
+ // Use exponential backoff for retries: 1ms, 2ms, 4ms, 8ms...
472
+ const retryTimeout = Math . min ( this . MAX_TIMEOUT , this . timeout * Math . pow ( 2 , requestObj . attempts - 1 ) ) ;
473
+ requestObj . timer = setTimeout ( ( ) => {
474
+ this . _handleTimeout ( requestObj , bufferOut ) ;
475
+ } , retryTimeout ) ;
518
476
} 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
-
477
+ console . error ( `MSP: Failed to send retry for request ${ requestObj . code } ` ) ;
525
478
this . _removeRequestFromCallbacks ( requestObj ) ;
526
-
527
- // Call error callback if available
528
479
if ( requestObj . callbackOnError && requestObj . callback ) {
529
480
requestObj . callback ( ) ;
530
481
}
0 commit comments