Skip to content

Commit 1e5eeaa

Browse files
authored
Merge pull request #47 from skrater/master
Receive notification when channel is closed
2 parents cb1bd81 + 8231c14 commit 1e5eeaa

3 files changed

Lines changed: 87 additions & 32 deletions

File tree

amqp/producer.go

Lines changed: 75 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,8 @@ import (
1414
)
1515

1616
// ErrNotAcked indicated that published messages was not acked by RabbitMQ
17-
var ErrNotAcked = errors.New("messge was not acked")
17+
var ErrNotAcked = errors.New("message was not acked")
18+
var ErrTimedout = errors.New("message was timed out")
1819

1920
type message struct {
2021
action string
@@ -23,20 +24,22 @@ type message struct {
2324

2425
// producer holds a amqp connection and channel to publish messages to.
2526
type producer struct {
26-
m sync.Mutex
27-
wg sync.WaitGroup
28-
conn *connection
29-
channel *amqplib.Channel
30-
notifyConfirm chan amqplib.Confirmation
31-
closeQueue chan bool
32-
config ProducerConfig
27+
m sync.Mutex
28+
wg sync.WaitGroup
29+
conn *connection
30+
channel *amqplib.Channel
31+
notifyConfirm chan amqplib.Confirmation
32+
notifyChanClose chan *amqplib.Error
33+
closeQueue chan bool
34+
config ProducerConfig
3335

3436
internalQueue chan message
3537

3638
exchangeName string
3739

38-
closed bool
39-
closes []chan bool
40+
closed bool
41+
channelReady bool
42+
closes []chan bool
4043
}
4144

4245
// ProducerConfig to be used when creating a new producer.
@@ -59,7 +62,6 @@ func NewProducerConfig(c messaging.Connection, exchange string, config ProducerC
5962
config: config,
6063
internalQueue: make(chan message, 2),
6164
exchangeName: exchange,
62-
notifyConfirm: make(chan amqplib.Confirmation),
6365
closeQueue: make(chan bool),
6466
}
6567

@@ -141,8 +143,14 @@ func (p *producer) Close() {
141143
// and updates the channel listeners to reflect this.
142144
func (p *producer) changeChannel(channel *amqplib.Channel) {
143145
p.channel = channel
146+
147+
p.notifyChanClose = make(chan *amqplib.Error)
148+
p.channel.NotifyClose(p.notifyChanClose)
149+
144150
p.notifyConfirm = make(chan amqplib.Confirmation)
145151
p.channel.NotifyPublish(p.notifyConfirm)
152+
153+
p.channelReady = true
146154
}
147155

148156
func (p *producer) setupTopology() error {
@@ -197,14 +205,59 @@ func (p *producer) setupTopology() error {
197205
return nil
198206
}
199207

208+
func (p *producer) setChannelReady(ready bool) {
209+
p.m.Lock()
210+
defer p.m.Unlock()
211+
212+
p.channelReady = ready
213+
}
214+
215+
func (p *producer) isChannelReady() bool {
216+
p.m.Lock()
217+
defer p.m.Unlock()
218+
219+
return p.channelReady
220+
}
221+
222+
func (p *producer) isConnected() bool {
223+
if !p.conn.IsConnected() {
224+
return false
225+
}
226+
227+
return p.isChannelReady()
228+
}
229+
230+
func (p *producer) waitConnectionLost() bool {
231+
if !p.isConnected() {
232+
return true
233+
}
234+
235+
defer p.setChannelReady(false)
236+
237+
select {
238+
case <-p.conn.NotifyConnectionClose():
239+
log.Warn("Producer connection closed")
240+
return true
241+
case <-p.notifyChanClose:
242+
log.Warn("Producer channel closed")
243+
return false
244+
}
245+
}
246+
200247
func (p *producer) handleReestablishedConnnection() {
201248
rs := p.conn.NotifyReestablish()
202249

203250
for !p.isClosed() {
204-
<-rs
251+
// true if connection is lot
252+
// false if channel connection is lost
253+
connectionLost := p.waitConnectionLost()
205254

206-
err := p.setupTopology()
255+
if connectionLost {
256+
// Wait reconnect
257+
<-rs
258+
}
207259

260+
err := p.setupTopology()
208261
if err != nil {
209262
log.WithFields(log.Fields{
210263
"type": "goevents",
@@ -216,8 +269,8 @@ func (p *producer) handleReestablishedConnnection() {
216269
}
217270

218271
func (p *producer) publishMessage(msg amqplib.Publishing, queue string) (err error) {
219-
if !p.conn.IsConnected() {
220-
err = errors.New("connection is not open")
272+
if !p.isConnected() {
273+
err = errors.New("connection/channel is not open")
221274
return
222275
}
223276

@@ -245,7 +298,12 @@ func (p *producer) publishMessage(msg amqplib.Publishing, queue string) (err err
245298
}
246299
}()
247300

248-
err = p.channel.Publish(p.exchangeName, queue, false, false, msg)
301+
err = p.channel.Publish(
302+
p.exchangeName, // Exchange
303+
queue, // Routing key
304+
false, // Mandatory
305+
false, // Immediate
306+
msg)
249307

250308
if err != nil {
251309
return
@@ -260,7 +318,7 @@ func (p *producer) publishMessage(msg amqplib.Publishing, queue string) (err err
260318
err = ErrNotAcked
261319
return
262320
case <-time.After(p.config.publishInterval):
263-
err = ErrNotAcked
321+
err = ErrTimedout
264322
return
265323
}
266324

go.mod

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -3,9 +3,9 @@ module github.com/eventials/goevents
33
go 1.12
44

55
require (
6-
github.com/aws/aws-sdk-go v1.18.0
7-
github.com/sirupsen/logrus v1.4.0
8-
github.com/streadway/amqp v0.0.0-20190312002841-61ee40d2027b
6+
github.com/aws/aws-sdk-go v1.19.21
7+
github.com/sirupsen/logrus v1.4.1
8+
github.com/streadway/amqp v0.0.0-20190404075320-75d898a42a94
99
github.com/stretchr/testify v1.3.0
10-
golang.org/x/net v0.0.0-20190311183353-d8887717615a // indirect
10+
golang.org/x/net v0.0.0-20190501004415-9ce7a6920f09 // indirect
1111
)

go.sum

Lines changed: 8 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
1-
github.com/aws/aws-sdk-go v1.18.0 h1:CXoiktHzavv3gwZsVPGuyTXpHg6dd1ssrGKMQuQNrdY=
2-
github.com/aws/aws-sdk-go v1.18.0/go.mod h1:KmX6BPdI08NWTb3/sm4ZGu5ShLoqVDhKgpiN924inxo=
1+
github.com/aws/aws-sdk-go v1.19.21 h1:xLaPxl8gy0ZSXbc13jsCKIaHD6NiX+2tAQodPSEL5r8=
2+
github.com/aws/aws-sdk-go v1.19.21/go.mod h1:KmX6BPdI08NWTb3/sm4ZGu5ShLoqVDhKgpiN924inxo=
33
github.com/davecgh/go-spew v1.1.0/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
44
github.com/davecgh/go-spew v1.1.1 h1:vj9j/u1bqnvCEfJOwUhtlOARqs3+rkHYY13jYWTU97c=
55
github.com/davecgh/go-spew v1.1.1/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
@@ -9,22 +9,19 @@ github.com/konsorten/go-windows-terminal-sequences v1.0.1 h1:mweAR1A6xJ3oS2pRaGi
99
github.com/konsorten/go-windows-terminal-sequences v1.0.1/go.mod h1:T0+1ngSBFLxvqU3pZ+m/2kptfBszLMUkC4ZK/EgS/cQ=
1010
github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZbAQM=
1111
github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4=
12-
github.com/sirupsen/logrus v1.4.0 h1:yKenngtzGh+cUSSh6GWbxW2abRqhYUSR/t/6+2QqNvE=
13-
github.com/sirupsen/logrus v1.4.0/go.mod h1:LxeOpSwHxABJmUn/MG1IvRgCAasNZTLOkJPxbbu5VWo=
14-
github.com/streadway/amqp v0.0.0-20190312002841-61ee40d2027b h1:VPo/aUrW0PUdrSwI0UWPY/zXBoLg78fEXLPSsOqngk4=
15-
github.com/streadway/amqp v0.0.0-20190312002841-61ee40d2027b/go.mod h1:1WNBiOZtZQLpVAyu0iTduoJL9hEsMloAK5XWrtW0xdY=
12+
github.com/sirupsen/logrus v1.4.1 h1:GL2rEmy6nsikmW0r8opw9JIRScdMF5hA8cOYLH7In1k=
13+
github.com/sirupsen/logrus v1.4.1/go.mod h1:ni0Sbl8bgC9z8RoU9G6nDWqqs/fq4eDPysMBDgk/93Q=
14+
github.com/streadway/amqp v0.0.0-20190404075320-75d898a42a94 h1:0ngsPmuP6XIjiFRNFYlvKwSr5zff2v+uPHaffZ6/M4k=
15+
github.com/streadway/amqp v0.0.0-20190404075320-75d898a42a94/go.mod h1:AZpEONHx3DKn8O/DFsRAY58/XVQiIPMTMB1SddzLXVw=
1616
github.com/stretchr/objx v0.1.0/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+wExME=
1717
github.com/stretchr/objx v0.1.1 h1:2vfRuCMp5sSVIDSqO8oNnWJq7mPa6KVP3iPIwFBuy8A=
1818
github.com/stretchr/objx v0.1.1/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+wExME=
1919
github.com/stretchr/testify v1.2.2/go.mod h1:a8OnRcib4nhh0OaRAV+Yts87kKdq0PP7pXfy6kDkUVs=
2020
github.com/stretchr/testify v1.3.0 h1:TivCn/peBQ7UY8ooIcPgZFpTNSz0Q2U6UrFlUfqbe0Q=
2121
github.com/stretchr/testify v1.3.0/go.mod h1:M5WIy9Dh21IEIfnGCwXGc5bZfKNJtfHm1UVUgZn+9EI=
22-
golang.org/x/crypto v0.0.0-20180904163835-0709b304e793 h1:u+LnwYTOOW7Ukr/fppxEb1Nwz0AtPflrblfvUudpo+I=
23-
golang.org/x/crypto v0.0.0-20180904163835-0709b304e793/go.mod h1:6SG95UA2DQfeDnfUPMdvaQW0Q7yPrPDi9nlGo2tz2b4=
24-
golang.org/x/crypto v0.0.0-20190308221718-c2843e01d9a2 h1:VklqNMn3ovrHsnt90PveolxSbWFaJdECFbxSq0Mqo2M=
2522
golang.org/x/crypto v0.0.0-20190308221718-c2843e01d9a2/go.mod h1:djNgcEr1/C05ACkg1iLfiJU5Ep61QUkGW8qpdssI0+w=
26-
golang.org/x/net v0.0.0-20190311183353-d8887717615a h1:oWX7TPOiFAMXLq8o0ikBYfCJVlRHBcsciT5bXOrH628=
27-
golang.org/x/net v0.0.0-20190311183353-d8887717615a/go.mod h1:t9HGtf8HONx5eT2rtn7q6eTqICYqUVnKs3thJo3Qplg=
23+
golang.org/x/net v0.0.0-20190501004415-9ce7a6920f09 h1:KaQtG+aDELoNmXYas3TVkGNYRuq8JQ1aa7LJt8EXVyo=
24+
golang.org/x/net v0.0.0-20190501004415-9ce7a6920f09/go.mod h1:t9HGtf8HONx5eT2rtn7q6eTqICYqUVnKs3thJo3Qplg=
2825
golang.org/x/sys v0.0.0-20180905080454-ebe1bf3edb33 h1:I6FyU15t786LL7oL/hn43zqTuEGr4PN7F4XJ1p4E3Y8=
2926
golang.org/x/sys v0.0.0-20180905080454-ebe1bf3edb33/go.mod h1:STP8DvDyc/dI5b8T5hshtkjS+E42TnysNCUPdjciGhY=
3027
golang.org/x/sys v0.0.0-20190215142949-d0b11bdaac8a h1:1BGLXjeY4akVXGgbC9HugT3Jv3hCI0z56oJR5vAMgBU=

0 commit comments

Comments
 (0)