Skip to content

Commit 3dd8ae1

Browse files
committed
Mitigate failed retry attempts
1 parent 9a05f3a commit 3dd8ae1

File tree

1 file changed

+29
-6
lines changed

1 file changed

+29
-6
lines changed

src/js/msp.js

Lines changed: 29 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -430,14 +430,37 @@ const MSP = {
430430
// Clear the existing timer before retry
431431
clearTimeout(requestObj.timer);
432432

433-
serial.send(bufferOut, (_sendInfo) => {
434-
requestObj.stop = performance.now();
435-
const executionTime = Math.round(requestObj.stop - requestObj.start);
436-
this.timeout = Math.max(this.MIN_TIMEOUT, Math.min(executionTime, this.MAX_TIMEOUT));
433+
serial.send(bufferOut, (sendInfo) => {
434+
if (sendInfo.bytesSent === bufferOut.byteLength) {
435+
// Successfully sent retry
436+
requestObj.stop = performance.now();
437+
const executionTime = Math.round(requestObj.stop - requestObj.start);
438+
this.timeout = Math.max(this.MIN_TIMEOUT, Math.min(executionTime, this.MAX_TIMEOUT));
439+
440+
// Re-arm the timeout for retry attempts
441+
this._setupTimeout(requestObj, bufferOut);
442+
} else {
443+
// Failed to send retry - remove request and handle error
444+
console.error(
445+
`MSP: Failed to send retry for request ${requestObj.code}: ` +
446+
`sent ${sendInfo.bytesSent}/${bufferOut.byteLength} bytes`,
447+
);
448+
449+
this._removeRequestFromCallbacks(requestObj);
450+
451+
// Call error callback if available
452+
if (requestObj.callbackOnError && requestObj.callback) {
453+
requestObj.callback();
454+
}
455+
}
437456
});
457+
},
438458

439-
// Re-arm the timeout for retry attempts
440-
this._setupTimeout(requestObj, bufferOut);
459+
_removeRequestFromCallbacks(requestObj) {
460+
const index = this.callbacks.indexOf(requestObj);
461+
if (index > -1) {
462+
this.callbacks.splice(index, 1);
463+
}
441464
},
442465

443466
/**

0 commit comments

Comments
 (0)