Skip to content

Commit c55133a

Browse files
committed
modify to concurrent
1 parent ea69458 commit c55133a

File tree

4 files changed

+75
-59
lines changed

4 files changed

+75
-59
lines changed

internal/pool/bench_test.go

Lines changed: 12 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -30,11 +30,12 @@ func BenchmarkPoolGetPut(b *testing.B) {
3030
for _, bm := range benchmarks {
3131
b.Run(bm.String(), func(b *testing.B) {
3232
connPool := pool.NewConnPool(&pool.Options{
33-
Dialer: dummyDialer,
34-
PoolSize: bm.poolSize,
35-
PoolTimeout: time.Second,
36-
DialTimeout: 1 * time.Second,
37-
ConnMaxIdleTime: time.Hour,
33+
Dialer: dummyDialer,
34+
PoolSize: bm.poolSize,
35+
MaxConcurrentDials: bm.poolSize,
36+
PoolTimeout: time.Second,
37+
DialTimeout: 1 * time.Second,
38+
ConnMaxIdleTime: time.Hour,
3839
})
3940

4041
b.ResetTimer()
@@ -74,11 +75,12 @@ func BenchmarkPoolGetRemove(b *testing.B) {
7475
for _, bm := range benchmarks {
7576
b.Run(bm.String(), func(b *testing.B) {
7677
connPool := pool.NewConnPool(&pool.Options{
77-
Dialer: dummyDialer,
78-
PoolSize: bm.poolSize,
79-
PoolTimeout: time.Second,
80-
DialTimeout: 1 * time.Second,
81-
ConnMaxIdleTime: time.Hour,
78+
Dialer: dummyDialer,
79+
PoolSize: bm.poolSize,
80+
MaxConcurrentDials: bm.poolSize,
81+
PoolTimeout: time.Second,
82+
DialTimeout: 1 * time.Second,
83+
ConnMaxIdleTime: time.Hour,
8284
})
8385

8486
b.ResetTimer()

internal/pool/buffer_size_test.go

Lines changed: 20 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -25,9 +25,10 @@ var _ = Describe("Buffer Size Configuration", func() {
2525

2626
It("should use default buffer sizes when not specified", func() {
2727
connPool = pool.NewConnPool(&pool.Options{
28-
Dialer: dummyDialer,
29-
PoolSize: 1,
30-
PoolTimeout: 1000,
28+
Dialer: dummyDialer,
29+
PoolSize: 1,
30+
MaxConcurrentDials: 1,
31+
PoolTimeout: 1000,
3132
})
3233

3334
cn, err := connPool.NewConn(ctx)
@@ -47,11 +48,12 @@ var _ = Describe("Buffer Size Configuration", func() {
4748
customWriteSize := 64 * 1024 // 64KB
4849

4950
connPool = pool.NewConnPool(&pool.Options{
50-
Dialer: dummyDialer,
51-
PoolSize: 1,
52-
PoolTimeout: 1000,
53-
ReadBufferSize: customReadSize,
54-
WriteBufferSize: customWriteSize,
51+
Dialer: dummyDialer,
52+
PoolSize: 1,
53+
MaxConcurrentDials: 1,
54+
PoolTimeout: 1000,
55+
ReadBufferSize: customReadSize,
56+
WriteBufferSize: customWriteSize,
5557
})
5658

5759
cn, err := connPool.NewConn(ctx)
@@ -68,11 +70,12 @@ var _ = Describe("Buffer Size Configuration", func() {
6870

6971
It("should handle zero buffer sizes by using defaults", func() {
7072
connPool = pool.NewConnPool(&pool.Options{
71-
Dialer: dummyDialer,
72-
PoolSize: 1,
73-
PoolTimeout: 1000,
74-
ReadBufferSize: 0, // Should use default
75-
WriteBufferSize: 0, // Should use default
73+
Dialer: dummyDialer,
74+
PoolSize: 1,
75+
MaxConcurrentDials: 1,
76+
PoolTimeout: 1000,
77+
ReadBufferSize: 0, // Should use default
78+
WriteBufferSize: 0, // Should use default
7679
})
7780

7881
cn, err := connPool.NewConn(ctx)
@@ -104,9 +107,10 @@ var _ = Describe("Buffer Size Configuration", func() {
104107
// Test the scenario where someone creates a pool directly (like in tests)
105108
// without setting ReadBufferSize and WriteBufferSize
106109
connPool = pool.NewConnPool(&pool.Options{
107-
Dialer: dummyDialer,
108-
PoolSize: 1,
109-
PoolTimeout: 1000,
110+
Dialer: dummyDialer,
111+
PoolSize: 1,
112+
MaxConcurrentDials: 1,
113+
PoolTimeout: 1000,
110114
// ReadBufferSize and WriteBufferSize are not set (will be 0)
111115
})
112116

internal/pool/pool.go

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -377,9 +377,11 @@ func (p *ConnPool) asyncNewConn(ctx context.Context) (*Conn, error) {
377377

378378
cn, err := p.newConn(w.ctx, true)
379379
delivered := w.tryDeliver(cn, err)
380-
if err == nil && !delivered {
380+
if err == nil && delivered {
381+
return
382+
} else if err == nil && !delivered {
381383
p.Put(w.ctx, cn)
382-
} else {
384+
} else { // freeTurn after error
383385
p.freeTurn()
384386
}
385387
}(w)

internal/pool/pool_test.go

Lines changed: 39 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -20,11 +20,12 @@ var _ = Describe("ConnPool", func() {
2020

2121
BeforeEach(func() {
2222
connPool = pool.NewConnPool(&pool.Options{
23-
Dialer: dummyDialer,
24-
PoolSize: 10,
25-
PoolTimeout: time.Hour,
26-
DialTimeout: 1 * time.Second,
27-
ConnMaxIdleTime: time.Millisecond,
23+
Dialer: dummyDialer,
24+
PoolSize: 10,
25+
MaxConcurrentDials: 10,
26+
PoolTimeout: time.Hour,
27+
DialTimeout: 1 * time.Second,
28+
ConnMaxIdleTime: time.Millisecond,
2829
})
2930
})
3031

@@ -46,11 +47,12 @@ var _ = Describe("ConnPool", func() {
4647
<-closedChan
4748
return &net.TCPConn{}, nil
4849
},
49-
PoolSize: 10,
50-
PoolTimeout: time.Hour,
51-
DialTimeout: 1 * time.Second,
52-
ConnMaxIdleTime: time.Millisecond,
53-
MinIdleConns: minIdleConns,
50+
PoolSize: 10,
51+
MaxConcurrentDials: 10,
52+
PoolTimeout: time.Hour,
53+
DialTimeout: 1 * time.Second,
54+
ConnMaxIdleTime: time.Millisecond,
55+
MinIdleConns: minIdleConns,
5456
})
5557
wg.Wait()
5658
Expect(connPool.Close()).NotTo(HaveOccurred())
@@ -130,12 +132,13 @@ var _ = Describe("MinIdleConns", func() {
130132

131133
newConnPool := func() *pool.ConnPool {
132134
connPool := pool.NewConnPool(&pool.Options{
133-
Dialer: dummyDialer,
134-
PoolSize: poolSize,
135-
MinIdleConns: minIdleConns,
136-
PoolTimeout: 100 * time.Millisecond,
137-
DialTimeout: 1 * time.Second,
138-
ConnMaxIdleTime: -1,
135+
Dialer: dummyDialer,
136+
PoolSize: poolSize,
137+
MaxConcurrentDials: poolSize,
138+
MinIdleConns: minIdleConns,
139+
PoolTimeout: 100 * time.Millisecond,
140+
DialTimeout: 1 * time.Second,
141+
ConnMaxIdleTime: -1,
139142
})
140143
Eventually(func() int {
141144
return connPool.Len()
@@ -309,11 +312,12 @@ var _ = Describe("race", func() {
309312

310313
It("does not happen on Get, Put, and Remove", func() {
311314
connPool = pool.NewConnPool(&pool.Options{
312-
Dialer: dummyDialer,
313-
PoolSize: 10,
314-
PoolTimeout: time.Minute,
315-
DialTimeout: 1 * time.Second,
316-
ConnMaxIdleTime: time.Millisecond,
315+
Dialer: dummyDialer,
316+
PoolSize: 10,
317+
MaxConcurrentDials: 10,
318+
PoolTimeout: time.Minute,
319+
DialTimeout: 1 * time.Second,
320+
ConnMaxIdleTime: time.Millisecond,
317321
})
318322

319323
perform(C, func(id int) {
@@ -340,10 +344,11 @@ var _ = Describe("race", func() {
340344
Dialer: func(ctx context.Context) (net.Conn, error) {
341345
return &net.TCPConn{}, nil
342346
},
343-
PoolSize: 1000,
344-
MinIdleConns: 50,
345-
PoolTimeout: 3 * time.Second,
346-
DialTimeout: 1 * time.Second,
347+
PoolSize: 1000,
348+
MaxConcurrentDials: 1000,
349+
MinIdleConns: 50,
350+
PoolTimeout: 3 * time.Second,
351+
DialTimeout: 1 * time.Second,
347352
}
348353
p := pool.NewConnPool(opt)
349354

@@ -367,8 +372,9 @@ var _ = Describe("race", func() {
367372
Dialer: func(ctx context.Context) (net.Conn, error) {
368373
panic("test panic")
369374
},
370-
PoolSize: 100,
371-
MinIdleConns: 30,
375+
PoolSize: 100,
376+
MaxConcurrentDials: 100,
377+
MinIdleConns: 30,
372378
}
373379
p := pool.NewConnPool(opt)
374380

@@ -385,8 +391,9 @@ var _ = Describe("race", func() {
385391
Dialer: func(ctx context.Context) (net.Conn, error) {
386392
return &net.TCPConn{}, nil
387393
},
388-
PoolSize: 1,
389-
PoolTimeout: 3 * time.Second,
394+
PoolSize: 1,
395+
MaxConcurrentDials: 1,
396+
PoolTimeout: 3 * time.Second,
390397
}
391398
p := pool.NewConnPool(opt)
392399

@@ -416,8 +423,9 @@ var _ = Describe("race", func() {
416423

417424
return &net.TCPConn{}, nil
418425
},
419-
PoolSize: 1,
420-
PoolTimeout: testPoolTimeout,
426+
PoolSize: 1,
427+
MaxConcurrentDials: 1,
428+
PoolTimeout: testPoolTimeout,
421429
}
422430
p := pool.NewConnPool(opt)
423431

0 commit comments

Comments
 (0)