-
Notifications
You must be signed in to change notification settings - Fork 0
/
pool.go
163 lines (147 loc) · 3.92 KB
/
pool.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
package fluffle
import (
"fmt"
"github.com/rabbitmq/amqp091-go"
"sync"
"time"
)
var connectionPool chan *connection
type RabbitConfig struct {
UserName string
Password string
Host string
Port int
ChannelLimitPerConn int
}
const (
reConnectTime = time.Second
)
type connection struct {
*amqp091.Connection
channelCount int
isPrimary bool
chanClose chan bool
syncMutex sync.Mutex
}
type iLogger interface {
Fatal(message2 string, err error, params map[string]interface{})
Error(message2 string, err error, params map[string]interface{})
Info(message2 string, params map[string]interface{})
Trace(message2 string, params map[string]interface{})
}
var logger iLogger
var config RabbitConfig
var poolStarted bool
//"amqp://"+config.UserName+":"+config.Password+"@"+config.Host+":"+config.Port+"/"
func getConnectionString(config RabbitConfig) string {
return fmt.Sprintf("amqp://%s:%s@%s:%d/", config.UserName, config.Password, config.Host, config.Port)
}
func startAmqpConnection() (conn *amqp091.Connection) {
var err error
for {
conn, err = amqp091.DialConfig(getConnectionString(config), amqp091.Config{
Heartbeat: 20 * time.Second,
Locale: "en_US",
})
if err != nil {
logger.Error("cannot (re)dial: %v: %q", err, nil)
time.Sleep(reConnectTime)
continue
}
break
}
return conn
}
func newConnection(connectionsChan chan *connection) {
conn := startAmqpConnection()
go func() {
closeErr := <-conn.NotifyClose(make(chan *amqp091.Error))
if closeErr != nil {
logger.Error("CONNECTION_CLOSE_NOTIf", closeErr, map[string]interface{}{
"initiating_server": closeErr.Server,
"recoverable": closeErr.Recover,
})
}
newConnection(connectionsChan)
}()
go func() {
conn := &connection{
Connection: conn,
channelCount: 0,
isPrimary: true,
chanClose: make(chan bool),
syncMutex: sync.Mutex{},
}
go conn.listenToChannelClose()
connectionsChan <- conn
}()
}
// gets a connection from the connection pool, opens a channel and pushes the connection back to the pool
// also handles cases where connection is no longer open
func getChannel() (rChan *rabbitChannel) {
var err error
var ch *amqp091.Channel
var conn *connection
for {
conn = <-connectionPool
conn.syncMutex.Lock()
if conn.IsClosed() { // discard this connection if it's closed
if conn.isPrimary { // if it's the only connection, then start a new one
newConnection(connectionPool)
}
conn.syncMutex.Unlock()
continue
}
usableConnection := config.ChannelLimitPerConn == 0 || conn.channelCount < config.ChannelLimitPerConn
if usableConnection {
ch, err = conn.Channel()
if err != nil {
usableConnection = false
} else {
rChan = &rabbitChannel{
amqpChan: ch,
isConnected: true,
}
go rChan.listenToClose(conn.chanClose)
conn.channelCount++
conn.syncMutex.Unlock()
}
}
if !usableConnection { // spawn a new connection as the primary connection, push the current one back to pool to be used later and retry the loop
if conn.isPrimary {
newConnection(connectionPool)
conn.isPrimary = false
}
go func() { connectionPool <- conn }()
conn.syncMutex.Unlock()
continue
}
break
}
go func() { connectionPool <- conn }() // puts the connection back to pool after use
return
}
func (c *connection) listenToChannelClose() {
for <-c.chanClose {
c.syncMutex.Lock()
c.channelCount--
if c.channelCount == 0 && !c.isPrimary {
c.Close()
close(c.chanClose)
c.syncMutex.Unlock()
return
}
c.syncMutex.Unlock()
}
}
func (t *rabbitChannel) listenToClose(chanClose chan bool) {
closeErr := <-t.amqpChan.NotifyClose(make(chan *amqp091.Error))
if closeErr != nil {
logger.Error("CHANNEL_CLOSE_NOTIf", closeErr, map[string]interface{}{
"initiating_server": closeErr.Server,
"recoverable": closeErr.Recover,
})
}
t.isConnected = false
chanClose <- true
}