-
Notifications
You must be signed in to change notification settings - Fork 1
/
amqp_conn.go
130 lines (114 loc) · 3.06 KB
/
amqp_conn.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
package main
import (
"context"
"crypto/tls"
"fmt"
"log"
"strings"
"github.com/Azure/go-amqp"
)
type AMQPConnOpts struct {
socketAddr string
sendQueues []string
recvQueues []string
username string
password string
}
type AMQPConn struct {
conn *amqp.Conn
senders []*amqp.Sender
receivers []*amqp.Receiver
}
func newAMQPConn(opts *AMQPConnOpts) (conn *AMQPConn, err error) {
ctx := context.Background()
conn = &AMQPConn{}
// Create outboxClient
connOpts := amqp.ConnOptions{
SASLType: amqp.SASLTypePlain(opts.username, opts.password),
}
if strings.HasPrefix(opts.socketAddr, "amqps://") {
// #nosec G402 - Ignore InsecureSkipVerify: true. This is a benchmark tool, no need to enforce strict security checking
connOpts.TLSConfig = &tls.Config{
InsecureSkipVerify: true,
}
}
conn.conn, err = amqp.Dial(ctx, opts.socketAddr, &connOpts)
if err != nil {
return nil, fmt.Errorf("Dialing AMQP server: %w", err)
}
if len(opts.sendQueues) > 0 {
sendSession, err := conn.conn.NewSession(context.Background(), &amqp.SessionOptions{})
if err != nil {
return nil, fmt.Errorf("Creating AMQP sender session: %w", err)
}
for _, queue := range opts.sendQueues {
// Create outbox sender
sender, err := sendSession.NewSender(
context.Background(),
queue,
&amqp.SenderOptions{
Durability: amqp.DurabilityUnsettledState,
TargetDurability: amqp.DurabilityUnsettledState,
TargetCapabilities: []string{
// QPID broker capabilities
"create-on-demand",
"durable",
"shared",
// Artemis capabilities
"queue",
},
},
)
if err != nil {
return nil, fmt.Errorf("Creating sender link: %w", err)
}
conn.senders = append(conn.senders, sender)
}
}
if len(opts.recvQueues) > 0 {
recvSession, err := conn.conn.NewSession(context.Background(), &amqp.SessionOptions{})
if err != nil {
return nil, fmt.Errorf("Creating AMQP outbox receive session: %w", err)
}
for _, queue := range opts.recvQueues {
// Create outbox reply receiver
receiver, err := recvSession.NewReceiver(
context.Background(),
queue,
&amqp.ReceiverOptions{
Credit: 1000,
Durability: amqp.DurabilityUnsettledState,
SourceDurability: amqp.DurabilityUnsettledState,
SourceCapabilities: []string{
// QPID broker capabilities
"create-on-demand",
"durable",
"shared",
// Artemis capabilities
"queue",
},
},
)
if err != nil {
return nil, fmt.Errorf("Creating receiver link: %w", err)
}
conn.receivers = append(conn.receivers, receiver)
}
}
return conn, nil
}
func (conn *AMQPConn) Close() error {
for _, sender := range conn.senders {
err := sender.Close(context.Background())
if err != nil {
log.Printf("unable to close sender amqp connection. err: %s\n", err.Error())
}
}
for _, receiver := range conn.receivers {
err := receiver.Close(context.Background())
if err != nil {
log.Printf("unable to close receiver amqp connection. err: %s\n", err.Error())
}
}
return conn.conn.Close()
}