forked from wagslane/go-rabbitmq
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathpublish.go
209 lines (180 loc) · 5.9 KB
/
publish.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
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
package rabbitmq
import (
"fmt"
"sync"
"github.com/streadway/amqp"
)
// DeliveryMode. Transient means higher throughput but messages will not be
// restored on broker restart. The delivery mode of publishings is unrelated
// to the durability of the queues they reside on. Transient messages will
// not be restored to durable queues, persistent messages will be restored to
// durable queues and lost on non-durable queues during server restart.
//
// This remains typed as uint8 to match Publishing.DeliveryMode. Other
// delivery modes specific to custom queue implementations are not enumerated
// here.
const (
Transient uint8 = amqp.Transient
Persistent uint8 = amqp.Persistent
)
// Return captures a flattened struct of fields returned by the server when a
// Publishing is unable to be delivered either due to the `mandatory` flag set
// and no route found, or `immediate` flag set and no free consumer.
type Return struct {
amqp.Return
}
// Publisher allows you to publish messages safely across an open connection
type Publisher struct {
chManager *channelManager
notifyFlowChan chan bool
disablePublishDueToFlow bool
disablePublishDueToFlowMux *sync.RWMutex
logger Logger
}
// NewPublisher returns a new publisher with an open channel to the cluster.
// If you plan to enforce mandatory or immediate publishing, those failures will be reported
// on the channel of Returns that you should setup a listener on.
// Flow controls are automatically handled as they are sent from the server, and publishing
// will fail with an error when the server is requesting a slowdown
func NewPublisher(url string, config amqp.Config, optionFuncs ...func(*PublisherOptions)) (Publisher, <-chan Return, error) {
options := &PublisherOptions{}
for _, optionFunc := range optionFuncs {
optionFunc(options)
}
if options.Logger == nil {
options.Logger = &noLogger{} // default no logging
}
chManager, err := newChannelManager(url, config, options.Logger)
if err != nil {
return Publisher{}, nil, err
}
publisher := Publisher{
chManager: chManager,
notifyFlowChan: make(chan bool),
disablePublishDueToFlow: false,
disablePublishDueToFlowMux: &sync.RWMutex{},
logger: options.Logger,
}
returnAMQPChan := make(chan amqp.Return)
returnChan := make(chan Return)
returnAMQPChan = publisher.chManager.channel.NotifyReturn(returnAMQPChan)
go func() {
for ret := range returnAMQPChan {
returnChan <- Return{
ret,
}
}
}()
publisher.notifyFlowChan = publisher.chManager.channel.NotifyFlow(publisher.notifyFlowChan)
go publisher.startNotifyFlowHandler()
publisher.chManager.channelMux.RLock()
defer publisher.chManager.channelMux.RUnlock()
// valid queue name, declare it
if options.QueueDeclare.DoDeclare {
_, err = publisher.chManager.channel.QueueDeclare(
options.QueueDeclare.QueueName,
options.QueueDeclare.QueueDurable,
options.QueueDeclare.QueueAutoDelete,
options.QueueDeclare.QueueExclusive,
options.QueueDeclare.QueueNoWait,
tableToAMQPTable(options.QueueDeclare.QueueArgs),
)
if err != nil {
return publisher, returnChan, err
}
}
//valid name, bind it
if options.BindingExchange.DoBinding {
exchange := options.BindingExchange
err = publisher.chManager.channel.ExchangeDeclare(
exchange.Name,
exchange.Kind,
exchange.Durable,
exchange.AutoDelete,
exchange.Internal,
exchange.NoWait,
tableToAMQPTable(exchange.ExchangeArgs),
)
if err != nil {
return publisher, returnChan, err
}
for _, routingKey := range options.RoutingKeys {
err = publisher.chManager.channel.QueueBind(
options.QueueDeclare.QueueName,
routingKey,
exchange.Name,
options.BindingExchange.BindingNoWait,
tableToAMQPTable(options.BindingExchange.BindingArgs),
)
if err != nil {
return publisher, returnChan, err
}
}
}
err = publisher.chManager.channel.Qos(
options.Qos.QOSPrefetchCount,
options.Qos.QOSPrefetchSize,
options.Qos.QOSGlobal,
)
return publisher, returnChan, err
}
// Publish publishes the provided data to the given routing keys over the connection
func (publisher *Publisher) Publish(
data []byte,
routingKeys []string,
optionFuncs ...func(*PublishOptions),
) error {
publisher.disablePublishDueToFlowMux.RLock()
if publisher.disablePublishDueToFlow {
return fmt.Errorf("publishing blocked due to high flow on the server")
}
publisher.disablePublishDueToFlowMux.RUnlock()
options := &PublishOptions{}
for _, optionFunc := range optionFuncs {
optionFunc(options)
}
if options.DeliveryMode == 0 {
options.DeliveryMode = Transient
}
for _, routingKey := range routingKeys {
var message = amqp.Publishing{}
message.ContentType = options.ContentType
message.DeliveryMode = options.DeliveryMode
message.Body = data
message.Headers = tableToAMQPTable(options.Headers)
message.Expiration = options.Expiration
// Actual publish.
err := publisher.chManager.channel.Publish(
options.Exchange,
routingKey,
options.Mandatory,
options.Immediate,
message,
)
if err != nil {
return err
}
}
return nil
}
// StopPublishing stops the publishing of messages.
// The publisher should be discarded as it's not safe for re-use
func (publisher Publisher) StopPublishing() {
publisher.chManager.channel.Close()
publisher.chManager.connection.Close()
}
func (publisher *Publisher) startNotifyFlowHandler() {
// Listeners for active=true flow control. When true is sent to a listener,
// publishing should pause until false is sent to listeners.
for ok := range publisher.notifyFlowChan {
publisher.disablePublishDueToFlowMux.Lock()
if ok {
publisher.logger.Printf("pausing publishing due to flow request from server")
publisher.disablePublishDueToFlow = true
} else {
publisher.disablePublishDueToFlow = false
publisher.logger.Printf("resuming publishing due to flow request from server")
}
publisher.disablePublishDueToFlowMux.Unlock()
}
}