-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtopic.go
98 lines (91 loc) · 2.47 KB
/
topic.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
package hop
import (
"github.com/pkg/errors"
"github.com/streadway/amqp"
)
// Topic represents a named "tube" from which jobs can be exclusively pulled and
// put into. Underneath, each topic corresponds to a queue bound to a direct
// exchange, and each topic instance manages its own channel.
type Topic struct {
queue *WorkQueue
name string
config *Config
}
// GetTopic returns a "tube" handle, from which to pull and put jobs into.
// Thanks to AMQP protocol rules, Topic declarations are idempotent, meaning
// that Topics only get created if they don't already exist.
func (q *WorkQueue) GetTopic(name string) (*Topic, error) {
ch, err := q.getChannel()
if err != nil {
return nil, errors.Wrap(err, "error getting channel")
}
defer q.putChannel(ch)
_, err = ch.QueueDeclare(name, q.config.Persistent,
false, false, false, nil)
if err != nil {
return nil, errors.Wrap(err, "error declaring queue")
}
err = ch.QueueBind(name, name, q.config.ExchangeName, true, nil)
if err != nil {
return nil, errors.Wrap(err, "unable to bind queue")
}
return &Topic{
queue: q,
name: name,
config: q.config,
}, nil
}
// Name returns the Topic's name.
func (t *Topic) Name() string {
return t.name
}
// Pull blocks until a Job is available to consume and returns it.
func (t *Topic) Pull() (*Job, error) {
var (
del amqp.Delivery
ok bool
err error
)
ch, err := t.queue.getChannel()
if err != nil {
return nil, errors.Wrap(err, "error getting channel")
}
for !ok {
del, ok, err = ch.Get(t.name, false)
if err != nil {
return nil, errors.Wrap(err, "error getting message")
}
}
return &Job{
topic: t,
del: &del,
ch: ch,
}, nil
}
// Put places a Job in the queue with the given body. Use PutPublishing if you
// need more control over your messages.
func (t *Topic) Put(body []byte) error {
deliveryMode := amqp.Transient
if t.config.Persistent {
deliveryMode = amqp.Persistent
}
p := &amqp.Publishing{
DeliveryMode: deliveryMode,
Body: body,
}
return t.PutPublishing(p)
}
// PutPublishing puts an AMQP message into the queue. Use this if you need more
// granularity in your message parameters.
func (t *Topic) PutPublishing(p *amqp.Publishing) error {
ch, err := t.queue.getChannel()
if err != nil {
return errors.Wrap(err, "error getting channel")
}
defer t.queue.putChannel(ch)
err = ch.Publish(t.config.ExchangeName, t.name, false, false, *p)
if err != nil {
return errors.Wrap(err, "error publishing message")
}
return nil
}