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