perf(go): avoid mqtt raw hex duplication

This commit is contained in:
lingniu
2026-07-03 08:48:07 +08:00
parent 1830798d2e
commit d4c164480f
2 changed files with 12 additions and 2 deletions

View File

@@ -4,7 +4,6 @@ import (
"context"
"crypto/tls"
"crypto/x509"
"encoding/hex"
"errors"
"fmt"
"log/slog"
@@ -193,7 +192,6 @@ func (c *MQTTClient) handleMessage(ctx context.Context, topic string, payload []
EventTimeMS: receivedAtMS,
ReceivedAtMS: receivedAtMS,
RawText: string(payload),
RawHex: strings.ToUpper(hex.EncodeToString(payload)),
ParseStatus: envelope.ParseBadFrame,
ParseError: err.Error(),
}

View File

@@ -45,6 +45,12 @@ func TestMQTTClientHandleMessagePublishesOnlyRawByDefault(t *testing.T) {
if sink.raw[0].Protocol != envelope.ProtocolYutongMQTT || sink.raw[0].VIN != "LTEST000000000001" {
t.Fatalf("unexpected raw envelope: %#v", sink.raw[0])
}
if sink.raw[0].RawText == "" {
t.Fatal("mqtt raw envelope should keep text payload")
}
if sink.raw[0].RawHex != "" {
t.Fatalf("mqtt raw envelope should not duplicate text payload as hex: %q", sink.raw[0].RawHex)
}
}
func TestMQTTClientRecordsMessageMetrics(t *testing.T) {
@@ -104,6 +110,12 @@ func TestMQTTClientHandleBadPayloadPublishesOnlyRaw(t *testing.T) {
if sink.raw[0].ParseStatus != envelope.ParseBadFrame {
t.Fatalf("parse status = %q", sink.raw[0].ParseStatus)
}
if sink.raw[0].RawText == "" {
t.Fatal("bad mqtt raw envelope should keep text payload")
}
if sink.raw[0].RawHex != "" {
t.Fatalf("bad mqtt raw envelope should not duplicate text payload as hex: %q", sink.raw[0].RawHex)
}
}
func TestMQTTClientUsesUncancelledMessageContextForReceivedMessage(t *testing.T) {