2024-11-20 18:51:46 +00:00
|
|
|
package ctrl
|
|
|
|
|
|
|
|
import (
|
|
|
|
"fmt"
|
|
|
|
|
|
|
|
"github.com/fxamacker/cbor/v2"
|
|
|
|
"github.com/klauspost/compress/zstd"
|
|
|
|
"github.com/nats-io/nats.go/jetstream"
|
|
|
|
)
|
|
|
|
|
2024-11-22 19:36:55 +00:00
|
|
|
// messageDeserializeAndUncompress will deserialize the ctrl message
|
|
|
|
func (p *process) messageDeserializeAndUncompress(msg jetstream.Msg) (Message, error) {
|
2024-11-20 18:51:46 +00:00
|
|
|
|
|
|
|
// Variable to hold a copy of the message data.
|
|
|
|
msgData := msg.Data()
|
|
|
|
|
|
|
|
// If debugging is enabled, print the source node name of the nats messages received.
|
|
|
|
headerFromNode := msg.Headers().Get("fromNode")
|
|
|
|
if headerFromNode != "" {
|
2024-11-22 19:36:55 +00:00
|
|
|
er := fmt.Errorf("info: subscriberHandlerJetstream: nats message received from %v, with subject %v ", headerFromNode, msg.Subject())
|
2024-11-20 18:51:46 +00:00
|
|
|
p.errorKernel.logDebug(er)
|
|
|
|
}
|
|
|
|
|
|
|
|
zr, err := zstd.NewReader(nil)
|
|
|
|
if err != nil {
|
|
|
|
er := fmt.Errorf("error: subscriberHandlerJetstream: zstd NewReader failed: %v", err)
|
2024-11-22 19:36:55 +00:00
|
|
|
return Message{}, er
|
2024-11-20 18:51:46 +00:00
|
|
|
}
|
|
|
|
msgData, err = zr.DecodeAll(msgData, nil)
|
|
|
|
if err != nil {
|
|
|
|
er := fmt.Errorf("error: subscriberHandlerJetstream: zstd decoding failed: %v", err)
|
|
|
|
zr.Close()
|
2024-11-22 19:36:55 +00:00
|
|
|
return Message{}, er
|
2024-11-20 18:51:46 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
zr.Close()
|
|
|
|
|
|
|
|
message := Message{}
|
|
|
|
|
|
|
|
err = cbor.Unmarshal(msgData, &message)
|
|
|
|
if err != nil {
|
2024-11-22 19:36:55 +00:00
|
|
|
er := fmt.Errorf("error: subscriberHandlerJetstream: cbor decoding failed, subject: %v, error: %v", msg.Subject(), err)
|
|
|
|
return Message{}, er
|
2024-11-20 18:51:46 +00:00
|
|
|
}
|
|
|
|
|
2024-11-22 19:36:55 +00:00
|
|
|
return message, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// messageSerializeAndCompress will serialize and compress the Message, and
|
|
|
|
// return the result as a []byte.
|
|
|
|
func (p *process) messageSerializeAndCompress(msg Message) ([]byte, error) {
|
|
|
|
|
|
|
|
// encode the message structure into cbor
|
|
|
|
bSerialized, err := cbor.Marshal(msg)
|
|
|
|
if err != nil {
|
|
|
|
er := fmt.Errorf("error: messageDeliverNats: cbor encode message failed: %v", err)
|
|
|
|
p.errorKernel.logDebug(er)
|
|
|
|
return nil, er
|
2024-11-20 18:51:46 +00:00
|
|
|
}
|
|
|
|
|
2024-11-22 19:36:55 +00:00
|
|
|
// Compress the data payload if selected with configuration flag.
|
|
|
|
// The compression chosen is later set in the nats msg header when
|
|
|
|
// calling p.messageDeliverNats below.
|
|
|
|
|
|
|
|
bCompressed := p.server.zstdEncoder.EncodeAll(bSerialized, nil)
|
2024-11-20 18:51:46 +00:00
|
|
|
|
2024-11-22 19:36:55 +00:00
|
|
|
return bCompressed, nil
|
2024-11-20 18:51:46 +00:00
|
|
|
}
|