-
Notifications
You must be signed in to change notification settings - Fork 18
/
Copy pathconsumer.go
54 lines (45 loc) · 1012 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
50
51
52
53
54
//nolint:dupl // ignore linter err
package amqpextra
import (
"github.com/makasim/amqpextra/consumer"
)
func NewConsumer(
connCh <-chan *Connection,
opts ...consumer.Option,
) (*consumer.Consumer, error) {
consumerConnCh := make(chan *consumer.Connection)
c, err := consumer.New(consumerConnCh, opts...)
if err != nil {
return nil, err
}
go proxyConsumerConn(connCh, consumerConnCh, c.NotifyClosed())
return c, nil
}
//nolint:dupl // ignore linter err
func proxyConsumerConn(
connCh <-chan *Connection,
consumerConnCh chan *consumer.Connection,
consumerCloseCh <-chan struct{},
) {
go func() {
defer close(consumerConnCh)
for {
select {
case conn, ok := <-connCh:
if !ok {
return
}
consumerConn := consumer.NewConnection(conn.AMQPConnection(), conn.NotifyLost())
select {
case consumerConnCh <- consumerConn:
case <-conn.NotifyLost():
continue
case <-consumerCloseCh:
return
}
case <-consumerCloseCh:
return
}
}
}()
}