-
-
Notifications
You must be signed in to change notification settings - Fork 1k
Improve msp #4632
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Improve msp #4632
Changes from 1 commit
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -58,9 +58,7 @@ const MSP = { | |
| packet_error: 0, | ||
| unsupported: 0, | ||
|
|
||
| MIN_TIMEOUT: 200, | ||
| MAX_TIMEOUT: 2000, | ||
| timeout: 200, | ||
| TIMEOUT: 1000, | ||
|
|
||
| last_received_timestamp: null, | ||
| listeners: [], | ||
|
|
@@ -289,7 +287,7 @@ const MSP = { | |
| }); | ||
| }, | ||
| listen(listener) { | ||
| if (this.listeners.indexOf(listener) == -1) { | ||
| if (this.listeners.indexOf(listener) === -1) { | ||
| this.listeners.push(listener); | ||
| } | ||
| }, | ||
|
|
@@ -318,8 +316,8 @@ const MSP = { | |
| const dataLength = data ? data.length : 0; | ||
| // always reserve 6 bytes for protocol overhead ! | ||
| const bufferSize = dataLength + 6; | ||
| let bufferOut = new ArrayBuffer(bufferSize); | ||
| let bufView = new Uint8Array(bufferOut); | ||
| const bufferOut = new ArrayBuffer(bufferSize); | ||
| const bufView = new Uint8Array(bufferOut); | ||
|
|
||
| bufView[0] = 36; // $ | ||
| bufView[1] = 77; // M | ||
|
|
@@ -378,25 +376,22 @@ const MSP = { | |
| serial.send(bufferOut); | ||
| }, | ||
| send_message(code, data, callback_sent, callback_msp, doCallbackOnError) { | ||
| const connected = serial.connected; | ||
|
|
||
| if (code === undefined || !connected || CONFIGURATOR.virtualMode) { | ||
| if (code === undefined || !serial.connected || CONFIGURATOR.virtualMode) { | ||
| if (callback_msp) { | ||
| callback_msp(); | ||
| } | ||
| return false; | ||
| } | ||
|
|
||
| let requestExists = false; | ||
| for (const instance of this.callbacks) { | ||
| if (instance.code === code) { | ||
| requestExists = true; | ||
|
|
||
| break; | ||
| } | ||
| } | ||
|
|
||
| const bufferOut = code <= 254 ? this.encode_message_v1(code, data) : this.encode_message_v2(code, data); | ||
| const view = new Uint8Array(bufferOut); | ||
| const keyCrc = this.crc8_dvb_s2_data(view, 0, view.length); | ||
| const requestExists = this.callbacks.some( | ||
| (i) => | ||
| i.code === code && | ||
| i.requestBuffer?.byteLength === bufferOut.byteLength && | ||
| this.crc8_dvb_s2_data(new Uint8Array(i.requestBuffer), 0, i.requestBuffer.byteLength) === keyCrc, | ||
| ); | ||
|
|
||
| const obj = { | ||
| code: code, | ||
|
|
@@ -409,31 +404,23 @@ const MSP = { | |
| if (!requestExists) { | ||
| obj.timer = setTimeout(() => { | ||
| console.warn( | ||
| `MSP: data request timed-out: ${code} ID: ${serial.connectionId} TAB: ${GUI.active_tab} TIMEOUT: ${ | ||
| this.timeout | ||
| } QUEUE: ${this.callbacks.length} (${this.callbacks.map((e) => e.code)})`, | ||
| `MSP: data request timed-out: ${code} ID: ${serial.connectionId} TAB: ${GUI.active_tab} QUEUE: ${this.callbacks.length} (${this.callbacks.map((e) => e.code)})`, | ||
| ); | ||
| serial.send(bufferOut, (_sendInfo) => { | ||
| obj.stop = performance.now(); | ||
| const executionTime = Math.round(obj.stop - obj.start); | ||
| this.timeout = Math.max(this.MIN_TIMEOUT, Math.min(executionTime, this.MAX_TIMEOUT)); | ||
| serial.send(bufferOut, (sendInfo) => { | ||
| if (sendInfo.bytesSent === bufferOut.byteLength && callback_sent) { | ||
| callback_sent(); | ||
| } | ||
| }); | ||
| }, this.timeout); | ||
| }, this.TIMEOUT); | ||
| } | ||
|
Comment on lines
403
to
414
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 🧩 Analysis chainTimeout currently triggers an unconditional resend; high risk of duplicate writes and log noise.
Prefer failing the request on timeout (and clearing it) instead of resending here. If you must retry, gate it to read‑only (no‑payload) requests and ensure timers are canceled on response. - if (!requestExists) {
- obj.timer = setTimeout(() => {
- console.warn(
- `MSP: data request timed-out: ${code} ID: ${serial.connectionId} TAB: ${GUI.active_tab} QUEUE: ${this.callbacks.length} (${this.callbacks.map((e) => e.code)})`,
- );
- serial.send(bufferOut, (sendInfo) => {
- if (sendInfo.bytesSent === bufferOut.byteLength && callback_sent) {
- callback_sent();
- }
- });
- }, this.TIMEOUT);
- }
+ if (!requestExists) {
+ obj.timer = setTimeout(() => {
+ console.warn(
+ `MSP: data request timed-out: ${code} ID: ${serial.connectionId} TAB: ${GUI.active_tab} QUEUE: ${this.callbacks.length} (${this.callbacks.map((e) => e.code)})`,
+ );
+ // Drop the request and signal an error
+ const idx = this.callbacks.indexOf(obj);
+ if (idx !== -1) this.callbacks.splice(idx, 1);
+ if (obj.callback) obj.callback(undefined);
+ }, this.TIMEOUT_MS ?? this.TIMEOUT);
+ }Additionally, ensure timers are cleared when a matching response arrives (see suggestion to resolve/purge callbacks on receive). I can draft that wiring if desired. 🏁 Script executed: #!/bin/bash
# Verify any code clears per-request timers on response
rg -n -C3 --type=js 'clearTimeout\(|callbacks\.(splice|shift|filter)\('Length of output: 10870 🏁 Script executed: #!/bin/bash
rg -n clearTimeout src/js/msp.jsLength of output: 45 Eliminate unguarded retry on timeout and clear per-request timers on response
🤖 Prompt for AI Agents |
||
|
|
||
| this.callbacks.push(obj); | ||
|
|
||
| // always send messages with data payload (even when there is a message already in the queue) | ||
| if (data || !requestExists) { | ||
| if (this.timeout > this.MIN_TIMEOUT) { | ||
| this.timeout--; | ||
| } | ||
|
|
||
| serial.send(bufferOut, (sendInfo) => { | ||
| if (sendInfo.bytesSent === bufferOut.byteLength) { | ||
| if (callback_sent) { | ||
| callback_sent(); | ||
| } | ||
| if (sendInfo.bytesSent === bufferOut.byteLength && callback_sent) { | ||
| callback_sent(); | ||
| } | ||
| }); | ||
| } | ||
|
|
||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
🛠️ Refactor suggestion | 🟠 Major
❓ Verification inconclusive
Dead/legacy parameter: doCallbackOnError.
This parameter was previously removed as unused; it’s reintroduced here and still not consumed elsewhere (only stored on obj). Drop it to reduce API surface and confusion.
Based on learnings
Please confirm there are no call sites passing the 5th arg.
🏁 Script executed:
Length of output: 108
🏁 Script executed:
Length of output: 187
🏁 Script executed:
Length of output: 0
Remove unused doCallbackOnError parameter from send_message
The parameter is never passed or used—drop it to simplify the API in src/js/msp.js:
📝 Committable suggestion
🤖 Prompt for AI Agents