Skip to content

Commit 0154a51

Browse files
committed
fix(clickhouse): use atomic bool for closed flag
1 parent 901e11c commit 0154a51

File tree

1 file changed

+8
-6
lines changed

1 file changed

+8
-6
lines changed

clickhouse.go

Lines changed: 8 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -73,6 +73,7 @@ func Open(opt *Options) (driver.Conn, error) {
7373
idle: newIdlePool(o.ConnMaxLifetime, o.MaxIdleConns),
7474
open: make(chan struct{}, o.MaxOpenConns),
7575
closeOnce: &sync.Once{},
76+
closed: &atomic.Bool{},
7677
}
7778

7879
return conn, nil
@@ -105,10 +106,11 @@ type clickhouse struct {
105106
opt *Options
106107
connID int64
107108

108-
idle *idlePool
109-
open chan struct{}
109+
idle *idlePool
110+
open chan struct{}
111+
110112
closeOnce *sync.Once
111-
closed bool
113+
closed *atomic.Bool
112114
}
113115

114116
func (clickhouse) Contributors() []string {
@@ -282,7 +284,7 @@ func DefaultDialStrategy(ctx context.Context, connID int, opt *Options, dial Dia
282284
}
283285

284286
func (ch *clickhouse) acquire(ctx context.Context) (conn nativeTransport, err error) {
285-
if ch.closed {
287+
if ch.closed.Load() {
286288
return nil, ErrConnectionClosed
287289
}
288290

@@ -351,7 +353,7 @@ func (ch *clickhouse) release(conn nativeTransport, err error) {
351353
conn.freeBuffer()
352354
}
353355

354-
if ch.closed {
356+
if ch.closed.Load() {
355357
return
356358
}
357359

@@ -361,7 +363,7 @@ func (ch *clickhouse) release(conn nativeTransport, err error) {
361363
func (ch *clickhouse) Close() (err error) {
362364
ch.closeOnce.Do(func() {
363365
err = ch.idle.Close()
364-
ch.closed = true
366+
ch.closed.Store(true)
365367
})
366368

367369
return

0 commit comments

Comments
 (0)