-
Notifications
You must be signed in to change notification settings - Fork 19
/
consumer.go
139 lines (115 loc) · 2.83 KB
/
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
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
package mq
import (
"sync"
"github.com/NeowayLabs/wabbit"
)
// Consumer describes available methods for consumer.
type Consumer interface {
// Consume runs consumer's workers with specified handler.
Consume(handler ConsumerHandler)
}
// ConsumerHandler describes handler function signature.
// It will be called for each obtained message.
type ConsumerHandler func(message Message)
// Message describes available methods of the message obtained from queue.
type Message interface {
Ack(multiple bool) error
Nack(multiple, requeue bool) error
Reject(requeue bool) error
Body() []byte
}
type consumer struct {
handler ConsumerHandler
once sync.Once
workers []*worker
// Options for reconnect.
queue string
name string
options wabbit.Option
prefetchCount int
prefetchSize int
}
func newConsumer(config ConsumerConfig) *consumer {
return &consumer{
workers: make([]*worker, config.Workers),
queue: config.Queue,
name: config.Name,
options: wabbit.Option(config.Options),
}
}
// Consume sets handler for incoming messages and runs it.
// Can be called only once.
func (consumer *consumer) Consume(handler ConsumerHandler) {
consumer.once.Do(func() {
consumer.consume(handler)
})
}
func (consumer *consumer) consume(handler ConsumerHandler) {
consumer.handler = handler
for _, worker := range consumer.workers {
go worker.Run(handler)
}
}
// Stop terminates consumer's workers.
func (consumer *consumer) Stop() {
for _, worker := range consumer.workers {
worker.Stop()
}
}
type worker struct {
sync.Mutex // Protect channel during reconnect.
workerStatus
channel wabbit.Channel
deliveries <-chan wabbit.Delivery
errorChannel chan<- error
shutdownChannel chan chan struct{}
}
func newWorker(errorChannel chan<- error) *worker {
return &worker{
errorChannel: errorChannel,
shutdownChannel: make(chan chan struct{}),
}
}
func (worker *worker) Run(handler ConsumerHandler) {
worker.markAsRunning()
for {
select {
case message := <-worker.deliveries:
if message == nil { // It seems like channel was closed.
if worker.markAsStoppedIfCan() {
// Stop the worker.
return
}
// Somebody is already trying to stop the worker.
continue
}
handler(message)
case done := <-worker.shutdownChannel:
worker.closeChannel()
close(done)
return
}
}
}
// Method safely sets new RMQ channel.
func (worker *worker) setChannel(channel wabbit.Channel) {
worker.Lock()
worker.channel = channel
worker.Unlock()
}
// Close worker's channel.
func (worker *worker) closeChannel() {
worker.Lock()
if err := worker.channel.Close(); err != nil {
worker.errorChannel <- err
}
worker.Unlock()
}
// Force stop.
func (worker *worker) Stop() {
if worker.markAsStoppedIfCan() {
done := make(chan struct{})
worker.shutdownChannel <- done
<-done
}
}