-
Notifications
You must be signed in to change notification settings - Fork 0
/
amqp.go
141 lines (117 loc) · 3.01 KB
/
amqp.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
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
// Author: crochee
// Date: 2021/9/3
// Package mq
package mq
import (
"fmt"
"sync/atomic"
"github.com/ThreeDotsLabs/watermill/message"
"github.com/streadway/amqp"
)
type option struct {
config *amqp.Config
URI string
}
type Option func(*option)
func WithURI(uri string) Option {
return func(o *option) {
o.URI = uri
}
}
func WithConfig(cfg *amqp.Config) Option {
return func(o *option) {
o.config = cfg
}
}
type Client struct {
*amqp.Connection
connected uint32
}
// New create a mq client
func New(opts ...Option) (*Client, error) {
o := &option{
URI: "amqp://guest:guest@localhost:5672/",
}
for _, opt := range opts {
opt(o)
}
r := &Client{}
var err error
if o.config == nil {
r.Connection, err = amqp.Dial(o.URI)
} else {
r.Connection, err = amqp.DialConfig(o.URI, *o.config)
}
if err != nil {
return nil, err
}
atomic.AddUint32(&r.connected, 1)
return r, nil
}
func (r *Client) IsConnected() bool {
return atomic.LoadUint32(&r.connected) == 1
}
type MarshalAPI interface {
Marshal(msg *message.Message) (amqp.Publishing, error)
Unmarshal(amqpMsg *amqp.Delivery) (*message.Message, error)
}
const DefaultMessageUUIDHeaderKey = "_message_uuid"
type DefaultMarshal struct {
PostprocessPublishing func(amqp.Publishing) amqp.Publishing
NotPersistentDeliveryMode bool
MessageUUIDHeaderKey string
}
func (d DefaultMarshal) Marshal(msg *message.Message) (amqp.Publishing, error) {
headers := make(amqp.Table, len(msg.Metadata)+1) // metadata + plus uuid
for key, value := range msg.Metadata {
headers[key] = value
}
headers[d.computeMessageUUIDHeaderKey()] = msg.UUID
publishing := amqp.Publishing{
Body: msg.Payload,
Headers: headers,
}
if !d.NotPersistentDeliveryMode {
publishing.DeliveryMode = amqp.Persistent
}
if d.PostprocessPublishing != nil {
publishing = d.PostprocessPublishing(publishing)
}
return publishing, nil
}
func (d DefaultMarshal) Unmarshal(amqpMsg *amqp.Delivery) (*message.Message, error) {
msgUUIDStr, err := d.unmarshalMessageUUID(amqpMsg)
if err != nil {
return nil, err
}
msg := message.NewMessage(msgUUIDStr, amqpMsg.Body)
msg.Metadata = make(message.Metadata, len(amqpMsg.Headers)-1) // headers - minus uuid
for key, value := range amqpMsg.Headers {
if key == d.computeMessageUUIDHeaderKey() {
continue
}
var ok bool
msg.Metadata[key], ok = value.(string)
if !ok {
return nil, fmt.Errorf("metadata %s is not a string, but %#v", key, value)
}
}
return msg, nil
}
func (d DefaultMarshal) unmarshalMessageUUID(amqpMsg *amqp.Delivery) (string, error) {
msgUUID, hasMsgUUID := amqpMsg.Headers[d.computeMessageUUIDHeaderKey()]
if !hasMsgUUID {
return "", nil
}
var msgUUIDStr string
if msgUUIDStr, hasMsgUUID = msgUUID.(string); !hasMsgUUID {
return "", fmt.Errorf("message UUID is not a string, but: %#v", msgUUID)
}
return msgUUIDStr, nil
}
func (d DefaultMarshal) computeMessageUUIDHeaderKey() string {
if d.MessageUUIDHeaderKey != "" {
return d.MessageUUIDHeaderKey
}
return DefaultMessageUUIDHeaderKey
}