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
75 lines
1.3 KiB
Go
75 lines
1.3 KiB
Go
package utils
|
|
|
|
import (
|
|
"context"
|
|
|
|
"github.com/apache/pulsar-client-go/pulsar"
|
|
"go.uber.org/zap"
|
|
)
|
|
|
|
var (
|
|
pulsarClient pulsar.Client
|
|
producer pulsar.Producer
|
|
pulsarUrl string
|
|
pulsarTopic string
|
|
clientName string
|
|
)
|
|
|
|
func CreateProducer(url string, topic string, name string) {
|
|
pulsarUrl = url
|
|
pulsarTopic = topic
|
|
clientName = name
|
|
create()
|
|
}
|
|
|
|
func create() {
|
|
var err error
|
|
pulsarClient, err = pulsar.NewClient(pulsar.ClientOptions{
|
|
URL: pulsarUrl,
|
|
})
|
|
if err != nil {
|
|
Log.Error("error in create pulsar client ", zap.Error(err))
|
|
}
|
|
producer, err = pulsarClient.CreateProducer(pulsar.ProducerOptions{
|
|
Topic: pulsarTopic,
|
|
Name: clientName,
|
|
})
|
|
if err != nil {
|
|
Log.Error("error in create pulsar producer ", zap.Error(err))
|
|
}
|
|
}
|
|
|
|
func resetPulsarProducer() {
|
|
if producer != nil {
|
|
producer.Close()
|
|
}
|
|
if pulsarClient != nil {
|
|
pulsarClient.Close()
|
|
}
|
|
create()
|
|
}
|
|
|
|
func PulsarSend(data string) error {
|
|
var err error
|
|
var id pulsar.MessageID
|
|
for i := 0; i < 5; i++ {
|
|
id, err = producer.Send(context.Background(), &pulsar.ProducerMessage{
|
|
Payload: []byte(data),
|
|
})
|
|
if err == nil {
|
|
Log.Info("send ", zap.Any("id", id))
|
|
break
|
|
}
|
|
resetPulsarProducer()
|
|
}
|
|
return err
|
|
}
|
|
|
|
func ClosePulsar() {
|
|
if producer != nil {
|
|
producer.Close()
|
|
}
|
|
if pulsarClient != nil {
|
|
pulsarClient.Close()
|
|
}
|
|
} |