/
queue.go
52 lines (41 loc) · 1.04 KB
/
queue.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
// Copyright 2015 Jonah Glover. All rights reserved.
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE fedle.
package hungry
import (
"github.com/streadway/amqp"
)
type Queue struct {
Name string
Key string
durable bool
readPump chan Message
writePump chan Message
}
func (q *Queue) bindMessages(msgs <-chan amqp.Delivery) {
log.Info("Binding messages to queue " + q.Name)
for {
for d := range msgs {
q.readPump <- Message{Body: d.Body}
}
}
}
func (q *Queue) bind(msgs <-chan amqp.Delivery) {
go q.bindMessages(msgs)
}
func NewQueue(ch *amqp.Channel, name string, options ...func(*Queue) error) (*Queue, error) {
q := Queue{Name: name, readPump: make(chan Message), writePump: make(chan Message)}
for _, option := range options {
option(&q)
}
mq, err := ch.QueueDeclare(
q.Name, // name
q.durable, // durable
false, // delete when unused
false, // exclusive
false, // no-wait
nil, // arguments
)
q.Key = mq.Name
return &q, err
}