Skip to content

Commit 9762559

Browse files
authored
Bound connection pool background dials to configured dial timeout (#3089)
1 parent 694a710 commit 9762559

File tree

4 files changed

+18
-2
lines changed

4 files changed

+18
-2
lines changed

internal/pool/bench_test.go

+2
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,7 @@ func BenchmarkPoolGetPut(b *testing.B) {
3333
Dialer: dummyDialer,
3434
PoolSize: bm.poolSize,
3535
PoolTimeout: time.Second,
36+
DialTimeout: 1 * time.Second,
3637
ConnMaxIdleTime: time.Hour,
3738
})
3839

@@ -76,6 +77,7 @@ func BenchmarkPoolGetRemove(b *testing.B) {
7677
Dialer: dummyDialer,
7778
PoolSize: bm.poolSize,
7879
PoolTimeout: time.Second,
80+
DialTimeout: 1 * time.Second,
7981
ConnMaxIdleTime: time.Hour,
8082
})
8183

internal/pool/pool.go

+10-2
Original file line numberDiff line numberDiff line change
@@ -62,6 +62,7 @@ type Options struct {
6262

6363
PoolFIFO bool
6464
PoolSize int
65+
DialTimeout time.Duration
6566
PoolTimeout time.Duration
6667
MinIdleConns int
6768
MaxIdleConns int
@@ -140,7 +141,10 @@ func (p *ConnPool) checkMinIdleConns() {
140141
}
141142

142143
func (p *ConnPool) addIdleConn() error {
143-
cn, err := p.dialConn(context.TODO(), true)
144+
ctx, cancel := context.WithTimeout(context.Background(), p.cfg.DialTimeout)
145+
defer cancel()
146+
147+
cn, err := p.dialConn(ctx, true)
144148
if err != nil {
145149
return err
146150
}
@@ -230,15 +234,19 @@ func (p *ConnPool) tryDial() {
230234
return
231235
}
232236

233-
conn, err := p.cfg.Dialer(context.Background())
237+
ctx, cancel := context.WithTimeout(context.Background(), p.cfg.DialTimeout)
238+
239+
conn, err := p.cfg.Dialer(ctx)
234240
if err != nil {
235241
p.setLastDialError(err)
236242
time.Sleep(time.Second)
243+
cancel()
237244
continue
238245
}
239246

240247
atomic.StoreUint32(&p.dialErrorsNum, 0)
241248
_ = conn.Close()
249+
cancel()
242250
return
243251
}
244252
}

internal/pool/pool_test.go

+5
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@ var _ = Describe("ConnPool", func() {
2222
Dialer: dummyDialer,
2323
PoolSize: 10,
2424
PoolTimeout: time.Hour,
25+
DialTimeout: 1 * time.Second,
2526
ConnMaxIdleTime: time.Millisecond,
2627
})
2728
})
@@ -46,6 +47,7 @@ var _ = Describe("ConnPool", func() {
4647
},
4748
PoolSize: 10,
4849
PoolTimeout: time.Hour,
50+
DialTimeout: 1 * time.Second,
4951
ConnMaxIdleTime: time.Millisecond,
5052
MinIdleConns: minIdleConns,
5153
})
@@ -129,6 +131,7 @@ var _ = Describe("MinIdleConns", func() {
129131
PoolSize: poolSize,
130132
MinIdleConns: minIdleConns,
131133
PoolTimeout: 100 * time.Millisecond,
134+
DialTimeout: 1 * time.Second,
132135
ConnMaxIdleTime: -1,
133136
})
134137
Eventually(func() int {
@@ -306,6 +309,7 @@ var _ = Describe("race", func() {
306309
Dialer: dummyDialer,
307310
PoolSize: 10,
308311
PoolTimeout: time.Minute,
312+
DialTimeout: 1 * time.Second,
309313
ConnMaxIdleTime: time.Millisecond,
310314
})
311315

@@ -336,6 +340,7 @@ var _ = Describe("race", func() {
336340
PoolSize: 1000,
337341
MinIdleConns: 50,
338342
PoolTimeout: 3 * time.Second,
343+
DialTimeout: 1 * time.Second,
339344
}
340345
p := pool.NewConnPool(opt)
341346

options.go

+1
Original file line numberDiff line numberDiff line change
@@ -531,6 +531,7 @@ func newConnPool(
531531
PoolFIFO: opt.PoolFIFO,
532532
PoolSize: opt.PoolSize,
533533
PoolTimeout: opt.PoolTimeout,
534+
DialTimeout: opt.DialTimeout,
534535
MinIdleConns: opt.MinIdleConns,
535536
MaxIdleConns: opt.MaxIdleConns,
536537
MaxActiveConns: opt.MaxActiveConns,

0 commit comments

Comments
 (0)