Skip to content

Commit f7cac8f

Browse files
committed
Improve duplicate message detection by also checking payload
1 parent 88bf161 commit f7cac8f

File tree

1 file changed

+30
-5
lines changed

1 file changed

+30
-5
lines changed

src/js/msp.js

Lines changed: 30 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -378,6 +378,21 @@ const MSP = {
378378

379379
serial.send(bufferOut);
380380
},
381+
// Helper function to create a unique key for request identification
382+
_createRequestKey(code, data) {
383+
if (!data || data.length === 0) {
384+
return `${code}:empty`;
385+
}
386+
387+
// Create a simple hash of the data
388+
let hash = 0;
389+
for (let i = 0; i < data.length; i++) {
390+
hash = ((hash << 5) - hash + data[i]) & 0xffffffff;
391+
}
392+
393+
return `${code}:${hash}`;
394+
},
395+
381396
send_message(code, data, callback_sent, callback_msp, doCallbackOnError) {
382397
if (code === undefined || !serial.connected || CONFIGURATOR.virtualMode) {
383398
if (callback_msp) {
@@ -386,19 +401,23 @@ const MSP = {
386401
return false;
387402
}
388403

389-
const isDuplicateRequest = this.callbacks.some((instance) => instance.code === code);
404+
// Create unique key combining code and data
405+
const requestKey = this._createRequestKey(code, data);
406+
const isDuplicateRequest = this.callbacks.some((instance) => instance.requestKey === requestKey);
407+
390408
const bufferOut = code <= 254 ? this.encode_message_v1(code, data) : this.encode_message_v2(code, data);
391409

392410
const requestObj = {
393411
code,
412+
requestKey, // Add the unique key to the request object
394413
requestBuffer: bufferOut,
395414
callback: callback_msp,
396415
callbackOnError: doCallbackOnError,
397416
start: performance.now(),
398417
attempts: 0, // Initialize retry counter
399418
};
400419

401-
// Track only the first outstanding request for a given code
420+
// Track only the first outstanding request for a given key
402421
if (!isDuplicateRequest) {
403422
this._setupTimeout(requestObj, bufferOut);
404423
this.callbacks.push(requestObj);
@@ -431,9 +450,15 @@ const MSP = {
431450

432451
_getDynamicMaxRetries() {
433452
// 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
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
437462
return this.MAX_RETRIES; // Full retries when queue is healthy
438463
},
439464

0 commit comments

Comments
 (0)