File tree Expand file tree Collapse file tree 1 file changed +3
-13
lines changed Expand file tree Collapse file tree 1 file changed +3
-13
lines changed Original file line number Diff line number Diff line change @@ -23,7 +23,6 @@ import (
23
23
"io"
24
24
"strconv"
25
25
"sync"
26
- "sync/atomic"
27
26
"time"
28
27
)
29
28
@@ -161,11 +160,10 @@ type Pool struct {
161
160
// the pool does not close connections based on age.
162
161
MaxConnLifetime time.Duration
163
162
164
- chInitialized uint32 // set to 1 when field ch is initialized
165
-
166
163
mu sync.Mutex // mu protects the following fields
167
164
closed bool // set to true when the pool is closed.
168
165
active int // the number of open connections in the pool
166
+ initOnce sync.Once // the init ch once func
169
167
ch chan struct {} // limits open connections when p.Wait is true
170
168
idle idleList // idle connections
171
169
waitCount int64 // total number of connections waited for.
@@ -339,13 +337,7 @@ func (p *Pool) Close() error {
339
337
}
340
338
341
339
func (p * Pool ) lazyInit () {
342
- // Fast path.
343
- if atomic .LoadUint32 (& p .chInitialized ) == 1 {
344
- return
345
- }
346
- // Slow path.
347
- p .mu .Lock ()
348
- if p .chInitialized == 0 {
340
+ p .initOnce .Do (func () {
349
341
p .ch = make (chan struct {}, p .MaxActive )
350
342
if p .closed {
351
343
close (p .ch )
@@ -354,9 +346,7 @@ func (p *Pool) lazyInit() {
354
346
p .ch <- struct {}{}
355
347
}
356
348
}
357
- atomic .StoreUint32 (& p .chInitialized , 1 )
358
- }
359
- p .mu .Unlock ()
349
+ })
360
350
}
361
351
362
352
// waitVacantConn waits for a vacant connection in pool if waiting
You can’t perform that action at this time.
0 commit comments