forked from st3v/go-plugins
-
Notifications
You must be signed in to change notification settings - Fork 0
/
connection.go
156 lines (128 loc) · 2.83 KB
/
connection.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
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
package rabbitmq
//
// All credit to Mondo
//
import (
"crypto/tls"
"strings"
"sync"
"time"
"github.com/streadway/amqp"
)
var (
DefaultExchange = "micro"
DefaultRabbitURL = "amqp://guest:guest@127.0.0.1:5672"
)
type rabbitMQConn struct {
Connection *amqp.Connection
Channel *rabbitMQChannel
notify chan bool
exchange string
url string
connected bool
mtx sync.Mutex
close chan bool
closed bool
}
func newRabbitMQConn(exchange string, urls []string) *rabbitMQConn {
var url string
if len(urls) > 0 && strings.HasPrefix(urls[0], "amqp://") {
url = urls[0]
} else {
url = DefaultRabbitURL
}
if len(exchange) == 0 {
exchange = DefaultExchange
}
return &rabbitMQConn{
exchange: exchange,
url: url,
notify: make(chan bool, 1),
close: make(chan bool),
}
}
func (r *rabbitMQConn) Init(secure bool, config *tls.Config) chan bool {
go r.Connect(secure, config, r.notify)
return r.notify
}
func (r *rabbitMQConn) Connect(secure bool, config *tls.Config, connected chan bool) {
for {
if err := r.tryToConnect(secure, config); err != nil {
time.Sleep(1 * time.Second)
continue
}
connected <- true
r.connected = true
notifyClose := make(chan *amqp.Error)
r.Connection.NotifyClose(notifyClose)
// Block until we get disconnected, or shut down
select {
case <-notifyClose:
// Spin around and reconnect
r.connected = false
case <-r.close:
// Shut down connection
if err := r.Connection.Close(); err != nil {
}
r.connected = false
return
}
}
}
func (r *rabbitMQConn) IsConnected() bool {
return r.connected
}
func (r *rabbitMQConn) Close() {
r.mtx.Lock()
defer r.mtx.Unlock()
if r.closed {
return
}
close(r.close)
r.closed = true
}
func (r *rabbitMQConn) tryToConnect(secure bool, config *tls.Config) error {
var err error
if secure || config != nil {
if config == nil {
config = &tls.Config{
InsecureSkipVerify: true,
}
}
url := strings.Replace(r.url, "amqp://", "amqps://", 1)
r.Connection, err = amqp.DialTLS(url, config)
} else {
r.Connection, err = amqp.Dial(r.url)
}
if err != nil {
return err
}
r.Channel, err = newRabbitChannel(r.Connection)
if err != nil {
return err
}
r.Channel.DeclareExchange(r.exchange)
return nil
}
func (r *rabbitMQConn) Consume(queue string) (<-chan amqp.Delivery, error) {
consumerChannel, err := newRabbitChannel(r.Connection)
if err != nil {
return nil, err
}
err = consumerChannel.DeclareQueue(queue)
if err != nil {
return nil, err
}
deliveries, err := consumerChannel.ConsumeQueue(queue)
if err != nil {
return nil, err
}
err = consumerChannel.BindQueue(queue, r.exchange)
if err != nil {
return nil, err
}
return deliveries, nil
}
func (r *rabbitMQConn) Publish(exchange, key string, msg amqp.Publishing) error {
return r.Channel.Publish(exchange, key, msg)
}