-
Notifications
You must be signed in to change notification settings - Fork 0
/
messsage.go
61 lines (51 loc) · 1.58 KB
/
messsage.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
package vnats
import (
"github.com/nats-io/nats.go"
)
// A Header represents the key-value pairs.
type Header map[string][]string
// Msg contains the arguments publishing a new message.
// By using a struct we are open for adding new arguments in the future
// and the caller can omit arguments where the default value is OK.
type Msg struct {
// Subject represents the destination subject name, like "PRODUCTS.new"
Subject string
// Reply represents an optional subject name where a reply message should be sent to.
// This value is just distributed, whether the response is sent to the specified subject depends on the Subscriber.
Reply string
// MsgID represents a unique value for the message, like a hash value of Data.
// Semantically equal messages must lead to the same MsgID at any time.
// E.g. two messages with the same Data must have the same MsgID.
//
// The MsgID is used for deduplication.
MsgID string
// Data represents the raw byte data to send. The data is sent as-is.
Data []byte
// Header represents the optional Header for the message.
Header Header
}
// NewMsg constructs a new Msg with the given data.
func NewMsg(subject, id string, data []byte) *Msg {
return &Msg{
Subject: subject,
MsgID: id,
Data: data,
}
}
func makeMsg(msg *nats.Msg) Msg {
return Msg{
Subject: msg.Subject,
Reply: msg.Reply,
MsgID: msg.Header.Get(nats.MsgIdHdr),
Data: msg.Data,
Header: Header(msg.Header),
}
}
func (m *Msg) toNATS() *nats.Msg {
return &nats.Msg{
Subject: m.Subject,
Reply: m.Reply,
Data: m.Data,
Header: nats.Header(m.Header),
}
}