Fix resource leak and CPU performance issues

- Fix integer underflow panic in LED color publisher by adding bounds checking
- Reduce screenshot capture frequency from 15 FPS to 5 FPS for better CPU performance
- Reduce WebSocket force-send frequency from 200ms to 500ms
- Fix WebSocket resource leak by properly cleaning up streams when connections end
- Add proper stream lifecycle management with is_running flag checks
- Ensure background tasks exit when streams are stopped

This resolves the issue where CPU usage remained above 100% after visiting
the LED strip configuration page, even when navigating to other pages.
This commit is contained in:
2025-07-04 21:49:05 +08:00
parent a10fae75d2
commit 5da81e5f93
3 changed files with 48 additions and 11 deletions

View File

@ -296,8 +296,19 @@ impl LedColorsPublisher {
let mut buffer = Vec::<u8>::with_capacity(group_size * 3);
if group.end > group.start {
for i in group.pos - display_led_offset..group_size + group.pos - display_led_offset
{
// Prevent integer underflow by using saturating subtraction
let start_index = if group.pos >= display_led_offset {
group.pos - display_led_offset
} else {
0
};
let end_index = if group.pos + group_size >= display_led_offset {
group_size + group.pos - display_led_offset
} else {
0
};
for i in start_index..end_index {
if i < colors.len() {
let bytes = colors[i].as_bytes();
buffer.append(&mut bytes.to_vec());
@ -308,10 +319,19 @@ impl LedColorsPublisher {
}
}
} else {
for i in (group.pos - display_led_offset
..group_size + group.pos - display_led_offset)
.rev()
{
// Prevent integer underflow by using saturating subtraction
let start_index = if group.pos >= display_led_offset {
group.pos - display_led_offset
} else {
0
};
let end_index = if group.pos + group_size >= display_led_offset {
group_size + group.pos - display_led_offset
} else {
0
};
for i in (start_index..end_index).rev() {
if i < colors.len() {
let bytes = colors[i].as_bytes();
buffer.append(&mut bytes.to_vec());