-
Notifications
You must be signed in to change notification settings - Fork 26
/
Copy pathconn_pool.go
275 lines (242 loc) · 5.93 KB
/
conn_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
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
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
package surveyor
import (
"crypto/sha256"
"crypto/tls"
"encoding/json"
"fmt"
"os"
"sync"
"github.com/nats-io/nats.go"
"github.com/sirupsen/logrus"
"golang.org/x/sync/singleflight"
)
type natsContext struct {
Name string `json:"name"`
URL string `json:"url"`
JWT string `json:"jwt"`
Seed string `json:"seed"`
Credentials string `json:"credential"`
Nkey string `json:"nkey"`
Token string `json:"token"`
Username string `json:"username"`
Password string `json:"password"`
TLSCA string `json:"tls_ca"`
TLSCert string `json:"tls_cert"`
TLSKey string `json:"tls_key"`
// only passed programmatically
NatsOptsID string `json:"nats_opts_id"`
NatsOpts []nats.Option `json:"-"`
}
func (c *natsContext) copy() *natsContext {
if c == nil {
return nil
}
cp := *c
return &cp
}
func (c *natsContext) hash() (string, error) {
b, err := json.Marshal(c)
if err != nil {
return "", fmt.Errorf("error marshaling context to json: %v", err)
}
if c.Nkey != "" {
fb, err := os.ReadFile(c.Nkey)
if err != nil {
return "", fmt.Errorf("error opening nkey file %s: %v", c.Nkey, err)
}
b = append(b, fb...)
}
if c.Credentials != "" {
fb, err := os.ReadFile(c.Credentials)
if err != nil {
return "", fmt.Errorf("error opening creds file %s: %v", c.Credentials, err)
}
b = append(b, fb...)
}
if c.TLSCA != "" {
fb, err := os.ReadFile(c.TLSCA)
if err != nil {
return "", fmt.Errorf("error opening ca file %s: %v", c.TLSCA, err)
}
b = append(b, fb...)
}
if c.TLSCert != "" {
fb, err := os.ReadFile(c.TLSCert)
if err != nil {
return "", fmt.Errorf("error opening cert file %s: %v", c.TLSCert, err)
}
b = append(b, fb...)
}
if c.TLSKey != "" {
fb, err := os.ReadFile(c.TLSKey)
if err != nil {
return "", fmt.Errorf("error opening key file %s: %v", c.TLSKey, err)
}
b = append(b, fb...)
}
hash := sha256.New()
hash.Write(b)
return fmt.Sprintf("%x", hash.Sum(nil)), nil
}
type natsContextDefaults struct {
Name string
URL string
TLSCA string
TLSCert string
TLSKey string
TLSConfig *tls.Config
}
type pooledNatsConn struct {
nc *nats.Conn
cp *natsConnPool
key string
count uint64
closed bool
}
func (pc *pooledNatsConn) ReturnToPool() {
pc.cp.Lock()
pc.count--
if pc.count == 0 {
if pooledConn, ok := pc.cp.cache[pc.key]; ok && pc == pooledConn {
delete(pc.cp.cache, pc.key)
}
pc.closed = true
pc.cp.Unlock()
pc.nc.Close()
return
}
pc.cp.Unlock()
}
type natsConnPool struct {
sync.Mutex
cache map[string]*pooledNatsConn
logger *logrus.Logger
group *singleflight.Group
natsDefaults *natsContextDefaults
natsOpts []nats.Option
}
func newNatsConnPool(logger *logrus.Logger, natsDefaults *natsContextDefaults, natsOpts []nats.Option) *natsConnPool {
return &natsConnPool{
cache: map[string]*pooledNatsConn{},
group: &singleflight.Group{},
logger: logger,
natsDefaults: natsDefaults,
natsOpts: natsOpts,
}
}
const getPooledConnMaxTries = 10
// Get returns a *pooledNatsConn
func (cp *natsConnPool) Get(cfg *natsContext) (*pooledNatsConn, error) {
if cfg == nil {
return nil, fmt.Errorf("nats context must not be nil")
}
// copy cfg
cfg = cfg.copy()
// set defaults
if cfg.Name == "" {
cfg.Name = cp.natsDefaults.Name
}
if cfg.URL == "" {
cfg.URL = cp.natsDefaults.URL
}
if cfg.TLSCA == "" {
cfg.TLSCA = cp.natsDefaults.TLSCA
}
if cfg.TLSCert == "" {
cfg.TLSCert = cp.natsDefaults.TLSCert
}
if cfg.TLSKey == "" {
cfg.TLSKey = cp.natsDefaults.TLSKey
}
// get hash
key, err := cfg.hash()
if err != nil {
return nil, err
}
for i := 0; i < getPooledConnMaxTries; i++ {
connection, err := cp.getPooledConn(key, cfg)
if err != nil {
return nil, err
}
cp.Lock()
if connection.closed {
// ReturnToPool closed this while lock not held, try again
cp.Unlock()
continue
}
// increment count out of the pool
connection.count++
cp.Unlock()
return connection, nil
}
return nil, fmt.Errorf("failed to get pooled connection after %d attempts", getPooledConnMaxTries)
}
// getPooledConn gets or establishes a *pooledNatsConn in a singleflight group, but does not increment its count
func (cp *natsConnPool) getPooledConn(key string, cfg *natsContext) (*pooledNatsConn, error) {
conn, err, _ := cp.group.Do(key, func() (interface{}, error) {
cp.Lock()
pooledConn, ok := cp.cache[key]
if ok && pooledConn.nc.IsConnected() {
cp.Unlock()
return pooledConn, nil
}
cp.Unlock()
opts := append(cp.natsOpts, cfg.NatsOpts...)
opts = append(opts, func(options *nats.Options) error {
if cfg.Name != "" {
options.Name = cfg.Name
}
if cfg.Token != "" {
options.Token = cfg.Token
}
if cfg.Username != "" {
options.User = cfg.Username
}
if cfg.Password != "" {
options.Password = cfg.Password
}
return nil
})
if cfg.JWT != "" && cfg.Seed != "" {
opts = append(opts, nats.UserJWTAndSeed(cfg.JWT, cfg.Seed))
}
if cfg.Nkey != "" {
opt, err := nats.NkeyOptionFromSeed(cfg.Nkey)
if err != nil {
return nil, fmt.Errorf("unable to load nkey: %v", err)
}
opts = append(opts, opt)
}
if cfg.Credentials != "" {
opts = append(opts, nats.UserCredentials(cfg.Credentials))
}
if cfg.TLSCA != "" {
opts = append(opts, nats.RootCAs(cfg.TLSCA))
}
if cfg.TLSCert != "" && cfg.TLSKey != "" {
opts = append(opts, nats.ClientCert(cfg.TLSCert, cfg.TLSKey))
}
nc, err := nats.Connect(cfg.URL, opts...)
if err != nil {
return nil, err
}
cp.logger.Infof("%s connected to NATS Deployment: %s", cfg.Name, nc.ConnectedAddr())
connection := &pooledNatsConn{
nc: nc,
cp: cp,
key: key,
}
cp.Lock()
cp.cache[key] = connection
cp.Unlock()
return connection, err
})
if err != nil {
return nil, err
}
connection, ok := conn.(*pooledNatsConn)
if !ok {
return nil, fmt.Errorf("not a pooledNatsConn")
}
return connection, nil
}