-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathutils.go
224 lines (188 loc) · 5.1 KB
/
utils.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
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
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
package cfrabbit
import (
"errors"
"github.com/r-franke/cfrabbit/config"
amqp "github.com/rabbitmq/amqp091-go"
"strings"
"time"
)
type Session struct {
connection *amqp.Connection
channel *amqp.Channel
done chan bool
notifyConnClose chan *amqp.Error
notifyChanClose chan *amqp.Error
notifyConfirm chan amqp.Confirmation
isReady bool
}
const (
reconnectDelay = 5 * time.Second
reInitDelay = 2 * time.Second
maxRetries = 5
)
var (
errNotConnected = errors.New("not connected to a server")
errAlreadyClosed = errors.New("already closed: not connected to the server")
reconnectRetries = 0
)
//goland:noinspection GoUnusedExportedFunction
func New() *Session {
session := Session{
done: make(chan bool),
}
go session.handleReconnect(config.RMQConnectionString)
// Wait until session ready
for {
if session.isReady {
break
}
}
return &session
}
// handleReconnect will wait for a connection error on
// notifyConnClose, and then continuously attempt to reconnect.
func (s *Session) handleReconnect(addr string) {
for {
s.isReady = false
conn, err := s.connect(addr)
if err != nil {
if reconnectRetries > maxRetries {
config.ErrorLogger.Fatalf("Max retries for reconnect reached, restarting...")
}
reconnectRetries++
config.ErrorLogger.Println("Failed to connect:")
config.ErrorLogger.Println(err)
config.ErrorLogger.Printf("Retrying attempt %d out of %d...", reconnectRetries, maxRetries)
select {
case <-s.done:
return
case <-time.After(reconnectDelay):
}
continue
}
reconnectRetries = 0
if done := s.handleReInit(conn); done {
break
}
}
}
// connect will create a new AMQP connection
func (s *Session) connect(addr string) (*amqp.Connection, error) {
conn, err := amqp.DialTLS(addr, &config.TlsConfig)
if err != nil {
return nil, err
}
s.changeConnection(conn)
return conn, nil
}
// handleReconnect will wait for a channel error
// and then continuously attempt to re-initialize both channels
func (s *Session) handleReInit(conn *amqp.Connection) bool {
for {
s.isReady = false
err := s.init(conn)
if err != nil {
config.ErrorLogger.Println("Failed to initialize channel. Retrying...")
select {
case <-s.done:
return true
case <-time.After(reInitDelay):
}
continue
}
select {
case <-s.done:
return true
case <-s.notifyConnClose:
config.InfoLogger.Println("Connection closed. Reconnecting...")
return false
case <-s.notifyChanClose:
config.InfoLogger.Println("Channel closed. Re-running init...")
}
}
}
// init will initialize channel
func (s *Session) init(conn *amqp.Connection) error {
ch, err := conn.Channel()
if err != nil {
return err
}
err = ch.Confirm(false)
if err != nil {
return err
}
s.changeChannel(ch)
s.isReady = true
return nil
}
// changeConnection takes a new connection to the queue,
// and updates the close listener to reflect this.
func (s *Session) changeConnection(connection *amqp.Connection) {
s.connection = connection
s.notifyConnClose = make(chan *amqp.Error)
s.connection.NotifyClose(s.notifyConnClose)
}
// changeChannel takes a new channel to the queue,
// and updates the channel listeners to reflect this.
func (s *Session) changeChannel(channel *amqp.Channel) {
s.channel = channel
s.notifyChanClose = make(chan *amqp.Error)
s.notifyConfirm = make(chan amqp.Confirmation, 1)
s.channel.NotifyClose(s.notifyChanClose)
s.channel.NotifyPublish(s.notifyConfirm)
}
//goland:noinspection GoUnusedExportedFunction
func CreateAndBindQueue(exchangeName, exchangeType, queueName string, routingkeys []string) error {
session := New()
if !session.isReady {
return errNotConnected
}
config.InfoLogger.Printf("Declaring exchange: %s, with type: %s\n", exchangeName, exchangeType)
err := session.channel.ExchangeDeclare(exchangeName, exchangeType, true, false, false, false, nil)
if err != nil {
return err
}
config.InfoLogger.Printf("Declaring queue: %s\n", queueName)
_, err = session.channel.QueueDeclare(
queueName,
true, // Durable
strings.Contains(queueName, "-dev-"), // Delete when unused
false, // Exclusive
false, // No-wait
nil, // Arguments
)
if err != nil {
return err
}
for _, rk := range routingkeys {
config.InfoLogger.Printf("Binding queue: %s to exchangeName: %s with routingkey: %s\n", queueName, exchangeName, rk)
err = session.channel.QueueBind(queueName, rk, exchangeName, false, nil)
if err != nil {
return err
}
}
_ = session.Close()
return nil
}
//goland:noinspection GoUnusedExportedFunction
func UnbindQueue(queueName, key, exchange string) error {
session := New()
if !session.isReady {
return errNotConnected
}
err := session.channel.QueueUnbind(queueName, key, exchange, amqp.Table{})
_ = session.Close()
return err
}
// Close will cleanly shutdown the channel and connection.
func (s *Session) Close() error {
if !s.isReady {
return errAlreadyClosed
}
s.done <- true
err := s.channel.Close()
if err != nil {
return err
}
return nil
}