Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 8 additions & 4 deletions pubsub/consumer.go
Original file line number Diff line number Diff line change
Expand Up @@ -87,7 +87,7 @@ func NewConsumer(rabbitURL, queueName string) Consumer {
}

// Consume consume messages from the channels
func (c *consumer) Consume(workerFunc func([]byte)) error {
func (c *consumer) Consume(workerFunc func(amqp.Delivery)) error {
cfg := amqp.Config{
Properties: amqp.Table{
"connection_name": c.connectionName,
Expand All @@ -99,14 +99,18 @@ func (c *consumer) Consume(workerFunc func([]byte)) error {
return fmt.Errorf("failed to connect to RabbitMQ: %s", err.Error())
}

defer conn.Close()
defer func() {
_ = conn.Close()
}()

ch, err := conn.Channel()
if err != nil {
return fmt.Errorf("failed to open a channel: %s", err.Error())
}

defer ch.Close()
defer func() {
_ = ch.Close()
}()

q, err := ch.QueueDeclare(
c.queue, // name
Expand Down Expand Up @@ -140,7 +144,7 @@ func (c *consumer) Consume(workerFunc func([]byte)) error {
}

for message := range deliveries {
workerFunc(message.Body)
workerFunc(message)
}

return nil
Expand Down
4 changes: 3 additions & 1 deletion pubsub/publisher.go
Original file line number Diff line number Diff line change
Expand Up @@ -75,7 +75,9 @@ func (p *publisher) publish(body []byte, delay time.Duration) error {
return fmt.Errorf("failed to connect to RabbitMQ: %s", err.Error())
}

defer conn.Close()
defer func() {
_ = conn.Close()
}()

channel, err := conn.Channel()
if err != nil {
Expand Down
2 changes: 1 addition & 1 deletion pubsub/pubsub.go
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ type Consumer interface {
SetExclusive(bool)
SetNoLocal(bool)
SetNoWait(bool)
Consume(func([]byte)) error
Consume(func(amqp.Delivery)) error
}

// declare create exchange and queue
Expand Down
Loading