Skip to content

Commit 2cfa156

Browse files
committed
use atomic.Bool for closed bool
1 parent 3b6ec40 commit 2cfa156

File tree

1 file changed

+28
-37
lines changed

1 file changed

+28
-37
lines changed

clients/tfchain-client-go/impl.go

Lines changed: 28 additions & 37 deletions
Original file line numberDiff line numberDiff line change
@@ -160,28 +160,19 @@ func (m *manager) initializePool() {
160160
}
161161
}
162162

163-
func (m *manager) createConnection(ctx context.Context, url string) (*poolConn, error) {
163+
func (m *manager) createConnection(url string) (*poolConn, error) {
164164
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")
185176
}
186177
return nil, fmt.Errorf("failed to create connection to %s", url)
187178
}
@@ -239,11 +230,7 @@ func (m *manager) getHealthyConn() (*poolConn, error) {
239230
return ErrNoConnectionsAvailable
240231
}, b)
241232

242-
if err != nil {
243-
return nil, err
244-
}
245-
246-
return conn, nil
233+
return conn, err
247234
}
248235

249236
func (m *manager) healthChecker() {
@@ -303,7 +290,7 @@ func (m *manager) ensureMinConnections() {
303290
poolSize := len(m.pool)
304291

305292
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 {
307294
m.mu.Lock()
308295
m.pool = append(m.pool, conn)
309296
m.mu.Unlock()
@@ -402,7 +389,7 @@ type Substrate struct {
402389
conn *poolConn
403390
mgr *manager
404391
mu sync.Mutex
405-
closed bool
392+
closed atomic.Bool
406393
}
407394

408395
func newSubstrate(conn *poolConn, mgr *manager) *Substrate {
@@ -434,18 +421,23 @@ func createSubstrateConn(url string) (Conn, Meta, error) {
434421
}
435422

436423
func (s *Substrate) GetClient() (Conn, Meta, error) {
437-
if s.closed {
424+
if s.closed.Load() {
438425
log.Error().Msg("attempted to get client from closed substrate")
439426
return nil, nil, fmt.Errorf("substrate connection closed")
440427
}
441428

442-
if s.conn.isHealthy() {
429+
s.mu.Lock()
430+
if s.conn != nil && s.conn.isHealthy() {
443431
conn := s.conn.conn
444432
meta := s.conn.meta
445433
s.conn.lastUsed.Store(time.Now().Unix())
434+
s.mu.Unlock()
446435
return conn, meta, nil
447436
}
448-
s.conn.inUse.Store(false)
437+
if s.conn != nil {
438+
s.conn.inUse.Store(false)
439+
}
440+
s.mu.Unlock()
449441

450442
conn, err := s.mgr.getHealthyConn()
451443
if err != nil {
@@ -454,22 +446,21 @@ func (s *Substrate) GetClient() (Conn, Meta, error) {
454446
}
455447

456448
s.mu.Lock()
449+
defer s.mu.Unlock()
457450

458451
s.conn = conn
459-
s.mu.Unlock()
460452

461453
log.Debug().Str("url", conn.url).Msg("swapped connection")
462454
return conn.conn, conn.meta, nil
463455
}
464456

465457
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) {
470459
return
471460
}
472-
s.closed = true
461+
462+
s.mu.Lock()
463+
defer s.mu.Unlock()
473464

474465
if s.conn != nil {
475466
s.conn.inUse.Store(false)

0 commit comments

Comments
 (0)