Phase 1 — Bug fixes (7 bugs): - Bug 1: Pulsar mode data loss — always persist to SQLite regardless of mode - Bug 2: PulsarSend closing TCP client — use independent resetPulsarProducer() - Bug 3: Greedy regex — use non-greedy (?s)ZCZC.*?NNNN - Bug 4: Data after NNNN discarded — keep remaining buffer data - Bug 5: strings.Index > 0 boundary — use strings.Contains - Bug 6: Variable shadowing in PulsarSend — use = not := - Bug 7: Accept failure nil panic — add continue + retry logic Phase 2 — Architecture restructuring: - Split utils/ into config/, serial/, telegram/, storage/, transport/ - Define Sender, Repository, Reader interfaces - Introduce app/ layer with context.Context lifecycle - Replace spinlock with sync.Mutex - Unified Config struct replaces 15+ global vars Phase 3 — Testing & tooling: - telegram/parser_test.go (7 test cases) - storage/store_test.go (5 test cases) - transport/transport_test.go (4 test cases) - Taskfile: add test, test-race, test-cover tasks - Go version: 1.15 -> 1.21
64 lines
1.4 KiB
Go
64 lines
1.4 KiB
Go
package utils
|
|
|
|
import (
|
|
"go.uber.org/zap"
|
|
"regexp"
|
|
"strings"
|
|
|
|
rotatelogs "github.com/lestrrat-go/file-rotatelogs"
|
|
)
|
|
|
|
const (
|
|
EndTag = "NNNN"
|
|
Expression = "(?s)ZCZC.*?NNNN"
|
|
MaxBufferSize = 65536
|
|
BufferTrimSize = 32768
|
|
)
|
|
|
|
var buffer string
|
|
var exp, _ = regexp.Compile(Expression)
|
|
var RawLog *rotatelogs.RotateLogs
|
|
var Tcp bool
|
|
var Pulsar bool
|
|
|
|
func Append(data string) bool {
|
|
buffer += data + "\n"
|
|
if len(buffer) > MaxBufferSize {
|
|
Log.Warn("buffer overflow, truncating",
|
|
zap.Int("size", len(buffer)),
|
|
zap.Int("max", MaxBufferSize))
|
|
buffer = buffer[len(buffer)-BufferTrimSize:]
|
|
}
|
|
return check()
|
|
}
|
|
|
|
func check() bool {
|
|
teleString := buffer
|
|
if strings.Contains(teleString, EndTag) {
|
|
loc := exp.FindStringIndex(teleString)
|
|
if loc != nil {
|
|
telegram := teleString[loc[0]:loc[1]]
|
|
telegram = removeEmpty(telegram) + "\n\n"
|
|
telegram += "\n"
|
|
_, _ = RawLog.Write([]byte(telegram + "\n"))
|
|
buffer = teleString[loc[1]:]
|
|
|
|
// 先持久化,再发送:即使发送失败,数据已安全落盘
|
|
if err := InsertTelegram(telegram); err != nil {
|
|
Log.Error("error in save telegram to db", zap.Error(err))
|
|
}
|
|
if Pulsar {
|
|
if err := PulsarSend(telegram); err != nil {
|
|
Log.Error("error in send to pulsar", zap.Error(err))
|
|
}
|
|
}
|
|
return true
|
|
}
|
|
}
|
|
return false
|
|
}
|
|
|
|
func removeEmpty(s string) string {
|
|
return regexp.MustCompile(`[\t\r\n]+`).ReplaceAllString(strings.TrimSpace(s), "\n")
|
|
}
|