mirror of
https://github.com/eventials/goevents.git
synced 2025-04-24 13:48:53 +08:00
Fix producer to no block when publish fail
This commit is contained in:
parent
dce9bd51db
commit
4d54df7868
@ -28,7 +28,6 @@ type producer struct {
|
||||
conn *connection
|
||||
channel *amqplib.Channel
|
||||
notifyConfirm chan amqplib.Confirmation
|
||||
connectionClosed <-chan error
|
||||
closeQueue chan bool
|
||||
config ProducerConfig
|
||||
|
||||
@ -55,8 +54,6 @@ func NewProducer(c messaging.Connection, exchange string) (*producer, error) {
|
||||
|
||||
// NewProducerConfig returns a new AMQP Producer.
|
||||
func NewProducerConfig(c messaging.Connection, exchange string, config ProducerConfig) (*producer, error) {
|
||||
conn := c.(*connection)
|
||||
|
||||
producer := &producer{
|
||||
conn: c.(*connection),
|
||||
config: config,
|
||||
@ -64,7 +61,6 @@ func NewProducerConfig(c messaging.Connection, exchange string, config ProducerC
|
||||
exchangeName: exchange,
|
||||
notifyConfirm: make(chan amqplib.Confirmation),
|
||||
closeQueue: make(chan bool),
|
||||
connectionClosed: conn.NotifyConnectionClose(),
|
||||
}
|
||||
|
||||
err := producer.setupTopology()
|
||||
@ -73,6 +69,7 @@ func NewProducerConfig(c messaging.Connection, exchange string, config ProducerC
|
||||
return nil, err
|
||||
}
|
||||
|
||||
go producer.drainInternalQueue()
|
||||
go producer.handleReestablishedConnnection()
|
||||
|
||||
return producer, err
|
||||
@ -134,6 +131,10 @@ func (p *producer) Close() {
|
||||
close(p.closeQueue)
|
||||
|
||||
p.channel.Close()
|
||||
|
||||
for _, c := range p.closes {
|
||||
c <- true
|
||||
}
|
||||
}
|
||||
|
||||
// changeChannel takes a new channel to the queue,
|
||||
@ -191,9 +192,7 @@ func (p *producer) setupTopology() error {
|
||||
log.WithFields(log.Fields{
|
||||
"type": "goevents",
|
||||
"sub_type": "producer",
|
||||
}).Debug("Topology ready. Draining internal queue.")
|
||||
|
||||
go p.drainInternalQueue()
|
||||
}).Debug("Topology ready.")
|
||||
|
||||
return nil
|
||||
}
|
||||
@ -217,6 +216,11 @@ func (p *producer) handleReestablishedConnnection() {
|
||||
}
|
||||
|
||||
func (p *producer) publishMessage(msg amqplib.Publishing, queue string) (err error) {
|
||||
if !p.conn.IsConnected() {
|
||||
err = errors.New("connection is not open")
|
||||
return
|
||||
}
|
||||
|
||||
log.WithFields(log.Fields{
|
||||
"action": queue,
|
||||
"body": msg.Body,
|
||||
@ -241,11 +245,6 @@ func (p *producer) publishMessage(msg amqplib.Publishing, queue string) (err err
|
||||
}
|
||||
}()
|
||||
|
||||
if !p.conn.IsConnected() {
|
||||
err = errors.New("connection is not open")
|
||||
return
|
||||
}
|
||||
|
||||
err = p.channel.Publish(p.exchangeName, queue, false, false, msg)
|
||||
|
||||
if err != nil {
|
||||
@ -257,6 +256,9 @@ func (p *producer) publishMessage(msg amqplib.Publishing, queue string) (err err
|
||||
if confirm.Ack {
|
||||
return
|
||||
}
|
||||
|
||||
err = ErrNotAcked
|
||||
return
|
||||
case <-time.After(p.config.publishInterval):
|
||||
err = ErrNotAcked
|
||||
return
|
||||
@ -277,9 +279,10 @@ func (p *producer) drainInternalQueue() {
|
||||
select {
|
||||
case <-p.closeQueue:
|
||||
return
|
||||
case <-p.connectionClosed:
|
||||
return
|
||||
case m := <-p.internalQueue:
|
||||
retry := true
|
||||
|
||||
for retry {
|
||||
// block until confirmation
|
||||
err := p.publishMessage(m.msg, m.action)
|
||||
|
||||
@ -293,14 +296,12 @@ func (p *producer) drainInternalQueue() {
|
||||
"sub_type": "producer",
|
||||
}).Error("Error publishing message to the exchange. Retrying...")
|
||||
|
||||
p.internalQueue <- m
|
||||
time.Sleep(p.config.publishInterval)
|
||||
} else {
|
||||
p.wg.Done()
|
||||
retry = false
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
for _, c := range p.closes {
|
||||
c <- true
|
||||
}
|
||||
}
|
||||
|
Loading…
x
Reference in New Issue
Block a user