-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathsubscriber.go
More file actions
206 lines (180 loc) · 4.04 KB
/
subscriber.go
File metadata and controls
206 lines (180 loc) · 4.04 KB
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
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
package main
import (
"time"
log "github.com/Sirupsen/logrus"
"github.com/streadway/amqp"
"golang.org/x/net/context"
)
const totalRetries = 10
const retryDelay = 15
type session struct {
*amqp.Connection
*amqp.Channel
}
// Subscriber represents a single consumer to an exchange
type Subscriber struct {
ctx context.Context
done context.CancelFunc
uri string
exchange string
exchangeType string
queue string
key string
ctag string
retries int
Messages chan *amqp.Delivery
}
// NewSubscriber creates and initializes a new subscriber
func NewSubscriber(ctx context.Context, done context.CancelFunc, uri, exchange, exchangeType, queue, key, ctag string) (*Subscriber, error) {
messages := make(chan *amqp.Delivery)
retries := 0
s := &Subscriber{
ctx,
done,
uri,
exchange,
exchangeType,
queue,
key,
ctag,
retries,
messages,
}
return s, nil
}
// Consume begins the subscribe and consume loop of a subscruber. Messages will be pumped through the Messages channel.
func (subscriber *Subscriber) Consume() {
go func() {
subscriber.subscribeAndConsume()
subscriber.done()
}()
}
// Close cleans up a subscriber and it's resources
func (subscriber *Subscriber) Close() {
close(subscriber.Messages)
}
func (subscriber *Subscriber) redial() chan chan session {
sessions := make(chan chan session)
go func() {
s := make(chan session)
defer close(sessions)
for {
if subscriber.retries > totalRetries {
log.Fatal("Too many retries")
}
if subscriber.retries >= 1 {
select {
case <-subscriber.ctx.Done():
log.Info("Shutting down session factory")
return
default:
break
}
log.Info("Waiting to retry")
time.Sleep(time.Second * retryDelay)
} else {
select {
case sessions <- s:
case <-subscriber.ctx.Done():
log.Info("Shutting down session factory")
return
}
}
subscriber.retries++
log.WithFields(log.Fields{
"uri": subscriber.uri,
}).Info("Dialing")
conn, err := amqp.Dial(subscriber.uri)
if err != nil {
log.Error(err)
continue
}
log.Info("Opening channel")
ch, err := conn.Channel()
if err != nil {
log.Error(err)
continue
}
log.WithFields(log.Fields{
"exchange": subscriber.exchange,
"type": subscriber.exchangeType,
}).Info("Declaring exchange")
err = ch.ExchangeDeclare(
subscriber.exchange,
subscriber.exchangeType,
true, // durable
false, // delete when complete
false, // internal
false, // noWait
nil, // arguments
)
if err != nil {
log.Error(err)
continue
}
// If we get here, we are connected succesfully, reset retries
subscriber.retries = 0
select {
case s <- session{conn, ch}:
case <-subscriber.ctx.Done():
log.Info("Shutting down new session")
}
}
}()
return sessions
}
func (subscriber *Subscriber) subscribeAndConsume() {
for session := range subscriber.redial() {
sub := <-session
log.WithFields(log.Fields{
"queue": subscriber.queue,
}).Infoln("Declaring queue")
if _, err := sub.QueueDeclare(
subscriber.queue,
true, // durable
false, // delete when unused
false, // exclusive
false, // noWait
nil, // arguments)
); err != nil {
log.Error(err)
return
}
log.WithFields(log.Fields{
"queue": subscriber.queue,
"key": subscriber.key,
"exchange": subscriber.exchange,
}).Infoln("Binding queue")
if err := sub.QueueBind(
subscriber.queue,
subscriber.key,
subscriber.exchange,
false,
nil,
); err != nil {
log.Error(err)
return
}
log.WithFields(log.Fields{
"queue": subscriber.queue,
"ctag": subscriber.ctag,
}).Infoln("Consuming")
deliveries, err := sub.Consume(
subscriber.queue,
subscriber.ctag,
false, // noAck
false, // exclusive
false, // noLocal
false, // noWait
nil, // arguments
)
if err != nil {
log.Error(err)
return
}
for msg := range deliveries {
subscriber.Messages <- &msg
sub.Ack(msg.DeliveryTag, false)
}
}
}