/
consumer.go
49 lines (40 loc) · 996 Bytes
/
consumer.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
package queue
import (
"fmt"
"github.com/streadway/amqp"
)
type Consumer struct {
Conn *amqp.Connection
Channel *amqp.Channel
Tag string
Done chan error
}
// Build a Consumer to use in Workers that will consume an AMQP queue
//
// @param {String} tag(consumer_tag)
// @param {*amqp.Connection} connection
// @return {*Consumer}, {error}
// @api public
func BuildConsumer(tag string, connection *amqp.Connection) (*Consumer, error) {
c := &Consumer{
Conn: connection,
Channel: nil,
Tag: tag,
Done: make(chan error),
}
var err error
c.Channel, err = connection.Channel()
if err != nil {
return nil, fmt.Errorf("Channel: %s", err)
}
return c, nil
}
// Shutdown a Consumer by cancelling the delivery channel
func (c *Consumer) Shutdown() error {
// will close() the deliveries channel
if err := c.Channel.Cancel(c.Tag, true); err != nil {
return fmt.Errorf("Consumer cancel failed: %s", err)
}
// wait for handle() to exit
return <-c.Done
}