@@ -160,28 +160,19 @@ func (m *manager) initializePool() {
160
160
}
161
161
}
162
162
163
- func (m * manager ) createConnection (ctx context. Context , url string ) (* poolConn , error ) {
163
+ func (m * manager ) createConnection (url string ) (* poolConn , error ) {
164
164
log .Debug ().Str ("url" , url ).Msg ("attempting to create a new connection" )
165
- ctx , cancel := context .WithTimeout (ctx , m .config .ConnectionTimeout )
166
- defer cancel ()
167
-
168
- select {
169
- case <- ctx .Done ():
170
- log .Error ().Str ("url" , url ).Msg ("context done while creating connection" )
171
- return nil , ctx .Err ()
172
- default :
173
- if conn , meta , err := createSubstrateConn (url ); err == nil {
174
- log .Debug ().Str ("url" , url ).Msg ("created new connection" )
175
- return & poolConn {
176
- conn : conn ,
177
- meta : meta ,
178
- url : url ,
179
- lastUsed : atomic.Int64 {},
180
- inUse : atomic.Bool {},
181
- }, nil
182
- } else {
183
- log .Error ().Str ("url" , url ).Err (err ).Msg ("failed to create connection" )
184
- }
165
+ if conn , meta , err := createSubstrateConn (url ); err == nil {
166
+ log .Debug ().Str ("url" , url ).Msg ("created new connection" )
167
+ return & poolConn {
168
+ conn : conn ,
169
+ meta : meta ,
170
+ url : url ,
171
+ lastUsed : atomic.Int64 {},
172
+ inUse : atomic.Bool {},
173
+ }, nil
174
+ } else {
175
+ log .Error ().Str ("url" , url ).Err (err ).Msg ("failed to create connection" )
185
176
}
186
177
return nil , fmt .Errorf ("failed to create connection to %s" , url )
187
178
}
@@ -239,11 +230,7 @@ func (m *manager) getHealthyConn() (*poolConn, error) {
239
230
return ErrNoConnectionsAvailable
240
231
}, b )
241
232
242
- if err != nil {
243
- return nil , err
244
- }
245
-
246
- return conn , nil
233
+ return conn , err
247
234
}
248
235
249
236
func (m * manager ) healthChecker () {
@@ -303,7 +290,7 @@ func (m *manager) ensureMinConnections() {
303
290
poolSize := len (m .pool )
304
291
305
292
if poolSize < m .config .MinPoolSize || (poolSize < m .config .MaxPoolSize && poolSize == inUseCount ) {
306
- if conn , err := m .createConnection (m . ctx , url ); err == nil {
293
+ if conn , err := m .createConnection (url ); err == nil {
307
294
m .mu .Lock ()
308
295
m .pool = append (m .pool , conn )
309
296
m .mu .Unlock ()
@@ -402,7 +389,7 @@ type Substrate struct {
402
389
conn * poolConn
403
390
mgr * manager
404
391
mu sync.Mutex
405
- closed bool
392
+ closed atomic. Bool
406
393
}
407
394
408
395
func newSubstrate (conn * poolConn , mgr * manager ) * Substrate {
@@ -434,18 +421,23 @@ func createSubstrateConn(url string) (Conn, Meta, error) {
434
421
}
435
422
436
423
func (s * Substrate ) GetClient () (Conn , Meta , error ) {
437
- if s .closed {
424
+ if s .closed . Load () {
438
425
log .Error ().Msg ("attempted to get client from closed substrate" )
439
426
return nil , nil , fmt .Errorf ("substrate connection closed" )
440
427
}
441
428
442
- if s .conn .isHealthy () {
429
+ s .mu .Lock ()
430
+ if s .conn != nil && s .conn .isHealthy () {
443
431
conn := s .conn .conn
444
432
meta := s .conn .meta
445
433
s .conn .lastUsed .Store (time .Now ().Unix ())
434
+ s .mu .Unlock ()
446
435
return conn , meta , nil
447
436
}
448
- s .conn .inUse .Store (false )
437
+ if s .conn != nil {
438
+ s .conn .inUse .Store (false )
439
+ }
440
+ s .mu .Unlock ()
449
441
450
442
conn , err := s .mgr .getHealthyConn ()
451
443
if err != nil {
@@ -454,22 +446,21 @@ func (s *Substrate) GetClient() (Conn, Meta, error) {
454
446
}
455
447
456
448
s .mu .Lock ()
449
+ defer s .mu .Unlock ()
457
450
458
451
s .conn = conn
459
- s .mu .Unlock ()
460
452
461
453
log .Debug ().Str ("url" , conn .url ).Msg ("swapped connection" )
462
454
return conn .conn , conn .meta , nil
463
455
}
464
456
465
457
func (s * Substrate ) Release () {
466
- s .mu .Lock ()
467
- defer s .mu .Unlock ()
468
-
469
- if s .closed {
458
+ if ! s .closed .CompareAndSwap (false , true ) {
470
459
return
471
460
}
472
- s .closed = true
461
+
462
+ s .mu .Lock ()
463
+ defer s .mu .Unlock ()
473
464
474
465
if s .conn != nil {
475
466
s .conn .inUse .Store (false )
0 commit comments