feat: migrate from msgpack to protobuf (handler-base v1.0.0)
- Replace msgpack encoding with protobuf wire format - Update field names to proto convention (SessionId) - Cast int fields to int32 (ChunkIndex, TotalChunks, SampleRate, Count) - Use pointer slices for repeated messages ([]*TTSVoiceInfo) - Rewrite tests for proto round-trips
This commit is contained in:
32
main.go
32
main.go
@@ -18,7 +18,7 @@ import (
|
||||
"time"
|
||||
|
||||
"github.com/nats-io/nats.go"
|
||||
"github.com/vmihailenco/msgpack/v5"
|
||||
"google.golang.org/protobuf/proto"
|
||||
|
||||
"git.daviestechlabs.io/daviestechlabs/handler-base/config"
|
||||
"git.daviestechlabs.io/daviestechlabs/handler-base/health"
|
||||
@@ -128,12 +128,12 @@ func (vr *VoiceRegistry) get(name string) *CustomVoice {
|
||||
return vr.voices[name]
|
||||
}
|
||||
|
||||
func (vr *VoiceRegistry) listVoices() []messages.TTSVoiceInfo {
|
||||
func (vr *VoiceRegistry) listVoices() []*messages.TTSVoiceInfo {
|
||||
vr.mu.RLock()
|
||||
defer vr.mu.RUnlock()
|
||||
result := make([]messages.TTSVoiceInfo, 0, len(vr.voices))
|
||||
result := make([]*messages.TTSVoiceInfo, 0, len(vr.voices))
|
||||
for _, v := range vr.voices {
|
||||
result = append(result, messages.TTSVoiceInfo{
|
||||
result = append(result, &messages.TTSVoiceInfo{
|
||||
Name: v.Name,
|
||||
Language: v.Language,
|
||||
ModelType: v.ModelType,
|
||||
@@ -223,7 +223,7 @@ func main() {
|
||||
// Helper: publish status
|
||||
publishStatus := func(sessionID, status, message string) {
|
||||
statusMsg := &messages.TTSStatus{
|
||||
SessionID: sessionID,
|
||||
SessionId: sessionID,
|
||||
Status: status,
|
||||
Message: message,
|
||||
Timestamp: time.Now().Unix(),
|
||||
@@ -285,13 +285,13 @@ func main() {
|
||||
isLast := end >= len(audioBytes)
|
||||
|
||||
msg := &messages.TTSAudioChunk{
|
||||
SessionID: sessionID,
|
||||
ChunkIndex: chunkIndex,
|
||||
TotalChunks: totalChunks,
|
||||
SessionId: sessionID,
|
||||
ChunkIndex: int32(chunkIndex),
|
||||
TotalChunks: int32(totalChunks),
|
||||
Audio: chunk,
|
||||
IsLast: isLast,
|
||||
Timestamp: time.Now().Unix(),
|
||||
SampleRate: sampleRate,
|
||||
SampleRate: int32(sampleRate),
|
||||
}
|
||||
_ = nc.Publish(fmt.Sprintf("%s.%s", audioSubjectPrefix, sessionID), msg)
|
||||
}
|
||||
@@ -307,8 +307,8 @@ func main() {
|
||||
}
|
||||
sessionID := parts[4]
|
||||
|
||||
req, err := natsutil.Decode[messages.TTSRequest](natMsg.Data)
|
||||
if err != nil {
|
||||
var req messages.TTSRequest
|
||||
if err := natsutil.Decode(natMsg.Data, &req); err != nil {
|
||||
slog.Error("decode error", "error", err)
|
||||
return
|
||||
}
|
||||
@@ -343,10 +343,10 @@ func main() {
|
||||
streamAudio(sessionID, audioBytes)
|
||||
} else {
|
||||
msg := &messages.TTSFullResponse{
|
||||
SessionID: sessionID,
|
||||
SessionId: sessionID,
|
||||
Audio: audioBytes,
|
||||
Timestamp: time.Now().Unix(),
|
||||
SampleRate: sampleRate,
|
||||
SampleRate: int32(sampleRate),
|
||||
}
|
||||
_ = nc.Publish(fmt.Sprintf("%s.%s", audioSubjectPrefix, sessionID), msg)
|
||||
}
|
||||
@@ -368,7 +368,7 @@ func main() {
|
||||
LastRefresh: registry.lastRefresh.Unix(),
|
||||
Timestamp: time.Now().Unix(),
|
||||
}
|
||||
packed, _ := msgpack.Marshal(resp)
|
||||
packed, _ := proto.Marshal(resp)
|
||||
if msg.Reply != "" {
|
||||
_ = msg.Respond(packed)
|
||||
}
|
||||
@@ -380,11 +380,11 @@ func main() {
|
||||
if _, err := nc.Conn().Subscribe(voicesRefreshSubject, func(msg *nats.Msg) {
|
||||
count := registry.refresh()
|
||||
resp := &messages.TTSVoiceRefreshResponse{
|
||||
Count: count,
|
||||
Count: int32(count),
|
||||
CustomVoices: registry.listVoices(),
|
||||
Timestamp: time.Now().Unix(),
|
||||
}
|
||||
packed, _ := msgpack.Marshal(resp)
|
||||
packed, _ := proto.Marshal(resp)
|
||||
if msg.Reply != "" {
|
||||
_ = msg.Respond(packed)
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user